如何将 USB RS485 寄存器读取到任何 Modbus 设备? [英] How can i read register USB RS485 to Any Modbus Device?

查看:60
本文介绍了如何将 USB RS485 寄存器读取到任何 Modbus 设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设备地址是1,我试过读保持寄存器,寄存器地址是0.

My Device Address is 1, I tried Read Holding Register, and Register Address is 0.

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

I tried pyserial to communicate Modbus Device but pyserial this is my code:

import serial,time

ser = serial.Serial(port='/dev/ttyUSB2')

while True:
    val = ser.read(b'\x01\x03\x00')
    print(val)

这是我的错误:

Traceback (most recent call last):
  File "modbus.py", line 6, in <module>
    val = ser.read(b'\x01\x03\x00') #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进行通信:


I also tried minimalmodbus to communicate:

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() 方法采用整数作为要读取的字节数.

The read() method takes an integer for the number of bytes to read.

你的意思是:

while True:
    ser.write(b'\x01\x03\x00')
    val = ser.read()
    print(val)

这篇关于如何将 USB RS485 寄存器读取到任何 Modbus 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆