我通过python中的串口发送命令,但它们被发送多次而不是一次

2022-01-18 00:00:00 python python-3.x serial-port

问题描述

我正在使用 com 端口串行发送一些具有特定响应的命令..这些命令保存在一个文件中..我通过文件逐行读取每个命令并通过 com 端口串行发送..但是当我我正在使用魔术终端(软件)从接收器端看到它..我发现每个命令都会执行多次..我只发送一次..我在 pycharm 中编写了一个代码..在控制台中我是看到该命令只执行一次,但从 uart 接收端来看,故事是另一回事..我被这个问题困住了..我保持相同的波特率和一切,但无法诊断问题..代码的 github 链接是:

在这种情况下,我们在计算机上有两个真正的串行端口.在第一个 (COM9) 上,我们正在运行一个 Modbus 服务器(您可以将其想象为一堆内存地址,每个地址都存储一个 16 位数字).在 COM10 上,我们有一个客户端正在发送查询,询问前 10 个地址的内容(使用 Modbus 术语称为寄存器).在一般用例中,我们将这些端口通过电缆连接,因此我们知道(理论上)COM10 上的客户端正在发送一个数据帧,请求这十个寄存器和 上的服务器COM9 用存储在这些寄存器中的数字来回答.但我们只能看到服务器上的内容(图片左侧)和客户端正在接收的内容(右侧).我们看不到总线上正在运行的是什么(是的,我们知道它是什么,但我们不知道 Modbus 协议在内部到底是什么样子).

如果我们想点击总线以查看每一端发送和接收的内容,我们可以使用 com0com 创建几个虚拟端口并使用 Termite 创建一个端口转发连接,类似于以下屏幕截图:

现在我们已将 Modbus 服务器移至其中一个虚拟串行端口(本例中为 COM4).安装 com0com 后,我们得到(默认情况下,但您可以更改名称或添加更多端口对,当然)一对转发端口(COM4<-->COM5).现在,如果我们想查看通过端口循环的内容,我们打开 Termite(图片右下角)并设置另一个端口转发方案,在本例中是从虚拟端口 COM5 到真实端口端口 COM9.

最后(与我们嗅探之前完全相同),我们将 COM9COM10 用电缆连接在一起.但现在我们可以看到总线上来回的所有数据(您在 Termite 上看到的所有 HEX 值都以绿色/蓝色字体显示).

如您所见,这将提供类似于您可以使用 更专业的工具.

i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue.. github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list

def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv

this i have used to receive the 2 byte response..

解决方案

There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).

I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.

One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.

As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.

To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:

In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).

If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:

Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.

Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).

As you can see, this will offer something similar to what you can do with more professional tools.

相关文章