python中写入和监听同一个串口

发布于 2024-11-27 12:16:39 字数 146 浏览 1 评论 0原文

有没有办法在不出现“COM 端口正在使用”错误的情况下执行此操作?我有一个在 com 端口上监听 gps GPRMC 句子的服务。但我的测试计算机上没有此设备。所以我想编写一个Python脚本来模拟端口上的GPRMC语句,而我的另一个Python脚本则监听相同的端口并进行解析。

Is there anyway to do this without getting a "COM PORT IN USE" error? I have a a service that listens to gps GPRMC sentences on a com port. But I don't have this device on my testing computer. So I wanted to write a python script to simulate GPRMC sentences on the port while my other python script listens to the same port and parses.

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

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

发布评论

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

评论(1

孤蝉 2024-12-04 12:16:39

写入串行端口不会在串行端口上留下可供同一设备读取的消息。这不是串行端口的工作方式,也不是大多数操作系统的编写方式以允许作为缓冲区行为。你真正需要的是一个虚拟串口。

查看关于 COM 端口重定向器的维基百科文章的部分,看看其中是否有任何一个会满足您的需求。否则,我建议搜索 COM 端口仿真器、串行端口虚拟化等,直到找到适合您的用例和操作系统的软件。这可能很困难,特别是如果计时对您的模拟很重要的话。

编辑:为了让这一点更清楚一些,我们来谈谈 pySerial 库实际上是如何与 python 通信的。 pySerial 只是与操作系统的串行端口 API 进行通信。通常,操作系统会将其建模为写入信息的位置和从中读取信息的位置(在几乎所有现代计算系统中缓冲)。需要理解的重要一点是,从操作系统的角度(串行端口的建模方式)来看,写入位置只能写入,而读取位置只能读取。这可能是也可能不是实际串行硬件与机器的接口方式,在我使用过的大多数串行端口硬件和接口设计中,出于简单性和降低成本的目的,情况都是如此。因此,您只有两个基本选择。

  1. 给操作系统一个虚拟串行端口,您可以以某种方式读取和写入

  2. 可能更简单,放置一个空调制解调器适配器并使用串行电缆连接这两个端口。现在,您可以在一个端口上提供服务,在另一个端口上提供模拟设备脚本。

Writing to a serial port does not leave a message on the serial port to be read by the same device. This just isn't how a serial port works and is not how most OSes are written to allow as a buffer behavior. What you really need is a virtual serial port.

Check out this section of a wikipedia article on COM port redirectors and see if any of it will fulfill your needs. Otherwise I recommend searching for COM port emulator, serial port virtualization, etc. until you find software that will work for your use case and operating system. This might be hard, especially if timing is important to your simulations.

Edit: To make this slightly more clear, let's talk about what the pySerial library is actually doing to communicate with python. pySerial is just communicating to the OS's API for the serial port. The OS will, generally, model this as a location to write information to and a location to read information from (buffered in just about all modern computing systems). What's important to understand is that from the point of view of the OS (how the serial port is modeled), the write location can ONLY be written to and the read location can only be read from. This may or may not be how the actual serial hardware interfaces with the machine, in most serial port hardware and interface designs that I've worked with, this is the case for the sake of simplicity and reduced cost. Because of this, you are down to two basic choices.

  1. Give the OS a virtual serial port that you can read to AND write from somehow

  2. Possibly simpler, put a null modem adapter on one of your computer's serial ports and, using a serial cable, connect the two ports. You can now have your service on one port and your simulated device script on the other.

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