Hi all,
I've been working with libmodbus for the last couple of weeks and would like to thank those involved in its creation.
My application is centred around a select() run loop because it has to service data from various different places, not just Modbus. The existing synchronous API works well but can introduce arbitrary delays while network operations happen. Even though the timeout is quite short at half a second, that's still too long to occupy an application which is busy with other things.
I therefore decided to try implementing an asynchronous API to libmodbus, so that instead of simply calling modbus_connect(), modbus_read_registers() and so on, the application code looks like this:
main() {
modbus_set_connected_cb(mb, &connect_callback);
modbus_set_read_cb(mb, &read_callback);
modbus_set_add_watch_cb(mb, &add_watch_callback);
modbus_set_remove_watch_cb(mb, &remove_watch_callback);
modbus_connect_async(mb);
while(1)
runLoop();
}
void connect_callback(modbus_t *mb, int failure) {
if(failure) {
printf("connect_callback(): Connection failed\n");
} else {
printf("connect_callback(): Connection OK\n");
modbus_read_registers_async(mb, 0, 5, tab_reg);
}
}
void read_callback(modbus_t *mb, int failure) {
int n;
if(failure) {
printf("read_callback(): read failed\n");
} else {
printf("read_callback(): read OK\n");
/* Read 5 registers from the address 0 */
for(n=0;n<5;n++) {
printf("Register %d = 0x%04x\n",n,tab_reg[n]);
}
}
}
The runLoop() function and add_watch_callback() and remove_watch_callback() functions are provided by my application code.
The asynchronous API model I've used is based on the one from D-Bus, which I've used successfully before. I have more details of my thought processes in my blog here:
I have a working demo, both server and client.
My questions: is anyone else interested? Could I contribute my modifications to the code base? They're far from fully developed, but they do work well enough to demonstrate and I am fully intending to ship them in an application in the next few months, so they will be tested!
Regards
Chris Jones
Martin-Jones Technology Ltd
Cambridge, UK and Warsaw, Poland