title: Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
body: <p><strong>Problem</strong></p>
  
  <p>pymodbus master/client can send a request to the slave/server. The slave/server make the things to return ready and is waiting for the master/client to pick them up. Despite the readiness of the server/slave, the master/client just returns the error "Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)".</p>
  
  <p><strong>Setup</strong></p>
  
  <p>I use the laptop as server/slave with this adapter: <a href="https://rads.stackoverflow.com/amzn/click/com/B076WVFXN8" rel="noreferrer" rel="nofollow noreferrer">https://www.amazon.com/dp/B076WVFXN8/ref=twister_B076X1BS4H?_encoding=UTF8&amp;psc=1</a></p>
  
  <p>I have an Raspberry Pi 3 / BananaPi as master/client with this adapter attached: <a href="https://www.aliexpress.com/item/32781613765.html?spm=a2g0s.9042311.0.0.1aec4c4d0EXx8M" rel="noreferrer">https://www.aliexpress.com/item/32781613765.html?spm=a2g0s.9042311.0.0.1aec4c4d0EXx8M</a></p>
  
  <p>I am following most of this tutorial for the setup, except for the Arduino is swapped with the laptop adapter: <a href="https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-and-raspberry-pi" rel="noreferrer">https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-and-raspberry-pi</a>
  - pin connections for the Raspberry is as in the tutorial.</p>
  
  <p>I have this program as server/slave on my laptop:</p>
  
  <pre><code>#!/usr/bin/env python
  from pymodbus.server.sync import StartTcpServer
  from pymodbus.server.sync import StartUdpServer
  from pymodbus.server.sync import StartSerialServer
  from pymodbus.device import ModbusDeviceIdentification
  from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSparseDataBlock
  from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
  from pymodbus.transaction import ModbusRtuFramer, ModbusBinaryFramer
  
  import logging
  FORMAT = ('%(asctime)-15s %(threadName)-15s'
            ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
  logging.basicConfig(format=FORMAT)
  log = logging.getLogger()
  log.setLevel(logging.DEBUG)
  
  def run_server():
  
      slave_store1 = ModbusSlaveContext(co=ModbusSequentialDataBlock(0, [1]*16))
      slave_store2 = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [1]*16))
      slave_store3 = ModbusSlaveContext(ir=ModbusSequentialDataBlock(0, [5]*16))
      slave_store4 = ModbusSlaveContext(hr=ModbusSequentialDataBlock(0, [5]*16))
  
      slaves = {
          0x01: slave_store1,
          0x02: slave_store2,
          0x03: slave_store3,
          0x04: slave_store4,
      }
  
      context = ModbusServerContext(slaves=slaves, single=False)
  
      identity = ModbusDeviceIdentification()
      identity.VendorName = 'Pymodbus'
      identity.ProductCode = 'PM'
      identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
      identity.ProductName = 'Pymodbus Server'
      identity.ModelName = 'Pymodbus Server'
      identity.MajorMinorRevision = '2.2.0'
  
      # RTU:
      StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/ttyUSB0', timeout=4, baudrate=115200, stopbits=1, bytesize=8, parity='N') 
  
  if __name__ == "__main__":
      run_server()
  </code></pre>
  
  <p>The python version on server/slave is:</p>
  
  <pre><code>$ python3 --version
  Python 3.5.2
  </code></pre>
  
  <p>And I start it with this command:</p>
  
  <pre><code>$ python3 pymodbus_sync_serv_example_2019.07.05-1316.py
  </code></pre>
  
  <p>I have the following as master/client on the Raspberry Pi 3 / BananaPi:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import logging
  FORMAT = ('%(asctime)-15s %(threadName)-15s '
  '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
  logging.basicConfig(format=FORMAT)
  log = logging.getLogger()
  log.setLevel(logging.DEBUG)
  
  UNIT = 0x1
  
  def run_sync_client():
  
      client = ModbusClient(method='rtu', port='/dev/ttyS2', timeout=4, baudrate=115200, stopbits=1, bytesize=8, parity='N')
  
      print(client)
  
      client.connect()
  
      log.debug("===================================")
      log.debug("Read input registers")
      log.debug("")
      rr = client.read_input_registers(1, 2, unit=3)
      print(rr)
  
      client.close()
  
  if __name__ == "__main__":
      #for _ in range(10):
      run_sync_client()
  </code></pre>
  
  <p><strong>Test and analyse</strong></p>
  
  <p>I have tried Raspberry Pi 3 as well as BananaPi. Same results.</p>
  
  <p>I have tried baudrate= 9600, 38400 and now 115200.</p>
  
  <p>timeout is already high as you can see in the code.</p>
  
  <p>Logs for server/slave:</p>
  
  <pre><code>2019-07-07 13:35:00,333 MainThread      DEBUG    sync           :45       Client Connected [/dev/ttyUSB0:/dev/ttyUSB0]
  2019-07-07 13:35:00,333 MainThread      DEBUG    sync           :522      Started thread to serve client
  2019-07-07 13:35:08,341 MainThread      DEBUG    rtu_framer     :180      Getting Frame - 0x4 0x0 0x1 0x0 0x2
  2019-07-07 13:35:08,341 MainThread      DEBUG    factory        :137      Factory Request[ReadInputRegistersRequest: 4]
  2019-07-07 13:35:08,341 MainThread      DEBUG    rtu_framer     :115      Frame advanced, resetting header!!
  2019-07-07 13:35:08,342 MainThread      DEBUG    context        :64       validate: fc-[4] address-2: count-2
  2019-07-07 13:35:08,342 MainThread      DEBUG    context        :78       getValues fc-[4] address-2: count-2
  2019-07-07 13:35:08,342 MainThread      DEBUG    sync           :143      send: [ReadRegisterResponse (2)]- b'030404000500050846'
  </code></pre>
  
  <p>The above server/slave just waits with a blinking curser after this last log line...</p>
  
  <p>Logs for master/client:</p>
  
  <pre><code>ModbusSerialClient(rtu baud[115200])
  2019-07-07 13:35:04,428 MainThread      DEBUG    pymodbus_sync_client_example_2019.07.05-1319:165      ===================================
  2019-07-07 13:35:04,429 MainThread      DEBUG    pymodbus_sync_client_example_2019.07.05-1319:166      Read input registers
  2019-07-07 13:35:04,430 MainThread      DEBUG    pymodbus_sync_client_example_2019.07.05-1319:167      
  2019-07-07 13:35:04,430 MainThread      DEBUG    transaction    :111      Current transaction state - IDLE
  2019-07-07 13:35:04,430 MainThread      DEBUG    transaction    :116      Running transaction 1
  2019-07-07 13:35:04,431 MainThread      DEBUG    transaction    :215      SEND: 0x3 0x4 0x0 0x1 0x0 0x2 0x21 0xe9
  2019-07-07 13:35:04,431 MainThread      DEBUG    sync           :73       New Transaction state 'SENDING'
  2019-07-07 13:35:04,432 MainThread      DEBUG    transaction    :224      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
  2019-07-07 13:35:08,439 MainThread      DEBUG    transaction    :234      Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)) 
  2019-07-07 13:35:08,440 MainThread      DEBUG    rtu_framer     :235      Frame - [b''] not ready
  2019-07-07 13:35:08,441 MainThread      DEBUG    transaction    :390      Getting transaction 3
  2019-07-07 13:35:08,442 MainThread      DEBUG    transaction    :189      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
  Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
  </code></pre>
  
  <p>The python version on master/client is:</p>
  
  <pre><code>$ python3 --version
  Python 3.5.2
  </code></pre>
  
  <p>And I start it with this command:</p>
  
  <pre><code>$ python3 pymodbus_sync_client_example_2019.07.05-1319.py
  </code></pre>
  
  <p>The rights for the /dev's on the Raspberry/BananaPi are:</p>
  
  <pre><code>$ ls -l /dev/ttyS*
  crw--w---- 1 root tty     249, 0 Jul  7 11:21 /dev/ttyS0
  crw-rw---- 1 root dialout 249, 1 Jul  7 11:22 /dev/ttyS1
  crw-rw---- 1 root dialout 249, 2 Jul  7 13:35 /dev/ttyS2
  crw-rw---- 1 root dialout 249, 3 Jul  7 11:20 /dev/ttyS3
  </code></pre>
  
  <p>And on the server/slave on the laptop:</p>
  
  <pre><code>$ ls -l /dev/ttyUSB0
  crw-rw---- 1 root dialout 188, 0 Jul  7 13:35 /dev/ttyUSB0
  </code></pre>
  
  <p>I have tried to send simple numbers with RS485 protocol. They can be send from master/Raspberry/BananaPi to the laptop, but not the other way around.</p>
  
  <p>Have I the wrong rights settings for the devices?...</p>
  
  <p>What am I doing wrong?...</p>
  
  <p>What am I missing?...</p>
  
  <p>As RS485 only works in the one way, I do not think that pymodbus is the problem (?)... (My logic says that pymodbus builds in the RS485 standard, and if that underlying layer of RS485 does not work, pymodbus will not. Is that assumption correct?)</p>
  
  <p>I know some people are talking about that the Raspberry Pi is 3.3V on the pins and does not work with 5V pin-units. Despite that does all tutorials seem to ignore that fact and work. - Or are they just faking that it works? The TTL specifications say that all above 2.5V will be accepted as HIGH. SO in THEORY, 3.3V should be OK, just as the tutorials suggest.</p>
  
  <p>I have by purpose yet not attached any resistors on the tx/rx wires for pull up/down. The tutorials don't suggest them.</p>
  
  <p>I have tested the RS85 adapter sitting on the laptop with a modbus temperature-humidity sensor. This seems to work flawless. So this fact points in direction of BananaPi/Raspberry Pi and the RS485 adapter combination + software + settings to be flawed somehow.</p>