Skip to main content
 首页 » 编程设计

python之pyserial 2.7 文档错误,python 3.4 TypeError : an integer is required

2025年05月04日31jillzhang

我正在尝试从 its webpage 运行最简单的 pyserial 2.7 示例在 python shell 中:

>>> 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 

请您参考如下方法:

ser.write("Hello".encode()) 

你能试试这个吗,它应该能解决你的问题。或者;

check.write(b"Hello") 

我怀疑需要将其转换为字节数组