无源串口监视器

发布于 2024-12-10 01:29:38 字数 2005 浏览 0 评论 0原文

我使用 pyserial 打开两个端口,然后将我从另一个端口读取的内容写入每个端口。然后,我将一个物理 com 端口连接到其中一个端口,并将一个虚拟 com 端口连接到另一个端口。虚拟 com 端口依次连接到我的模拟器所连接的第二个虚拟 com 端口:

Hardware device <> COM1 <> Python Script <> VCOM2 <> VCOM3 <> Simulator

我可以看到通信正确地进入和退出我的脚本中的 com 端口,但由于硬件无法正常工作,所以出现了一些问题。与模拟器正确通信。

我有一个旧的 c 应用程序,可以代替 Python 脚本运行,并且可以正常工作。然而,它写得真的很糟糕,而且我并没有真正的兴趣修复它的所有错误。所以我希望我可以用 python 脚本替换这个应用程序。我最终希望用时间戳记录通过端口的数据。

我在这两种情况下都使用了正确的波特率,但是我似乎遗漏了一些东西。我应该在每个端口(例如 DTR)之间传输信号吗? pyserial 具有以下功能:

sendBreak(duration=0.25)
setBreak(level=True)
setRTS(level=True)
setDTR(level=True)
getCTS()
getDSR()
getRI()
getCD()

我对哪些信号感兴趣?

编辑:

当我轮询每个端口的这些值时:

getCTS()、getDSR()、getRI()、getCD()

我得到: 真、假、假、真 COM1 False, False, False, False VCOM2

但是,我发现 CD 有时会变为 false。我如何通过 VCOM2 将其转出或者我需要这样做吗?

编辑:

这是我的代码。一旦通信开始,脚本就会锁定,我需要重新启动计算机才能释放端口。我无法终止 Windows 7 上关联的 python 进程...

import serial

class NewMonitor():
    def __init__(self, com_port_1, com_port_2):
        self.read_time_in_seconds = 0.1
        self.serialPort1 = serial.Serial(com_port_1, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)
        self.serialPort2 = serial.Serial(com_port_2, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)

        try:
            while True:
                item = self.serialPort1.read()
                self.serialPort2.write(item)
                self.serialPort2.setRTS(self.serialPort1.getCTS())
                self.serialPort2.setDTR(self.serialPort1.getDSR())


                item = self.serialPort2.read()
                self.serialPort1.write(item)
                self.serialPort1.setRTS(self.serialPort2.getCTS())
                self.serialPort1.setDTR(self.serialPort2.getDSR())
        finally:
                self.serialPort1.close()
                self.serialPort2.close()

I'm using pyserial to open two ports, and then write to each what I read from the other. I then have a physical com port connected to one of these ports and a virtual com port connected to the other. The virtual com port is in turn connected to a second virtual com port to which my simulator connects:

Hardware device <> COM1 <> Python Script <> VCOM2 <> VCOM3 <> Simulator

I can see that the communication is correctly entering and exiting the com ports in my script but something isn't right since the the hardware is failing to communicate correctly with the simulator.

I have an old c app that I can run in place of the Python Script and this works correctly. However, it is really badly written and I've no real interest in fixing all its bugs. So I'm hoping I can replace this app with a python script. I eventually wish to log the data passing through the ports with a timestamp.

I am using the correct baud rate in both cases, however I seem to be missing something. Should I be transferring signals between each port, DTR for example? pyserial has these functions:

sendBreak(duration=0.25)
setBreak(level=True)
setRTS(level=True)
setDTR(level=True)
getCTS()
getDSR()
getRI()
getCD()

What signals am I interested in?

EDIT:

When I poll these values for each port:

getCTS(), getDSR(), getRI(), getCD()

I Get:
True, False, False, True COM1
False, False, False, False VCOM2

However, I see that CD becomes false sometimes. How do I transfer this out through VCOM2 or do I need to do this?

EDIT:

Here's my code. Once communication starts the script locks up and I need to restart my computer to release the port. I can't kill the associated python process on Windows 7...

import serial

class NewMonitor():
    def __init__(self, com_port_1, com_port_2):
        self.read_time_in_seconds = 0.1
        self.serialPort1 = serial.Serial(com_port_1, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)
        self.serialPort2 = serial.Serial(com_port_2, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)

        try:
            while True:
                item = self.serialPort1.read()
                self.serialPort2.write(item)
                self.serialPort2.setRTS(self.serialPort1.getCTS())
                self.serialPort2.setDTR(self.serialPort1.getDSR())


                item = self.serialPort2.read()
                self.serialPort1.write(item)
                self.serialPort1.setRTS(self.serialPort2.getCTS())
                self.serialPort1.setDTR(self.serialPort2.getDSR())
        finally:
                self.serialPort1.close()
                self.serialPort2.close()

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

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

发布评论

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

评论(1

仅一夜美梦 2024-12-17 01:29:38

您不能忽略这些信号,特别是如果您使用基于硬件的流量控制。我假设除了 RX(读)和 TX(写)之外,您至少还必须在两个端口之间链接以下信号

CTS -> RTS
DSR -> DTR

关于您的代码的注释:

  1. 您定义了一个超时,这意味着 IO 功能会阻塞(即使只是很短的时间)。考虑将其设置为 0

  2. 您调用 read 时不带参数。这只会读取一个字节。您有什么理由不想立即阅读更多内容?会减少开销。

  3. 考虑添加一个条件来退出 while 循环。目前,代码将一直运行,直到 pyserial 引发异常。

you cannot ignore the signals, especially if you use HW based flow-control. I assume that you at least have to link the following signals between both ports besides RX(read) and TX(write)

CTS -> RTS
DSR -> DTR

Comments about your code:

  1. You defined a timeout, this means the IO function will block (even if it is only for a short amount of time). Consider setting it to 0

  2. You call read without parameter. This will only read a single byte. Any reason why you do not want to read more at once? Would reduce the overhead.

  3. Consider adding a condition to exit the while loop. At the moment the code will run until pyserial throws an exception.

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