emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.
Other
519 stars 186 forks source link

Checksum error, or CRC error #339

Closed proasnet closed 4 months ago

proasnet commented 7 months ago

I am trying the first model for Modbus RTU to my project. ESP32 Devkit V1 and MAX485 . Hardware connection working, with another library, but I like to have one library for TCP and RTU. Your TCP library working fine. As Master (client) I am trying QModRTU, QMod Master and Modbus Poll and in theese all SW, I get error with Checksum or CRC and not working. Please, can you check my model code where is a problem? Its builded from examples. Thank you.

Code is


#include <HardwareSerial.h>
//#include <SoftwareSerial.h>
#include <ModbusRTU.h>

#define TXenableRS485 21
#define M_RX 34
#define M_TX 32
#define MODBUSRTU_REDE

HardwareSerial RS485(1);
//EspSoftwareSerial::UART RS485;
ModbusRTU mb;

void setup() {
  Serial.begin( 115200 );

  pinMode( TXenableRS485 , OUTPUT );
  digitalWrite( TXenableRS485 , LOW );

  RS485.begin( 9600, SERIAL_8N1 , M_RX , M_TX );

  //RS485.begin( 9600 , SWSERIAL_8N1 , M_RX , M_TX , false );

  mb.begin( &RS485 , TXenableRS485 );
  mb.setBaudrate( 9600 );
  mb.slave( 1 );

  // RTU SERVER
  mb.addIreg( 0 ); // analog input
  mb.addIreg( 1 ); // analog input
  mb.addIreg( 2 ); // analog input
  mb.addIreg( 3 ); // analog input
  mb.addIreg( 4 ); // analog input

  // RTU SERVER
  mb.addIsts( 0 ); // digital input
  mb.addIsts( 1 ); // digital input
  mb.addIsts( 2 ); // digital input
  mb.addIsts( 3 ); // digital input
  mb.addIsts( 4 ); // digital input

  // RTU SERVER
  mb.addHreg( 0 ); // holding
  mb.addHreg( 1 ); // holding
  mb.addHreg( 2 ); // holding
  mb.addHreg( 3 ); // holding
  mb.addHreg( 4 ); // holding

  // RTU SERVER
  mb.addCoil( 0 ); // coil
  mb.addCoil( 1 ); // coil
  mb.addCoil( 2 ); // coil
  mb.addCoil( 3 ); // coil
  mb.addCoil( 4 ); // coil
}

void loop() {
  mb.task();                // Server Modbus TCP queries

  mb.Ireg( 0 , 400 ); // analog in
  mb.Ireg( 1 , 401 ); // analog in
  mb.Ireg( 2 , 402 ); // analog in
  mb.Ireg( 3 , 403 ); // analog in
  mb.Ireg( 4 , 404 ); // analog in

  mb.Ists( 0 , true ); // digital input
  mb.Ists( 1 , false ); // digital input
  mb.Ists( 2 , true ); // digital input
  mb.Ists( 3 , false ); // digital input
  mb.Ists( 4 , true ); // digital input

  mb.Hreg( 0 , 500 ); // holding
  mb.Hreg( 1 , 501 ); // holding
  mb.Hreg( 2 , 502 ); // holding
  mb.Hreg( 3 , 503 ); // holding
  mb.Hreg( 4 , 504 ); // holding

  Serial.println ( "coil 0: " + String( mb.Coil( 0 )) + " coil 2: " + String( mb.Coil( 1 )) + "coil 2: " + String( mb.Coil( 2 )) + " coil 3: " + String( mb.Coil( 3 )));

  yield();
}
emelianov commented 5 months ago

I suspect it was some wiring or line speed related issue

proasnet commented 5 months ago

Its possible as you write. I had as master PC with low price usb rs485 cable. Tomorow, I will try two tests of my slave (server). One with master simatic, the second master esp8266, so, on master side is better use converter without re+de pin? And on slave with re+de converter? I thing that library control re+de without own code controll.

Dne čt 25. 4. 2024 19:14 uživatel Alexander Emelianov < @.***> napsal:

I suspect it was some wiring or line speed related issue

— Reply to this email directly, view it on GitHub https://github.com/emelianov/modbus-esp8266/issues/339#issuecomment-2077777290, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASIIEITSCYOHUNEACKS7S6TY7E2WFAVCNFSM6AAAAABDPSHFCCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZXG43TOMRZGA . You are receiving this because you authored the thread.Message ID: @.***>

emelianov commented 5 months ago

I was testing the library with cheap transceivers with no problem up to 1Mbit speed. Auto RX-TX (with no re-de) one was working only at 9600.