Modbus Temp sensor interface over RS485 to Serial TTL Module.

Hello

I am looking forward to learn modbus and how to use it on arduino.

I have a hum/temp sensor which has a modbus output ( i will attach the datasheet )

I have a RS485 to Serial TTL module and an Arduino one.

Following all documentation the Modbus output +/- is connected to the AB input on the RS485 to Serial TTL.

The RS485 to Serial TTL module connects to the Arduino as follows, DI--TX, DE -- D3, RE -- D2 and RO --Rx.

The code is as follows, I have copied bits and pieces from here and there, but I am not quite sure how everything works.

// include the library code:

#include <ModbusMaster.h>
#define MAX485_DE      3
#define MAX485_RE_NEG  2


// instantiate ModbusMaster object
ModbusMaster node;


void preTransmission()
{
  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);

 //init in receive mode

 digitalWrite(MAX485_RE_NEG, 0);
 digitalWrite (MAX485_DE, 0);

Serial.begin (19200);
node.begin(254, Serial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
 
}

void loop()
{
  
 // int result = node.readHoldingsRegisters
  
  uint8_t resultMain;
  resultMain = node.readInputRegisters(0x101, 2);
  if (resultMain == node.ku8MBSuccess)
{
  Serial.println(node.getResponseBuffer(0x101));
}
delay(1000);
}

I get some strange characters instead of numbers, like a wrong baut rate. I already looked into it.

Any help provided will be highly Appreciate it.

Thanks.

Tony

HUMIDITY TRANSMITTER.pdf (875 KB)

When I search on ModbusMaster I get this link Arduino Playground - ModbusMaster Library in there is says it uses the RTU version of the protocol. Which means the characters you see/don't see are hex values not ASCII test values. If it was using Modbus ASCII then you would see human readable text.

I guess your call to

  Serial.println(node.getResponseBuffer(0x101));

Doesn't return what you'd expected as the method is defined as

uint16_t ModbusMaster::getResponseBuffer(uint8_t u8Index)

so the index is a byte and a byte cannot hold a 0x101 value. The call returns the index 0x01 instead. Which is not that bad because the ModbusMaster library doesn't expect the register address but the index into the result. As you requested 2 registers you can use indexes 0 and 1.