Modbus Communication Issue between Arduino OPTA and Mega - CRC Error during Write Operation

Hi!

I'm currently facing a challenge in establishing Modbus communication between an Arduino OPTA (which already has an integrated module) and an Arduino Mega (where I added two separate modules).

For the OPTA, I utilized the libraries #include <ArduinoModbus.h> and #include <ArduinoRS485.h>, configuring it as a ModbusRTUClient, which, to my understanding, acts as the master responsible for requesting and sending values. On the Arduino Mega, as the ArduinoModbus library isn't available, I used <ModbusRtu.h>.

The issue I'm encountering is that while OPTA can successfully read and write, the problem arises when the Arduino Mega attempts to write to a register. During this write operation, OPTA generates a CRC error.

I suspect that the discrepancy in libraries might be causing the problem. Has anyone faced a similar situation or have suggestions on resolving this CRC error during the write operation?

Your insights and assistance would be highly appreciated. Thank you in advance!

Don't quite understand, sounds like 2 masters??
The ModbusRTUClient is a master, requesting and sending info to the servers (slaves), can only be 1 master..
Not sure how you have the ModbusRtu.h but servers (slaves) don't send anything unless client (master) asks for it..
CRC woes are typically bad comms, the CRC is in there to make sure data is proper when received..

good luck.. ~q

In loop of Arduino there’s only the .poll function and the update of the register 0x00.

/Arduino mega sketch//

uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };


void loop{
slave.poll( au16data, 16 );
au16data[0] = 34;
}

Arduino mega doesn’t write nothing on modbus.

OPTA, that is the master, try to read value from Mega and catch a CRC error.

hmm..
maybe try..
CMB27 - ModbusRTUSlave
it's a bit fresher, i've used it before and i see it's changed since i last did..

good luck.. ~q

I've already try that library and OPTA warn me, every time I try to read or write from Modbus that the connection is timed out, so OPTA cannot read or write registers.

Previously you said CRC error, now you are saying timed out? But you also said "The issue I'm encountering is that while OPTA can successfully read and write, the problem arises when the Arduino Mega attempts to write to a register. During this write operation, OPTA generates a CRC error." but the sketch for the Mega you showed does not write to a register (it is a slave).

Unfortunately your description of the problem is too confusing. We need to go back to basics I think.

Please see How to get the best out of this forum and particularly "Hardware" and "Code Problems".

Yes, I made a mistake. OPTA reads and writes to two registers. Arduino Mega periodically reads register 0x8A and then inserts the value into register 0x00. Register 0x8A is written by OPTA, while reading register 0x00 results in a CRC error when using the <ModbusRtu.h> library. However, with CMB27 - ModbusRTUSlave, it results in a timeout error.

You need to post the code you are using.

Do you mean the OPTA is programmed to do that, or that it actually runs without error? Which registers is it reading/writing? How is it doing it? What commands does it send?

If you cannot provide details, we cannot help you.

Sorry for my late feedback.
I'm using OPTA to comunicate with a third part Modbus motor driver, and everything work, The OPTA code for modbus its from https://docs.arduino.cc/tutorials/opta/getting-started-with-modbus-rtu/

For Arduino mega the code it's below:


#include <ModbusRtu.h>

Modbus pump_port;
uint16_t pump_array[0xFF] = {0};

void setup() {
  Serial.begin(115200);
  pump_port = Modbus(1,1,2);
  pump_port.begin(9600);
  Serial.println("Setup finished");
}

void loop() 
{  
  pump_port.poll(pump_array, sizeof(pump_array)/sizeof(pump_array[0]));
  pump_array[0] = pump_array[0x8A];
}