1

I am trying to use Arduino as Modbus- RTU slave and Raspberry Pi as a Modbus master I have interfaced Max485 (RS485 to TTL converter) with Arduino and Rs485 to USB converter in Raspberry Pi, Initially, I was able to send data from Arduino to Raspberry Pi, but the data seems to inconsistent and fails to read most times

I am using https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino for Arduino Minimal Modbus for raspberry pi

Connections:

  • DI(max485)- TX ( Arduino),
  • DE(max485) -D2 ( Arduino),
  • RE(max485) - D2( Arduino),
  • Ro(max485) - RX( Arduino),
  • Vcc(max485) -5v( Arduino),
  • Gnd (max485)- GND( Arduino)

code for Arduino

#include <ModbusRtu.h>
uint16_t au16data[6] = {};
Modbus slave(5,0,2); 
void setup() {
  slave.begin(19200,SERIAL_8N2 ); 
 }
void loop() {
  au16data[0]=100;
  au16data[1]=200;
  au16data[2]=300;
  au16data[3]=400;
  au16data[4]=500;
  au16data[5]=600;
  slave.poll( au16data, 6 );
  delay(3000);
}

python code

import minimalmodbus
from datetime import datetime
import time

instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 5)
instrument.serial.baudrate = 19200
instrument.serial.bytesize = 8
instrument.serial.parity = minimalmodbus.serial.PARITY_NONE
instrument.serial.stopbits = 2
instrument.serial.timeout =6
instrument.mode = minimalmodbus.MODE_RTU

while(1):
    try:
        val = instrument.read_registers(0,6)
        # Registernumber, number of decimals
        print val
    except IOError:
        error_msg = "Failed to read from device"
        error_time = datetime.now()
        print "IO Error Time",error_time.isoformat(),error_msg 

    except ValueError:
        error_msg = "Failed to read CRC"
        error_time = datetime.now()
        print "Value Error Time",error_time.isoformat(),error_msg
    time.sleep(2)

connection

but now I am getting The exception of failed to read

  • why the delay in slave? it must run slave.poll to respond to requests from master – Juraj Dec 21 '18 at 11:58
  • the array positions could be replaced by sensor values, some sensor do work slow for instance DHT11 – Zeref Dec 21 '18 at 12:08
  • time sensor reading as BlinkWithoutDelay example, but the slave must poll, the loop must run – Juraj Dec 21 '18 at 12:14
  • even without the delay I am getting the exception of failed to read – Zeref Dec 21 '18 at 12:22
  • on fritzing and in code you have DE as pin 2. in the wiring table in Question you have D4? – Juraj Dec 21 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.