K
@all
As I was struggling again with this I figured it out now finally (at least I hope so).
My issue was the interpretation of whatever value I got.
So for alle future readings:
In the linked Calc-Sheet above you have notes about the registers (read only registers, not bits!).
So for the M103 you have the "Registers - group 1" and "Registers - group 2". Use these. The coils tables are somehow misleading (at least for me).
Additionally be aware modbus_read_registers() need to write the data into an uint16_t variable. With the help of @ntd and his suggestes "union" I was easy to "convert" them into the uint32_t. Unions access the same memory by different variables. See more here.
In the code sniplet above modbus_read_registers() writes the content into the counter.word memory location and the prinft reads them as uint32_t. Easy going now to re-calculate the counter.dword variable to get the values you need.
So to read the current settings and values of all relays and digital inputs I use the following code:
uint16_t state;
union {
uint16_t word[2];
uint32_t dword;
} counter;
/* Relais */
modbus_read_registers(modbusglob,101,1,&state);
printf("Lesen Relay Register: %u\n",state);
printf("\n");
/* DigitalIN 1 */
modbus_read_registers(modbusglob,0,1,&state);
printf("Lesen DigIn1: %u\n",state);
printf("\n");
/* Digital-In 1 Counter */
for (i=8;i<16;i=i+2) {
modbus_read_registers(modbusglob,i,2,counter.word);
printf("Counter %d DigIn1: %u\n",i,counter.dword);
}
printf("\n");
/* DigitalIN 2 */
modbus_read_registers(modbusglob,100,1,&state);
printf("Lesen DigIn1: %d\n",state);
printf("\n");
/* DigitalIN 2 Counter */
for (i=103;i<118;i=i+2) {
modbus_read_registers(modbusglob,i,2,counter.word);
printf("Counter %d DigIn2: %u\n",i,counter.dword);
}
This works now perfectly.
/KNEBB