I have sniffed the data to and from the M21+ and i have valid data going out and coming back but the M21+ is not recognizing it. I have attached what is basically the example code. Can anyone see anything wrong with the Rx portion?
I am on Z and Y and have half duplex switch set to on. again validated i have a good modbus string leaving and coming to the M21+.
sniffed data
SENT - 01 03 00 05 00 03 15 CA
Returned - 01 03 06 00 00 00 44 00 00 61 60
At this point this if statment is not firing: " if (modbus.isWaitingResponse()) "
----------------------------------------------------------------------------code
#include <ModbusRTUMaster.h>
// Define the ModbusRTUMaster object, using the RS-485 or RS-232 port (depending on availability)
#include <RS485.h>
ModbusRTUMaster modbus(RS485);
uint32_t lastSentTime = 0UL;
const uint32_t baudrate = 9600;
bool Dig1State = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600UL);
Serial.println("Modbus starting...");
// Start the serial port
RS485.begin(baudrate, HALFDUPLEX, SERIAL_8N1);
// Start the modbus master object.
// It is possible to define the port rate (default: 38400)
modbus.begin(9600);
pinMode(Q0_1, OUTPUT);
delay(2000);
Serial.println("Loop start...");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Send a request every 1000ms
if (millis() - lastSentTime > 2000) {
Dig1State = !Dig1State;
digitalWrite(Q0_1, Dig1State);
Serial.println("Modbus poll...");
// Send a Read Coils request to the slave with address 1
// It requests for 23 coils starting at address 10
// IMPORTANT: all read and write functions start a Modbus transmission, but they are not
// blocking, so you can continue the program while the Modbus functions work. To check for
// available responses, call master.available() function often.
modbus.readHoldingRegisters( 1 , 5, 3);
lastSentTime = millis();
}
// Check available responses often
if (modbus.isWaitingResponse()) {
ModbusResponse response = modbus.available();
if (response) {
Serial.println("Modbus responce...");
if (response.hasError()) {
// Response failure treatment. You can use response.getErrorCode()
Serial.print("Modbus Comm Error");
}
else {
// Get the coil value from the response
for (int i = 0; i < 3; ++i) {
Serial.print(i);
Serial.print("- ");
Serial.println(response.getRegister(i));
}
}
}
}
} //loop