无源串口监视器
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能忽略这些信号,特别是如果您使用基于硬件的流量控制。我假设除了 RX(读)和 TX(写)之外,您至少还必须在两个端口之间链接以下信号
关于您的代码的注释:
您定义了一个超时,这意味着 IO 功能会阻塞(即使只是很短的时间)。考虑将其设置为 0
您调用 read 时不带参数。这只会读取一个字节。您有什么理由不想立即阅读更多内容?会减少开销。
考虑添加一个条件来退出 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)
Comments about your code:
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
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.
Consider adding a condition to exit the while loop. At the moment the code will run until pyserial throws an exception.