串行端口通信协议
在与数字万用表(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,这对于基于RS232/RS485协议的设备很常见。
来自,我引用:
在许多设备上,这实际上是您可以打开和关闭的设置。
现在,至于您的问题:
在发送新角色之前,您应该阅读每个字符,以验证是否正确接收该字符。但是在您的代码中实际上是在阅读其中一个角色之前发送所有角色。
在您具有可靠连接的情况下,您的方法也将起作用,但是因此您需要阅读两次。一旦验证是否收到命令,并且第二次检索实际数据。
请记住,阅读缓冲区可能仅限于一定数量。如果您在查询大量数据并发送大量命令时经历意外行为,则可能是由于这些缓冲区已满。
This is actually quite common with devices based on RS232/RS485 protocols.
From the manual of the machine you mentioned, I quote:
On a lot of devices this is actually a setting which you can turn on and off.
Now, as for your question:
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.