我试图通过串行通信控制从Python连接到Arduino的led。我附上了Arduino和python的代码。但是当我在Python中运行代码时,我没有从led得到任何响应,尽管我没有收到任何错误。也许我在语法上犯了些错误?在import serial
import time
arduino=serial.Serial('COM3',250000,timeout=5.0)
m=[]
commands=open('1.txt','r')
lines=commands.readlines()
for line in lines:
m.append(line)
commands.close()
s=0
while s!=len(m):
m[s]=float(m[s])
s+=1
s=0
def delay():
x=0
y=0
while x!=y:
x+=1
while s!=len(m):
c=str(m[s])
if m[s]==1:
arduino.write(b'c')
time.sleep(2)
print('1on')
elif m[s]==-1:
arduino.write(b'c')
time.sleep(2)
print('1off')
delay()
elif m[s]==2:
arduino.write(b'c')
time.sleep(2)
print('2on')
elif m[s]==-2:
arduino.write(b'c')
time.sleep(2)
print('2off')
elif m[s]==3:
arduino.write(b'c')
time.sleep(2)
print('3on')
elif m[s]==-3:
arduino.write(b'c')
time.sleep(2)
print('3off')
s+=1
这是在Arduino中控制Python中的led的代码。Arduino代码如下
^{pr2}$