Reading Modbus RTU sensor data with Arduino and Max rs485 485 to TTL converter

Hi everyone,
This is my first post on a Arduino Forum, first i would like to thank everyone in here for any input received regarding my query and would like to point out that i am really appreciative that there is a resource like this available on the Web.

My query pertains to a project i am doing where i would like to read the data from a sound sensor that works on MODBUS RTU.

Using Modbusmaster library i have incorporated my sensor parameters as per below but wanted to confirm if this approach is adequate since i have not received any input from sensor yet.

the sensor parameters are as follows:

Transmission mode
: MODBUS RTU Baud rate : 9600bps Data bits: 8 Stop bit: 1 Check bit: no
Slave address
:the factory default is 01H (set according to the need,00H to FFH
 The 03 H Function Code Example: Read The Noise value
Host Scan Order
(slave address:0x01)
01
03 00 00 00 01 840A
Slave Response
01
03 02 02 55 791B
Noise:(
0255) H= (597) D , 597 /10 =59.7db

and the code i got so far is the following:

#include <Arduino.h>
#include <ModbusMaster.h>  //Library for using ModbusMaster

#define MAX485_DE 3
#define MAX485_RE_NEG 2



ModbusMaster node;  //object node for class ModbusMaster

void preTransmission()  //Function for setting stste of Pins DE & RE of RS-485
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

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

void setup() {

  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_RE_NEG, 0); // Init in receive mode
  digitalWrite(MAX485_DE, 0);

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

  node.begin(1, Serial);    //Slave ID as 1

  //Callback for configuring RS-485 Transreceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop() {

  uint8_t result;
  uint16_t data[6];

  result = node.readHoldingRegisters(0x30001, 1); //Read 1 Holding starting from 840A
  state = !state;

  if (result == node.ku8MBSuccess)
  {
    Serial.print("Noise (db): ");
    Serial.println(result);
  }

  delay(1000);

}

i believe that to read holding registers i need the fixed bits to be displayed:

result = node.readHoldingRegisters(0x30001, 1); //Read 1 Holding starting from 840A

The problem is that i don't get any interaction between rs485 and sensor, i have also tried 0000 instead of 30001 as sometimes sensors start from 0.

-Should i use a better rs485 to ttl converter?
-Is there a direction or resource i can look for where i could perhaps get the code to work?

(sensor datasheet attached)

Kind Regards

RK300-06 Noise sensor user manualv1.0.pdf (209 KB)

A couple of things:

It looks like you are using the same serial port for modbus comms and normal text output. You don't say which Arduino you are using. If you are using an UNO or similar, then you will have to use a software serial port for your modbus comms.

It might help you to know what the message 01 03 00 00 00 01 84 0A means.
01 = device address
03 = function code -> read holding registers
00 = start address high byte
00 = start address low byte
00 = number of registers to read high byte
01 = number of registers to read low byte
84 = CRC16 checksum high byte
0A = CRC16 checksum low byte

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.