问题
I am trying to open a serial port with python. This is on Ubuntu. I import the openinterface.py and enter in this
ser = openinterface.CreateBot(com_port = "/dev/ttyUSB1", mode="full")
I get an error saying "unsupported operand types for -: 'str' and 'int'" I tried the same call with single quotes instead of double, and with no quotes at all.
How can I fix this? Or is there an alternative function to use? I only know the basics of Python so maybe its some small syntax thing I am not noticing? Any help would be appreciated, thanks.
回答1:
According to this page in Russian, there's a bug with the openinterface.py
file that tries to subtract one from the port argument. It suggests making this change (removing the - 1
on line 803) with sed
:
sed -ie "803s/ - 1//" openinterface.py
Either try that, or see if there's an updated version of openinterface.py
.
回答2:
This is what you want if you are using python 3:
import serial #import pyserial lib
ser = serial.Serial("/dev/ttyS0", 9600) #specify your port and braudrate
data = ser.read() #read byte from serial device
print(data) #display the read byte
来源:https://stackoverflow.com/questions/4234823/how-to-open-a-serial-port-with-pyserial