【发布时间】:2015-07-02 22:43:38
【问题描述】:
我正在尝试发送以下 ASCII 命令: 关闭1
使用 PySerial,下面是我的尝试:
import serial
#Using pyserial Library to establish connection
#Global Variables
ser = 0
#Initialize Serial Port
def serial_connection():
COMPORT = 3
global ser
ser = serial.Serial()
ser.baudrate = 38400
ser.port = COMPORT - 1 #counter for port name starts at 0
#check to see if port is open or closed
if (ser.isOpen() == False):
print ('The Port %d is Open '%COMPORT + ser.portstr)
#timeout in seconds
ser.timeout = 10
ser.open()
else:
print ('The Port %d is closed' %COMPORT)
#call the serial_connection() function
serial_connection()
ser.write('open1\r\n')
但结果我收到以下错误:
Traceback (most recent call last):
, line 31, in <module>
ser.write('open1\r\n')
, line 283, in write
data = to_bytes(data)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 76, in to_bytes
b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer is required
不确定我将如何解决这个问题。 close1 只是我要发送的 ASCII 命令的一个示例,还有 status1 来查看我的锁是打开还是关闭等。
提前致谢
【问题讨论】:
标签: python python-3.x serial-port hardware pyserial