Timeout Error when testing ModBus over RS485, using Ardunio mod bus libraries

Hi Team,

As part of a larger project, I am trying to communicate with a 0-20mA output module over ModBus. (datasheet/output module)

I have wired up a UART to RS485 convertor board (Datasheet) to serial port 1 on a Arduino Mega 2560 board. (see attachments for wiring diagrams). The convertor board uses a MAX485 chip( Datasheet)

I have also used a standard ModBus library (Link) pulled from GitHub. I have made two modifications to the library:

  • Lines 708-709 : Before transmission, I pull Pins 50 & 52 high to enable transmission on the MAX485 chip on the convertor board

  • Lines 724-725 : After transmission, I pull Pins 50 & 52 low to enable receive on the MAX485 chip

I have attached .zip file of the modified library to this post.

My code follows the example provided on the GitHub page:

#include <ModbusMaster.h>

ModbusMaster node;

 

void setup() {

  //initialise two serial ports, one for MODBUS (port 1) and one for Serial comms to PC (Serial 0)

  Serial.begin(9600);

  Serial1.begin(9600);

 

  //sets up the modbus master class from the library

  node.begin(1,Serial1);

}

 

void loop() {

  uint8_t result;

 

  //Read a single register 48 on the  analog output device

  result = node.readHoldingRegisters(48,1);

 

  //print the result to the serial port 0 (conencted to PC)

  Serial.print(result,HEX);

  delay(3000);

}

This code should poll the value of holding registrar 48 on the analog output module and print the "Result" back into the serial port 0. "Result" returns '0x00' if the Modbus transaction was successful but when I run the code I only get "0xE2" which according to the ModbusMaster.h file indicates a response timeout exception.

Naturally I have tried a number of de-bugging steps to isolate the problem:

  1. I used an Oscilloscope to verify that the signals being transmitted are as expected ( square waves)

  2. I tried communicating with other Modbus devices but get the same error no matter what device I use.

  3. I have tried looping through the slave ID address in case my output module was configured incorrectly, with no success.

  4. I have re-written my code from scratch, with no success.

  5. I have tried to read from other registrars within the output module, with no success.

I am pretty sure the error is in my code or my understanding of RS485 ModBus.

I would like some help as I am unsure how to overcome this problem.

I was hoping that someone can look through the provided information and help me:

  1. Verify that I have used the library correctly. It is the first library that comes up on a google search so I sure that it will not have any errors.

  2. Verify that my code makes functional sense

  3. Try use my code to communicate with another ModBus device independently. This will help me isolate the problem.

I can provide as much information as required, so please don't hesitate to ask!

Thanks for the assistance,

Segar

Modbus wiring diagram - Analog output module.pdf (988 KB)

ModbusMaster-master (2).zip (266 KB)

ModBusMasterTest1.ino (554 Bytes)

  //Pull these pins low to enable receive functionaility onn the MAX485 chip
  digitalWrite(50, HIGH);
  digitalWrite(52, HIGH);

It might have worked if the code would do what the comment says.