我正在尝试使用 Python (PyCharm) 读取 modbus 设备上的寄存器。我已经确认了 COM 端口、波特率和其他通信设置,我可以使用设备应用程序读取该值(它是水位记录仪)。我没有收到仪器的任何响应。
使用 mbpoll 可以读取寄存器 -
mbpoll -B -m RTU -t 4:float -a 1 -b 19200 -r 46 -c 2 /dev/ttyUSB0
我的代码如下 -
import minimalmodbus
import serial
instrument = minimalmodbus.Instrument('COM5', 1) # port name, slave address (in decimal)
instrument.serial.port = 'COM5' # this is the serial port name
instrument.serial.baudrate = 19200 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout = 3 # seconds
instrument.address = 1 # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
instrument.clear_buffers_before_each_transaction = True
temperature = instrument.read_float(registeraddress=40046, functioncode=3, number_of_registers=2, byteorder=0) # Registernumber, number of decimals
print(temperature)
import minimalmodbus
import serial
instrument = minimalmodbus.Instrument('COM5', 1) # port name, slave address (in decimal)
instrument.serial.port = 'COM5' # this is the serial port name
instrument.serial.baudrate = 19200 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout = 0.1 # seconds
instrument.address = 1 # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
#nstrument.clear_buffers_before_each_transaction = True
temperature = instrument.read_float(registeraddress=45, functioncode=4, number_of_registers=2, byteorder=0) # Registernumber, number of decimals
try:
print(temperature)
except:
print(temperature)
编辑以包含尝试 - 除外
感谢任何帮助!
编辑: 链接至设备手册 - https://in-situ.com/en/pub/media/support/documents/Modbus_Manual.pdf 设备是 Level Troll 400,通过制造商电缆连接到 PC
编辑2: 我尝试合并最小的 modbus 结构,但没有成功。
编辑3: 我可以使用 Modbus Poll 读取寄存器。寄存器是40046,所以我理解这是保持寄存器的寄存器45?我如何将其转换为minimalmodbus?
编辑4: 我没有与最小的 modbus 结婚 - 我很乐意使用任何工具来完成此任务
编辑5:
我还尝试了使用不同值的 深度 = Instrument.read_long(x, x)
最佳答案
如果有人偶然发现这个问题并遇到同样的问题,只需更新解决方案即可。正如其他建议中半暗示的那样,我连接的设备有一个“ sleep ”期等,需要轮询一次,但不成功,然后才能成功返回任何后续轮询的值。我对我的电枢代码表示歉意,但对我有用的解决方案如下 -
import minimalmodbus
import serial
instrument = minimalmodbus.Instrument('COM5', 1) # port name, slave address (in decimal)
instrument.serial.port = 'COM5' # this is the serial port name
instrument.serial.baudrate = 19200 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout = 1 # seconds
instrument.address = 1 # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
#nstrument.clear_buffers_before_each_transaction = True
try:
temperature = instrument.read_long(registeraddress=9001, functioncode=3,
byteorder=0) # Registernumber, number of decimals
print(temperature)
except:
pass
try:
temperature = instrument.read_long(registeraddress=9001, functioncode=3,
byteorder=0) # Registernumber, number of decimals
print(temperature)
except:
pass
关于python - 使用 Python 读取 MODBUS 寄存器时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67669720/