ModbusRTUMaster icon indicating copy to clipboard operation
ModbusRTUMaster copied to clipboard

Skip/Reset CRC check

Open marcohald opened this issue 8 months ago • 1 comments

I have a device that uses its uart serial as plaintext as well as modbus. The workflow looks like that

  1. Signal pulled down
  2. Device Boots and send plaintext data over the Serial/Modbus Interface
  3. I send a readHoldingRegisters which is fine
  4. In Pulseview I can see the Response which looks also fine
  5. The Arduino Code gets an CRC Error.

crc-error.srzip.zip

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

Image

Image

marcohald avatar Apr 10 '25 07:04 marcohald

You could run something like this before running modbus.readHoldingRegisters.

while (mySerial.available()) {
  mySerial.read();
}

This should clear mySerial's receiving buffer.

CMB27 avatar Apr 11 '25 02:04 CMB27