串行端口通信协议

发布于 2025-01-23 00:12:22 字数 726 浏览 2 评论 0原文

在与数字万用表(Ex。BKPrecision 2831E)等设备的串行通信中,为什么我需要发送一次查询命令,但两次读取输出?例如,我为测量的电压发送了一个查询命令,并收到了一个回声,但没有电压值。 然后,我两次发送了查询命令,该命令返回了回声和测量电压。本质上,要读取测量的电压,我必须连续两次发送相同的查询命令。 我不明白这个概念。任何人都可以帮助我解决这一推理。

我在下面附上了一个示例代码:

def readoutmm(portnumber_multimeter):
    import serial
    import time
    ser2 = serial.Serial(
    port="com"+str(portnumber_multimeter),
    baudrate=9600,
    bytesize=serial.EIGHTBITS,  
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE
    )
    ser2.write(b'fetc?\n') # Query command
    voltage= ser2.readline() # Returns echo
    voltage=ser2.readline() # Returns measured voltage
    voltage=float(voltage)
    ser2.close()
    packet=[voltage]
    return packet 

In serial communication with devices such as a digital Multimeter (ex. BK Precision 2831E), why do I need to send a query command once but read the output twice? For instance, I sent a query command for the voltage measured, and received an echo but no value of voltage.
I then sent the query command twice which returned the echo and the measured voltage. In essence, to read out the voltage measured, I had to send the same query command in succession twice.
I do not understand this concept. Can anyone kindly help me out with this reasoning.

I have attached a sample code here below:

def readoutmm(portnumber_multimeter):
    import serial
    import time
    ser2 = serial.Serial(
    port="com"+str(portnumber_multimeter),
    baudrate=9600,
    bytesize=serial.EIGHTBITS,  
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE
    )
    ser2.write(b'fetc?\n') # Query command
    voltage= ser2.readline() # Returns echo
    voltage=ser2.readline() # Returns measured voltage
    voltage=float(voltage)
    ser2.close()
    packet=[voltage]
    return packet 

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

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

发布评论

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

评论(1

夜深人未静 2025-01-30 00:12:22

实际上,这对于基于RS232/RS485协议的设备很常见。

来自,我引用:

万用表收到的字符将再次发送回控制器。控制器将
在从仪表中正确接收到最后一个返回的字符之前,请勿发送下一个字符。如果
控制器无法接收从仪表发回的字符,可能的原因列出了
如下:

  • 串行接口未正确连接。
  • 检查是否为仪表和控制器选择了相同的波特率。
  • 当仪表忙于执行总线命令时,它不会接受
    串行接口同时。因此,控制器发送的字符将被忽略。

顺序
为了确保正确发送和接收整个命令,控制器应再次发送没有返回字符的字符。

在许多设备上,这实际上是您可以打开和关闭的设置。

现在,至于您的问题:

为什么我需要发送一次查询命令,但两次读取输出?

在发送新角色之前,您应该阅读每个字符,以验证是否正确接收该字符。但是在您的代码中实际上是在阅读其中一个角色之前发送所有角色。

在您具有可靠连接的情况下,您的方法也将起作用,但是因此您需要阅读两次。一旦验证是否收到命令,并且第二次检索实际数据。

请记住,阅读缓冲区可能仅限于一定数量。如果您在查询大量数据并发送大量命令时经历意外行为,则可能是由于这些缓冲区已满。

This is actually quite common with devices based on RS232/RS485 protocols.

From the manual of the machine you mentioned, I quote:

The character received by the multimeter will be sent back to the controller again. The controller will
not send the next character until the last returned character is received correctly from the meter. If
the controller fails to receive the character sent back from the meter, the possible reasons are listed
as follows:

  • The serial interface is not connected correctly.
  • Check if the same baud rate is selected for both the meter and the controller.
  • When the meter is busy with executing a bus command, it will not accept any character from
    the serial interface at the same time. So the character sent by controller will be ignored.

In order
to make sure the whole command is sent and received correctly, the character without a return character should be sent again by the controller.

On a lot of devices this is actually a setting which you can turn on and off.

Now, as for your question:

why do I need to send a query command once but read the output twice?

You are supposed to read every character back before sending a new one to validate if the character was received correctly. But in your code are actually sending all the character before reading a single one of them.

In scenario's where you have a reliable connection, you method will work as well, but as a consequence you'll need to read twice; once to validate if the command was received and the second time to retrieve the actual data.

Do keep in mind that read buffers might be limited to a certain amount. If you are experiencing unexpected behavior while querying large amount of data and sending a lot of commands, it might be due to the fact these buffers are full.

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