Hello people,
I am trying to read the input registers of a sensor that I have connected to my Controllino Maxi through RS485.
The Controllino Maxi has RS485 implemented which is why I don't need any extra shield for the RS485.
My code is an edit of the RS485 examples provided by Controllino. But it is not working and I am only getting back zeros.
The sensor I am using is a RHE42 with the manual here: https://www.rheonik.com/fileadmin/Documents/PDF/Installation/RHE40_Desktop_Reference.pdf
It has a baud rate of 19200, even parity and one stop bit, the adresses I am trying to read are 0x4900 and 0x6500. About the number of coils I am not very sure as I am a beginner, but maybe someone can explain that to me.
This is my code:
#include <Controllino.h>
#include "ModbusRtu.h"
#include <SoftwareSerial.h>
#define MasterModbusAdd 0
#define SlaveModbusAdd 1
#define RS485Serial 3
Modbus ControllinoModbusMaster(MasterModbusAdd, RS485Serial, 0);
uint16_t ModbusSlaveRegisters[8];
modbus_t ModbusQuery[2];
uint8_t myState;
uint8_t currentQuery;
unsigned long WaitingTime;
void setup() {
Serial.begin(19200);
Serial.println("-----------------------------------------");
Serial.println("CONTROLLINO Modbus RTU Master Test Sketch");
Serial.println("-----------------------------------------");
Serial.println("");
// Define Modbus queries for the register addresses
ModbusQuery[0].u8id = SlaveModbusAdd;
ModbusQuery[0].u8fct = 3;
ModbusQuery[0].u16RegAdd = 0x4900;
ModbusQuery[0].u16CoilsNo = 1;
ModbusQuery[0].au16reg = ModbusSlaveRegisters;
ModbusQuery[1].u8id = SlaveModbusAdd;
ModbusQuery[1].u8fct = 3;
ModbusQuery[1].u16RegAdd = 0x6500;
ModbusQuery[1].u16CoilsNo = 1;
ModbusQuery[1].au16reg = ModbusSlaveRegisters;
ControllinoModbusMaster.begin(19200, SERIAL_8N1);
ControllinoModbusMaster.setTimeOut(20000);
WaitingTime = millis() + 3000;
myState = 0;
currentQuery = 0;
}
void loop() {
switch(myState) {
case 0:
if (millis() > WaitingTime) myState++;
break;
case 1:
Serial.print("---- Sending query ");
Serial.print(currentQuery);
Serial.println(" -------------");
ControllinoModbusMaster.query(ModbusQuery[currentQuery]);
Serial.print("State after query: ");
Serial.println(ControllinoModbusMaster.getState());
ControllinoModbusMaster.query(ModbusQuery[currentQuery]);
myState++;
currentQuery++;
if (currentQuery == 2) {
currentQuery = 0;
}
break;
case 2:
ControllinoModbusMaster.poll();
if (ControllinoModbusMaster.getState() == COM_IDLE) {
myState = 0;
WaitingTime = millis() + 3000;
Serial.println("---------- READ RESPONSE RECEIVED ----");
Serial.print("Slave ");
Serial.print(SlaveModbusAdd, DEC);
Serial.print(" Register: 0x");
Serial.print(ModbusQuery[currentQuery].u16RegAdd, HEX);
Serial.print(" Value: ");
Serial.println(ModbusSlaveRegisters[0], HEX);
Serial.println("-------------------------------------");
Serial.println("");
}
break;
}
}