如何用pyserial打开串口?

发布于 2024-10-03 08:45:37 字数 318 浏览 1 评论 0原文

我正在尝试用 python 打开串口。这是在 Ubuntu 上。我导入 openinterface.py 并输入此

ser = openinterface.CreateBot(com_port = "/dev/ttyUSB1", mode="full")

错误,提示“不支持的操作数类型 -: 'str' 和 'int'” 我尝试使用单引号而不是双引号进行相同的调用,并且根本不使用引号。

我该如何解决这个问题?或者有其他功能可以使用吗?我只知道Python的基础知识,所以也许它是一些我没有注意到的小语法问题?任何帮助将不胜感激,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-10-10 08:45:37

根据俄语页面,有openinterface.py 文件中尝试从端口参数中减一的错误。它建议使用 sed 进行此更改(删除第 803 行上的 - 1):

sed -ie "803s/ - 1//" openinterface.py

尝试一下,或者查看是否有 openinterface.py< 的更新版本/代码>。

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.

遇见了你 2024-10-10 08:45:37

如果您使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文