Unable to communicate with Modbus device

Hello everyone,

I have been scouring the forums trying to get my setup functioning and so far no luck. I am a bit of a novice so, that's isn't exactly helpful either.

I have an Arduino Mega 2560 and a piece of hardware that communicates using the Modbus standard via RS485. My RS485 converter is a MAX485.

The modbus hardware info:
-maximum polling interval of 10 ms
-32-bit Floating point or signed integer Little Endian [CDAB]
-the manual says all data is stored in 2 16-bit registers with starting addresses listed in the register map. The address below is the starting address for example.
-I am interested in measuring temperature which according to the register map verbatim is Hex address 0x41 which is type 32-bit integer.

I think part of my problem is that the library is expecting a 16-bit chunk of data and I am not exactly sure how to break this up into 2 pieces and then recombine them.

I have the MAX485 to Arduino, respectively, wired as follows:

Vcc - 5v
GND - GND
A - A on modbus device
B - B on modbus device
R0 - RX0
RE/DE - tied together to Digital pin 3
DI - TX0

I am using the ModbusMaster.h library from the Arduino IDE. There is a line near the bottom of my code, 'node.getResponseBuffer(0x00/10.00)' that I am not exactly sure what should go in place of '0x00'. The 10 is scaling I believe from other examples that I can find.

I would appreciate any resources or help that anyone can provide to get this functioning.

Thank you!

My code:

#include <ModbusMaster.h>


#define MAX485_DE      3

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_DE, 0);
}

void setup()
{

  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(38400);

//Baudrate of modbus device is 38400
  Serial1.begin(38400);
  
  // Modbus slave ID is 9
  node.begin(9, Serial1);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  //Need data from starting address 0x41 with length 2
   result = node.readHoldingRegisters(0x41, 2);

    //digitalWrite(MAX485_DE, 0);
     
    Serial.println(node.getResponseBuffer(0x00)/10.00);

  delay(500);
}

but

I guess you connected to RX1 and TX1 but you should check that!

What values do you get in result?

Why do you know you cannot communicate with the device?

Where is the link to the other device's manual?

Modbus defines to use the parity bit (although quite a few devices ignore that). Did you check that detail?

1 Like

I changed to Serial instead of Serial1 and defined SERIAL_8N1 as well.

I see a bunch of jumbled stuff in the serial monitor.
000048

The link to the manual for the modbus device is: https://www.vpinstruments.com/download/119/manual-vpflowscope-in-line/1qrQXZZ8SbpXxw88MMzQbSTv6LlPBS-7I/Manual%20VPFlowScope%20In-line%20-%20EN

Again, I am pretty new to this so I do appreciate the help!

Thank you

I downloaded a modbus traffic monitor to see what the arduino is broadcasting. Here is what I get:

The initial broadcast is wrong of course as I am trying to read a holding register which should be function code 03 not 02.

Could this be timing related or am I missing something completely?

Bad idea as you currently use that Serial for debugging.
You should keep Serial1 and connect to RX1 and TX1!

Probably just the debugging output you put on the same serial interface.

1 Like

Turns out the manual for the flowmeter has A/B reversed. I swapped them and if I use modpoll and a rs485 converter I can get what I need. I am doing that just to verify that I have data coming from the flowmeter.

I get the same response whether from Serial or Serial1. I have changed to Serial1 as a precaution noting what you said regarding debugging.

When I use Serial Port Monitor now, I see the following:

Any suggestions? Also, thank you for your patience. I imagine that answering some of these questions is rather trivial and monotonous after a while.

Okay so further update... I changed the baudrate to 9600 on the flowmeter as well as in the code and I get exactly what I need now. It works...