如何将寄存器USB RS485读取到任何Modbus设备



我的设备地址是1,我尝试读取保持寄存器,寄存器地址是0。

我尝试用pyserial与Modbus设备通信,但pyserial这是我的代码:

import serial,time
ser = serial.Serial(port='/dev/ttyUSB2')
while True:
val = ser.read(b'x01x03x00')
print(val)

这是我的错误:

Traceback (most recent call last):
File "modbus.py", line 6, in <module>
val = ser.read(b'x01x03x00') #Slave Address = 1, RTU Function number = 3 = Read Holding Registers, Register Address = 0
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/serial/serialposix.py", line 481, in read
while len(read) < size:
TypeError: '<' not supported between instances of 'int' and 'bytes'

我还尝试了使用minimalmodbus进行通信:

import minimalmodbus
instrument = minimalmodbus.Instrument('/dev/ttyUSB2', 1)  # port name, slave address (in decimal)
## Read temperature (PV = ProcessValue) ##
temperature = instrument.read_register(0, 1)  # Registernumber, number of decimals
print(temperature)

我也得到了这个错误:

Traceback (most recent call last):
File "modbus.py", line 15, in <module>
temperature = instrument.read_register(0, 1)  # Registernumber, number of decimals
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 447, in read_register
payloadformat=_PAYLOADFORMAT_REGISTER,
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1170, in _generic_command
payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1240, in _perform_command
response = self._communicate(request, number_of_bytes_to_read)
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1406, in _communicate
raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)

read()方法采用一个整数表示要读取的字节数。

你的意思是:

while True:
ser.write(b'x01x03x00')
val = ser.read()
print(val)

相关内容

  • 没有找到相关文章

最新更新