How to open a serial port with pyserial?

故事扮演 提交于 2019-12-08 04:12:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!