我试图在python shell中从its webpage运行pyserial 2.7最简单的例子:
>>> import serial
>>> ser = serial.Serial(0) # open first serial port
>>> print ser.name # check which port was really used
>>> ser.write("hello") # write a string
>>> ser.close() # close port
然而,它不起作用:
>>> import serial
>>> ser = serial.Serial(0)
>>> print ser.name
SyntaxError: Missing parentheses in call to 'print'
>>> print (ser.name)
COM1
>>> ser.write("hello")
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
ser.write("hello")
File "C:\Python34\lib\site-packages\serial\serialwin32.py", 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
答案 0 :(得分:1)
ser.write("Hello".encode())
你能试试这个,它应该可以解决你的问题。或;
check.write(b"Hello")
我怀疑需要将其转换为字节数组