ModbusRTUMaster
ModbusRTUMaster copied to clipboard
Skip/Reset CRC check
I have a device that uses its uart serial as plaintext as well as modbus. The workflow looks like that
- Signal pulled down
- Device Boots and send plaintext data over the Serial/Modbus Interface
- I send a readHoldingRegisters which is fine
- In Pulseview I can see the Response which looks also fine
- The Arduino Code gets an CRC Error.
I was wondering, if it it is possible to clear the buffer before reading it, so it contains only the Modbus data. I hooked up an Logic Analyzer and in Pulseview the Request and Response looks fine including CRC. A simplified snippet of the Code I'm using:
int yellow = 4;
int orange = 3;
SoftwareSerial mySerial(yellow, orange);
ModbusRTUMaster modbus(mySerial); // no DE_PIN
const uint8_t devId = 170; // ID Battery
const uint8_t holding_regs_read = 10;
#include <SoftwareSerial.h>
#include <ModbusRTUMaster.h>
void setup() {
pinMode(yellow, INPUT);
pinMode(orange, OUTPUT);
pinMode(purple, OUTPUT);
digitalWrite(purple, HIGH);
Serial.begin(9600);
Serial.setTimeout(1000);
mySerial.begin(9600);
while (!Serial) {}
ModbusRTUMaster modbus(mySerial); // no DE_PIN
modbus.begin(9600); // modbus baud rate, config to do
modbus.setTimeout(1000);
uint16_t holdingRegisters[holding_regs_read];
uint8_t error = modbus.readHoldingRegisters(devId, 0, holdingRegisters, holding_regs_read);
if(error != 0){
Serial.println(String(error));
}
}
which returns the Error 6 which should be CRC error
You could run something like this before running modbus.readHoldingRegisters.
while (mySerial.available()) {
mySerial.read();
}
This should clear mySerial's receiving buffer.