How to read RS485 data in arduino

Hi everyone,

I’m kinda new to Arduino and Arduino programming. I have an RS485 data coming from a DPC mass flow controller by AALBORG Instruments. I want to read the data in my Arduino. I’m using an RS485 to UART TTL converter (MAX485) to communicate with the flow controller. I have taken bits and pieces of codes from the internet and modified it as i wanted it. I’m not able to read the data at all and i’m not able to figure out where i have gone wrong.

I have attached the link to the RS485 to UART TTL converter.

Any help would be much appreciated.
Thanks in advance.

RS485 to UART TTL converter:

test_1.ino (698 Bytes)

1 Like

Hi Karthik,

I have no clue on the type of data coming from your mass flow meter.

I have seen the interface hardware you are using. It is correct. Just to assist you on certain key points like using HW serial for RS485 and converting the data that you receive there are a few things before you can get it to work. I am using a EM6400 power meter and could do it.

I am attaching the code below and you can start off by modifying this … I have left the development history intact as that can help you trace your flaws…

And to get going I have also attached the library file that i used.

/*
 02 Aug 2020

 ModBus_EM6400_read.ino

 Code to read the Modbus register of EM6436H power meter using a UNO. 

 Not working .... tried with many Software libraries and Parity changes. Getting zero as result most times

 06 Aug 2020 

 Parity cannot be set in any Software Serial. So will need to use a Hardware serial and then set it.  Thus
 UNO cannot be used for this.

 Use the Serial1 HW Serial of the Adafruit 32u4 and the problem is solved !! 

 Pay attention to the size of the data types , addressing scheme , and swapping received data order to get the float. 

 Hardware Setup :  
 1. Adafruit 32U4 pcb : Rx / TX / 5V / GND  connected to the RS485<>TTL module 
 2. RS485<>TTL module connected to D+ and D- of the Schneider EN6436H data bus
 
  
*/

#include <ModbusMaster.h>

ModbusMaster node; 

union X             // Create a Union to hold the data types in line with the Modbus register being read..
{
  float f;          // 32 bit float                          
  uint16_t i[2];    // Array of 16bit word
} unX;

//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

void setup()
{
  Serial.begin(9600, SERIAL_8N1);            //Baud rate is 9600 / 8N1
  Serial1.begin(19200, SERIAL_8E1);          //Modbus Baud rate is 9600/ 8E1

  Serial.println(" Beginning Modbus master !" ); 
 
  node.begin(Serial1);                      // Communicate with Modbus slave ID 1 over Hardware Serial
}

//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

void loop()
{
  uint8_t j, result;
  uint16_t data[2];

  // Read 2 nos 16-bit registers starting at register 3110 to RX buffer. 
  // Modbus address starts at 1 while MCU index starts with 0. So load 3109 for address
  
  //result = node.readHoldingRegisters(1,3109, 2);      // To read Line Frequency 

  result = node.readHoldingRegisters(1,3035, 2);     //  To read line-neutral voltage
               
  if (result == node.ku8MBSuccess)
  {
    for (j = 0; j < 2; j++)
    {
      data[j] = node.getResponseBuffer(j);
      Serial.println(unX.i[j], HEX); 
    }
  }
  unX.i[1] = data[0];               // Rearrange the received data to get the float ...
  unX.i[0] = data[1]; 
  Serial.println(unX.f, 3); 

  delay(2000); 
}

//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

ModbusMaster.zip (11.8 KB)

1 Like

a good tutorial on Arduino RS485

I find it useful when working on RS485 to have a USB-RS485 converter on a PC to monitor the bus, e.g. FTDI USB-RS485