using mb_mapping->tabregisters

1,517 views
Skip to first unread message

Hanuma Tej

unread,
Jan 10, 2013, 11:30:11 AM1/10/13
to libm...@googlegroups.com
From the  Modbus client i wan to read  8 holding registers from 400010 , reading them in the server.

This is the code in the Modbus Server to read those values:

       ctx = modbus_new_tcp(SERVER_IP, MODBUS_PORT);
       modbus_set_debug(ctx, TRUE);
       socket = modbus_tcp_listen(ctx, 1);
       modbus_tcp_accept(ctx, &socket);


        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
        int rc;

       mb_mapping = modbus_mapping_new(0x1C+1, 0x2C+1, 0x3C+8, 0x4C+8);

   if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        return -1;
    }

        rc = modbus_receive(ctx, query);
        if (rc != -1) {
            // rc is the query size
     
            modbus_reply(ctx, query, rc, mb_mapping);
 
        } else {
            // Connection closed by the client or error
            modbus_close(ctx);
            modbus_tcp_accept(ctx, &socket);
        }

  
 
     modbus_reply(ctx,query, rc, mb_mapping);

      Reactor_Temp_setpoint = mb_mapping->tab_registers[10];
      Reactor_pressure_setpoint = mb_mapping->tab_registers[11];
      Reactor_level_setpoint = mb_mapping->tab_registers[12];
      Recycle_valve_setpoint = mb_mapping->tab_registers[13];
      Production_setpoint = mb_mapping->tab_registers[14];
      Strip_level_setpoint = mb_mapping->tab_registers[13];

      modbus_free(ctx);

        But i could not read the holding register values .The values read are always 0;

Thanks in advance.



Uğur Onursal

unread,
Feb 7, 2014, 2:14:44 AM2/7/14
to libm...@googlegroups.com
I am also curious about how to draw values from mapping registers to a usual variable.

Julien Blanc

unread,
Feb 7, 2014, 3:45:35 AM2/7/14
to libm...@googlegroups.com
Le 07/02/2014 08:14, Uğur Onursal a écrit :
> I am also curious about how to draw values from mapping registers to a
> usual variable.
>
Hi !

Your questions seems more general programming related than libmodbus
specific.

The modbus_mapping structure is the following :

typedef struct {
int nb_bits;
int nb_input_bits;
int nb_input_registers;
int nb_registers;
uint8_t *tab_bits;
uint8_t *tab_input_bits;
uint16_t *tab_input_registers;
uint16_t *tab_registers;
} modbus_mapping_t;

It is a public struct (imho it should be made opaque, but that’s a
different concern), so you can just directly acces the data using
mb_mapping->tab_registers[register_number] (note that it will fail if
register_number is greater than nb_registers), either for reading or
writing it.

If your concern is to be notified (ie, do a specific treatment) when a
value changes (via master command, for example), there is currently no
way in standard libmodbus to do that. There are, however, some forks
available on github that allows this :

https://github.com/forslund/libmodbus/tree/custom_requests_handlers for
a complete solution
https://github.com/bomm/libmodbus/tree/callback1 for a (maybe) simpler one

But i would recommend that you solve any other issues you have before
looking into these options. You can do active polling (ie, update the
file with modbus_mapping_t content every tenth seconds for example) as a
first time approach.

Regards,

Julien

Uğur Onursal

unread,
Feb 7, 2014, 4:08:36 AM2/7/14
to libm...@googlegroups.com
Thanks for the reply, Julien.

Best Regards

-Uğur
Reply all
Reply to author
Forward
0 new messages