CMB27 / ModbusRTUSlave

This is an Arduino library that implements the slave/server logic of the Modbus RTU protocol.
MIT License
71 stars 21 forks source link

QModBus Invalid CRC #34

Closed proasnet closed 6 months ago

proasnet commented 7 months ago

Hi, I am trying found a ideal RTU modbus library. Now, I am trying an example, on Arduino Mega 2560 ( Later on ESP32 ). As a master is a PC with QModBus. Between PC and Ardino is a converter with Tx En. On any ask from a PC, I get some data, but always with CRC Error.


#include <ModbusRTUSlave.h>

const byte buttonPins[2] = {2, 3};
const byte ledPins[4] = {5, 6, 7, 8};

//HardwareSerial RS485(1);
ModbusRTUSlave modbus( Serial1 , 13 ); // serial port, driver enable pin for rs-485

bool coils[2];
bool discreteInputs[2];
uint16_t holdingRegisters[2];
uint16_t inputRegisters[2];

void setup() {

  Serial1.begin( 9600 );

  pinMode(buttonPins[0], INPUT_PULLUP);
  pinMode(buttonPins[1], INPUT_PULLUP);
  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
  pinMode(ledPins[2], OUTPUT);
  pinMode(ledPins[3], OUTPUT);

  modbus.configureCoils(coils, 2);                       // bool array of coil values, number of coils
  modbus.configureDiscreteInputs(discreteInputs, 2);     // bool array of discrete input values, number of discrete inputs
  modbus.configureHoldingRegisters(holdingRegisters, 2); // unsigned 16 bit integer array of holding register values, number of holding registers
  modbus.configureInputRegisters(inputRegisters, 2);     // unsigned 16 bit integer array of input register values, number of input registers

  modbus.begin( 1 , 9600);
}

void loop() {
  inputRegisters[0] = 500;
  inputRegisters[1] = 550;
  discreteInputs[0] = !digitalRead(buttonPins[0]);
  discreteInputs[1] = !digitalRead(buttonPins[1]);

  modbus.poll();

  analogWrite(ledPins[0], holdingRegisters[0]);
  analogWrite(ledPins[1], holdingRegisters[1]);
  digitalWrite(ledPins[2], coils[0]);
  digitalWrite(ledPins[3], coils[1]);
}

crc_err

CMB27 commented 7 months ago

It looks like something is going wrong with the data being received or how it is being received by your computer.

It looks like you are sending the following request. 01 - Slave/Server ID 01 - Function Code 1 (Read Coils) 00 00 - Starting Data/Coil Address 00 01 - Number of Coils to Read FD CA - CRC Code (I'm pretty sure this is what QModBus is sending, otherwise you probably wouldn't get a response.)

The appropriate response is the following. 01 - Slave/Server ID 01 - Function Code 1 (Read Coils) 01 - Coil Data Byte Count 00 - Coil Data 51 88 - CRC Code

If you look at the raw data you are receiving, you are getting this message, there is just other data around it.

proasnet commented 7 months ago

In a few days I will test again, the new HW revision will be ready, so I will send the data from the modbus test