I ordered a sensor that measures three parameters at specific register addresses. The sensor uses a modbus protocol for communication (RS 485). I use the arduino as master and the sensor as slave. Since I don't have the sensor yet, there is a software (Modsim32) that allows me to simulate a Slave. I would like to make my arduino communicate with the simulation software through the RS485 using the Modbus RTU protocol.
when a run the program based on ModbusMaster library I find in the serial port a 226, according to what i had read that is a error message. But why?
Here is my code
#include <ModbusMaster.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/*!
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE 3
#define MAX485_RE_NEG 2
LiquidCrystal_I2C lcd(0x27, 20, 4);
// 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()
{
// Modbus communication runs at 115200 baud
Serial.begin(9600);
// Modbus slave ID 10
node.begin(10, Serial);
lcd.init();
lcd.backlight();
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t result;
// Read 1 registers starting at 0x3100)
result = node.readInputRegisters(0x3100, 1); // I am getting 226 here
lcd.setCursor(0, 0);
lcd.print(result);
/* if (result == node.ku8MBSuccess)
{
Serial.print("Vbatt: ");
Serial.println(node.getResponseBuffer(0x04)/100.0f);
Serial.print("Vload: ");
Serial.println(node.getResponseBuffer(0xC0)/100.0f);
Serial.print("Pload: ");
Serial.println((node.getResponseBuffer(0x0D) +
node.getResponseBuffer(0x0E) << 16)/100.0f);
}*/
delay(1000);
}
Does anyone has an idea about this Problem?
Here you can see a picture of the Slave simulation software

