添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am working on a automation test case for a system and need a automated modbus input device.

My use case here is to implement a Raspberry pi based RTU modbus slave and connected to a modbus master.

I want this Raspberry Pi based slave to populate and send a response to master when ever master requests for a register value.

I am new to this protocol and environment, I am not able to find any python script or libraries where we have a modbus slave client.

I came across this below Serial python code and I could successfully decode modbus requests from the Master,

import serial
import time
receiver = serial.Serial(     
     port='/dev/ttyUSB0',        
     baudrate = 115200,
     parity=serial.PARITY_NONE,
     stopbits=serial.STOPBITS_ONE,
     bytesize=serial.EIGHTBITS,
     timeout=1
while 1:
      x = receiver.readline()
      print x

The problem I am facing here is this block of code just prints a seris of serial bits and I am not sure how to decode modbus packets from these...

OUTPUT: b'\x1e\x03\x00\x19\x00\x01W\xa2\x1e\x10\x00\x0f\x00\x01\x02\x03 +\xb7\x1e\x03\x00\n' b'\x00\x02\xe6f\x1e\x03\x00\t\x00\x01Vg\x1e\x10\x00\x10\x00\x01\x02\x01,(\xbd\x1e\x03\x00\n' b'\x00\x02\xe6f\x1e\x03\x00\t\x00\x01Vg\x1e\x10\x00\x11\x00\x01\x02\x03 (\t\x1e\x03\x00\n' b'\x00\x02\xe6f\x1e\x03\x00\t\x00\x01Vg\x1e\x10\x00\x12\x00\x01\x02\x01,)_\x1e\x03\x00\n' b'\x00\x02\xe6f\x1e\x03\x00\t\x00\x01Vg\x1e\x03\x00\n' b'\x00\x02\xe6f\x1e\x03\x00\t\x00\x01Vg\x1e\x03\x00\n'

Pymodbus library has many examples for server/slave/responder (usually devices are a server/slave) and master/client/requester. The procedure in the Modbus protocol is such that the server/slave must give a request from the master/client side, then response to it. With this definition what is your purpose for implement on RPi? slave or master? server or client? – Benyamin Jafari Jan 25, 2019 at 5:09 Hi @BenyaminJafari, Thanks for responding. I am quite confused with the Modbus terms of Master and Slave. But from my understanding, the device which requests for a register value is the Master (Client/requester) and the Device which responds back to that register value will be the Server (Slave/Responder). So in my use case, RPi is the Server/Slave/Responder. – vkrm Jan 27, 2019 at 12:42 Hi, so you need to an Async ModbusRtuServer which is mentioned in my answer. Tel me if there is a problem. – Benyamin Jafari Jan 27, 2019 at 12:57 @BenyaminJafari Hi Thank you so much for pointing me int he right direction, the Async ModbusRtuServer was perfect it handled my use case. I did do some tweaking in library files for formatting the packets to my need. Still there was no issue in the library. Thanks again for your time :P – vkrm Jan 31, 2019 at 6:59

Pymodbus library has several examples for server/slave/responder (typically devices are server/slave) and master/client/requester. The procedure in Modbus protocol is such that the server/slave must give a request from the master/client side, then respond to it.

Here is a Modbus RTU client (master) code snippet to read data from a Modbus RTU server (slave) or a Modbus device using pymodbus library:

from pymodbus.client.sync import ModbusSerialClient
client = ModbusSerialClient(
    method='rtu',
    port='/dev/ttyUSB0',
    baudrate=115200,
    timeout=3,
    parity='N',
    stopbits=1,
    bytesize=8
if client.connect():  # Trying for connect to Modbus Server/Slave
    '''Reading from a holding register with the below content.'''
    res = client.read_holding_registers(address=1, count=1, unit=1)
    '''Reading from a discrete register with the below content.'''
    # res = client.read_discrete_inputs(address=1, count=1, unit=1)
    if not res.isError():
        print(res.registers)
    else:
        print(res)
else:
    print('Cannot connect to the Modbus Server/Slave')
  • Here is a Pymodbus Asynchronous Server Example.
  • And here is a Pymodbus Synchronous Server Example.
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.