【发布时间】:2015-07-03 19:22:24
【问题描述】:
我收到以下行中的错误:
command = input("please type command.example open 1")
#call the serial_connection() function
ser.write(b"%d\r\n"%command)
本质上我想要用户写的输入并解析成ser.write,不要求输入直接将字符串放入ser.write如:
ser.write(b'close1\r\n')
工作正常,仅当我尝试将输入结果作为字符串包含在 ser.write 中时才会出现问题
更多代码:
ser = 0
#Initialize Serial Port
def serial_connection():
COMPORT = int(input("Please enter the port number: "))
ser = serial.Serial()
ser.baudrate = 38400 #Suggested rate in Southco documentation, both locks and program must be at same rate
ser.port = COMPORT - 1 #counter for port name starts at 0
#check to see if port is open or closed
if not ser.isOpen():
print ('The Port %d is open - Will attempt to close lock 1 Stephan: '%COMPORT + ser.portstr)
#timeout in seconds
ser.timeout = 10
ser.open()
command = input("please type command.example open 1")
#call the serial_connection() function
ser.write(b"%d\r\n"%command)
else:
print ('The Port %d is **open** Stephan' %COMPORT)
如有任何澄清,请提出建议。
【问题讨论】:
标签: python python-3.x serial-port hardware pyserial