Python'ser.write'永久停止Windows上的程序,可能会出错?

时间:2018-09-06 21:28:32

标签: python serial-port

我正在用python代码与arduino通信,串行传输多个字节。下面的代码是我的主要功能,一直工作到第一个或第二个“ ser.wrte”功能之后。令人困惑的是,该程序可以在Mac上正常运行,没有错误,但是任何运行Windows的笔记本电脑都存在上述问题(版本7和10已通过测试,均未成功)是否有基于代码的原因,或者是由于Windows相关串口问题?

#Communicates to arduino via serial port
def pump():
print('****************')
if len(seq) == 0:
    print("EmptySeqError: please store patterns in the sequence before sending it!")
    return
#######################
port = "COM17"
macport = "/dev/cu.usbmodem1411"
baudrate = 9600
####################### 
try:
    ser = serial.Serial(port, baudrate, timeout=0.1) #, write_timeout=0.1)
except serial.SerialException:
    print('SerialException: cannot create serial object')
    return
else:
    if ser.isOpen():
        print('serial port {} is opened successfully!'.format(ser.name))
for json in seq:
    MSB = int(json['Hex Code MSB'], 16)
    LSB = int(json['Hex Code LSB'], 16)
    V = int(json['Voltage'], 16) #For voltage transmission
    Vlsb = V & 0xff              #Voltage LSB
    Vmsb = (V>>8) & 0xff         #Voltage MSB
    print(V, 'in hex =', hex(V))
    print(Vlsb, 'in hex =', hex(Vlsb))
    print(Vmsb, 'in hex =', hex(Vmsb))
    #X = int(json['Xhigh'], 16)
    #Y = int(json['Ybase'], 16)
    #Z = int(json['Zsec'], 16)
    HD = int(json['High Delay'], 16)
    LD = int(json['Low Delay'], 16)
    print("**************")
    print("(MSB/LSB) number of bytes written: ", ser.write(bytes([MSB, LSB])))
    print("(Voltage) number of bytes written: ", ser.write(bytes([Vmsb,Vlsb])))
    print("(HD/LD) number of bytes written: ", ser.write(bytes([HD, LD])))
print("serial is closing...")
sys.stdout.flush()
ser.close()
#End of sending

0 个答案:

没有答案