通过 Python 中的 USB 转换器进行串行通信 - 如何解决这个问题? [英] Serial communication over USB converter in Python - how to aproach this?

查看:53
本文介绍了通过 Python 中的 USB 转换器进行串行通信 - 如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 程序需要每秒从串行端口接受一个字符串.我打算使用 RS-232 到 USB 转换器.该应用程序将在 Ubuntu 10.04 下运行.

A python program needs to accept a string every second from a serial port. I plan on using a RS-232 to USB converter. The application is going to work under Ubuntu 10.04.

我该如何解决这个问题?我使用 pySerial 还是 libusb?

How do I approach this? Do I use pySerial or libusb?

在此期间需要进行一些处理,因此同步通信不可行.我是使用某种中断还是需要打开单独的线程?或者我是否使用阻塞读取,相信 1s 对我的计算来说已经足够了(现在已经足够了……)?

There needs to be done some processing in the meantime, so synchronous communication is not viable. Do I use some kind of interrupts or do I need to open separate threads? Or do I use blocking reads, believing that 1s is enough for my computations (it is plenty ... for now)?

我知道,RTFM,但是从一开始就朝着正确的方向前进将为我节省很多时间!谢谢你的陪伴.

I know, RTFM, but heading in the correct direction from the start will save me a lot of time! Thanks for bearing with me.

推荐答案

如果您的 RS232-USB 转换器在 Ubuntu 中有一个驱动程序,使它看起来像一个 COM 端口,那么您将需要使用 pySerial(接口与任何其他 COM 端口).如果您的设备没有驱动程序,那么您可能必须使用 libusb 并找到适用于您的特定设备的协议.现在大多数主要的 RS232-USB 转换器都在 Linux 内核中提交和维护了基于 USB 串行的驱动程序.只需向您的供应商确认这一点.

If your RS232-USB converter has a driver in Ubuntu that makes it look like a COM port then you will want to use pySerial (the interface is the same as any other COM port). If there is no driver for your device then you might have to use libusb and find the protocol for your specific device. Most major RS232-USB converters these days have usbserial based drivers committed and maintained in the Linux kernel. Simply check with your vendor for this.

进行并行处理的方法有很多种,但通常我用两种方式编写应用程序:

There are many ways to do parallel processing but typically I write my applications in two ways:

  • 有一个读取线程,它只读取和填充本地线程安全缓冲区,以便在需要时为其他线程准备好数据.

  • Have a read thread that does nothing but read and fill a local thread safe buffer so data is ready for other threads when needed.

具有读取数据的读取线程,确定数据去向并通过消息传递/事件处理将数据传递给需要它的组件.

Have a read thread that reads data, determines where it goes and delivers it via messaging/event processing to the component that needs it.

此处的决定将取决于您的目标是什么,以及阅读之外需要进行多少处理.

The decisions here will depend on what your goals are, and how much processing is needed outside the reading.

这篇关于通过 Python 中的 USB 转换器进行串行通信 - 如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆