1

我有一个脚本,它应该从与 MODBUS 连接的传感器收集浓度数据。当我运行脚本时,会发生此错误:

rtu 模式下的校验和错误: '\x00\x00' 而不是 'c\x85' 。响应是:'\x01\x04\x0e\x00\x00\x00\x00\x00\x00'(简单响应:'\x01\x04\x0e\x00\x00\x00\x00\x00\x00')

这是我的代码:

import time
import os
import serial
import minimalmodbus



delay = 1
pdata = 1


#-- H2 sensor read out  -----------------------------------
#def getH2():
res = 0      
ADRH1 = 1 # read out register for H2
ADRH2 = 0 # read out register for H2

try:
        H2 = minimalmodbus.Instrument('COM6',ADRH1)  # open serial port
        H2.serial.port          # this is the serial port name
        H2.serial.baudrate = 9600   # Baud
        H2.serial.bytesize = 8
        H2.serial.parity   = serial.PARITY_NONE
        H2.serial.stopbits = 1
        H2.serial.timeout  = 0.25   # seconds


        val = H2.read_long(ADRH2,functioncode=4,byteorder=0)
        print("test")
        res = float(val)/10
        print(val)
        print("---")
        print(res)

except ValueError:
        print("Failed to read H2")

如何更改对传感器的请求或来自传感器的响应,以使两个数据流的校验和相同?

真诚的,卢卡斯

4

1 回答 1

2

我想到了!

我需要使用read_registers而不是read_register,因为传感器一次返回 7 个寄存器。

于 2019-12-17T15:27:39.617 回答