Hi,
I have been trying to set up a Modbus RTU communication between a C# program (with another Modbus library) as a master and an arduino as as slave. I do so through a USB to RS485 cable. The arduino is connected to a MAX485 IC.
The goal of the project is for the slave to receive a few registers from the master, do some calculations with it and make the outcome available in another register for the master to read again.
The master can flawlessly read data from registers on the slave, I'm however not really grasping how my slave can receive and store bytes from the master.
#include <SimpleModbusSlave.h>
const int serialNumber = 7999;
const int securityCode = 999;
const int HOLDING_REGS_SIZE= 10 ;
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
void setup()
{
modbus_configure(&Serial, 115200, SERIAL_8N1, 3, 2, HOLDING_REGS_SIZE, holdingRegs);
holdingRegs[0] = serialNumber;
holdingRegs[2] = securityCode;
holdingRegs[4] = 0; //I'd like this to become a value sent by the master
}
void loop()
{
modbus_update();
}
As I said, I have no issue reading serialNumber and securityCode when I request this data using function code 3 with this code.
Let's say now I want to send an integer from the master to the slave, let's say decimal 32000 (0x7D 0x00). I'd like this number to be available in holdingRegs[4] and holdingRegs[5]. My masters' library is limited to function code 16 and will always write a minimum of 32 bits (4 bytes).
This is what my masters' library outputs:
0x03: (slave ID)
0x10: (function code 16 - write multiple registers)
0x00:
0x04: (2 bytes to specify I want to start writing at register 4)
0x00:
0x02: (2 bytes to specify there will be data for 2 registers (or more specifically 1 32-bit register)
0x04: 4 bytes to come
0x00:
0x00: first register (16 bits) is 0
0x7D:
0x00: value 32000
0xD8
0xB4: CRC
Hopefully, My understanding is not too wrong yet with this logic.
Here is where it probably goes wrong: I was under the impression that modbus_update(); would update holdingRegs[4] and holdingRegs[5] as soon as the function 16 from the master was received. However, when I read those registers later, they still have the original value 0.
I think I'm missing something very obvious. Any help would be greatly appreciated!
Thanks,
Jens