Addressing problem

Asked by JM

Hi,
  I am trying to read coils status from a slave PLC. I have used parts of the program random-test-client.c to write a small program for testing. Here is the program:

#include"modbus/modbus.h"

int main(void)
{
modbus_t *ctx;

int rc;
int nb_fail;
int nb_loop;
long int addr;
int nb;
uint8_t *tab_rq_bits;
uint8_t *tab_rp_bits;
uint16_t *tab_rq_registers;
uint16_t *tab_rw_rq_registers;
uint16_t *tab_rp_registers;

nb = 4;
addr = 30006;

ctx = modbus_new_rtu("/dev/ttyS2",9600,'N',8,1);
modbus_set_slave(ctx, 1);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1;
}

if (modbus_connect(ctx) == -1) {
printf("Connexion failed: %s\n",stderr);
modbus_free(ctx);
return -1;
}

tab_rq_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
memset(tab_rq_bits, 0, nb * sizeof(uint8_t));

rc = modbus_read_bits(ctx,addr, nb, tab_rq_bits);
if (rc != nb)
printf("ERROR modbus_read_bits\n");
printf("Address = %d, nb = %d\n", addr, nb);

int i;
for(i=0;i<4;i++)
printf("\n %d \n",tab_rq_bits[i]);

free(tab_rq_bits);

modbus_close(ctx);
modbus_free(ctx);

}

The output is:

#mymodbuscomm
ERROR modbus_read_bits
Address = 30006, nb = 4
0
0
0
0

I think this is an addressing problem. Any help/suggestions on this one would be appreciated.
                                                                                                                                                          Best Regards,
                                                                                                                                                         JM

Question information

Language:
English Edit question
Status:
Answered
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Karthik.K.M. (karthikkm1987) said :
#1

Try modbus_mapping_new() first.to map which regesters you are going to use and put into regesters the bits.
that is why you are getting 0 0 0........

Can you help with this problem?

Provide an answer of your own, or ask JM for more information if necessary.

To post a message you must log in.