title: Python script for RTU Modbus Slave
body: <p>I am working on a automation test case for a system and need a automated modbus input device.</p>
  <p>My use case here is to implement a Raspberry pi based RTU modbus slave and connected to a modbus master.</p>
  <p>I want this Raspberry Pi based slave to populate and send a response to master when ever master requests for a register value.</p>
  <p>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.</p>
  <p>I came across this below Serial python code and I could successfully decode modbus requests from the Master,</p>
  <pre class="lang-py prettyprint-override"><code>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
  </code></pre>
  <p>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...</p>
  <p>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'</p>