Commit 1faf5c3aad99c7f7e88deeb26b16d8f46261cfda
1 parent
5fd7ec5c
New setter/getter for context socket (fixes bandwidth-server-many-up)
These new functions seem clearer than the recent modbus_receive_from function IHMO. This change fixes the bandwidth-server-many-up program.
Showing
10 changed files
with
115 additions
and
27 deletions
doc/Makefile.am
| @@ -4,6 +4,7 @@ MAN3 = \ | @@ -4,6 +4,7 @@ MAN3 = \ | ||
| 4 | modbus_flush.3 \ | 4 | modbus_flush.3 \ |
| 5 | modbus_free.3 \ | 5 | modbus_free.3 \ |
| 6 | modbus_get_header_length.3 \ | 6 | modbus_get_header_length.3 \ |
| 7 | + modbus_get_socket.3 \ | ||
| 7 | modbus_get_timeout_begin.3 \ | 8 | modbus_get_timeout_begin.3 \ |
| 8 | modbus_get_timeout_end.3 \ | 9 | modbus_get_timeout_end.3 \ |
| 9 | modbus_new_rtu.3 \ | 10 | modbus_new_rtu.3 \ |
| @@ -14,12 +15,12 @@ MAN3 = \ | @@ -14,12 +15,12 @@ MAN3 = \ | ||
| 14 | modbus_read_input_registers.3 \ | 15 | modbus_read_input_registers.3 \ |
| 15 | modbus_read_registers.3 \ | 16 | modbus_read_registers.3 \ |
| 16 | modbus_receive.3 \ | 17 | modbus_receive.3 \ |
| 17 | - modbus_receive_from.3 \ | ||
| 18 | modbus_receive_confirmation.3 \ | 18 | modbus_receive_confirmation.3 \ |
| 19 | modbus_send_raw_request.3 \ | 19 | modbus_send_raw_request.3 \ |
| 20 | modbus_set_debug.3 \ | 20 | modbus_set_debug.3 \ |
| 21 | modbus_set_error_recovery.3 \ | 21 | modbus_set_error_recovery.3 \ |
| 22 | modbus_set_slave.3 \ | 22 | modbus_set_slave.3 \ |
| 23 | + modbus_set_socket.3 \ | ||
| 23 | modbus_set_timeout_begin.3 \ | 24 | modbus_set_timeout_begin.3 \ |
| 24 | modbus_set_timeout_end.3 \ | 25 | modbus_set_timeout_end.3 \ |
| 25 | modbus_strerror.3 \ | 26 | modbus_strerror.3 \ |
doc/libmodbus.txt
| @@ -102,6 +102,8 @@ Context setters and getters:: | @@ -102,6 +102,8 @@ Context setters and getters:: | ||
| 102 | linkmb:modbus_set_debug[3] | 102 | linkmb:modbus_set_debug[3] |
| 103 | linkmb:modbus_set_error_recovery[3] | 103 | linkmb:modbus_set_error_recovery[3] |
| 104 | linkmb:modbus_set_slave[3] | 104 | linkmb:modbus_set_slave[3] |
| 105 | + linkmb:modbus_set_socket[3] | ||
| 106 | + linkmb:modbus_get_socket[3] | ||
| 105 | linkmb:modbus_get_timeout_begin[3] | 107 | linkmb:modbus_get_timeout_begin[3] |
| 106 | linkmb:modbus_set_timeout_begin[3] | 108 | linkmb:modbus_set_timeout_begin[3] |
| 107 | linkmb:modbus_get_timeout_end[3] | 109 | linkmb:modbus_get_timeout_end[3] |
| @@ -159,7 +161,6 @@ receive and reply: | @@ -159,7 +161,6 @@ receive and reply: | ||
| 159 | 161 | ||
| 160 | Receive:: | 162 | Receive:: |
| 161 | linkmb:modbus_receive[3] | 163 | linkmb:modbus_receive[3] |
| 162 | - linkmb:modbus_receive_from[3] | ||
| 163 | 164 | ||
| 164 | Reply:: | 165 | Reply:: |
| 165 | linkmb:modbus_reply[3] | 166 | linkmb:modbus_reply[3] |
doc/modbus_get_socket.txt
0 → 100644
| 1 | +modbus_get_socket(3) | ||
| 2 | +==================== | ||
| 3 | + | ||
| 4 | + | ||
| 5 | +NAME | ||
| 6 | +---- | ||
| 7 | +modbus_get_socket - get the current socket of the context | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +SYNOPSIS | ||
| 11 | +-------- | ||
| 12 | +*int modbus_get_socket(modbus_t *'ctx')* | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +DESCRIPTION | ||
| 16 | +----------- | ||
| 17 | +The _modbus_get_socket()_ function shall return the current socket or file | ||
| 18 | +descriptor of the libmodbus context. | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +RETURN VALUE | ||
| 22 | +------------ | ||
| 23 | +The current socket or file descriptor of the context. | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +SEE ALSO | ||
| 27 | +-------- | ||
| 28 | +linkmb:modbus_set_socket[3] | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +AUTHORS | ||
| 32 | +------- | ||
| 33 | +The libmodbus documentation was written by Stéphane Raimbault | ||
| 34 | +<stephane.raimbault@gmail.com> |
doc/modbus_receive.txt
| @@ -18,8 +18,8 @@ The _modbus_receive()_ function shall receive an indication request from the | @@ -18,8 +18,8 @@ The _modbus_receive()_ function shall receive an indication request from the | ||
| 18 | socket of the context 'ctx'. This function is used by Modbus slave/server to | 18 | socket of the context 'ctx'. This function is used by Modbus slave/server to |
| 19 | receive and analyze indication request sent by the masters/clients. | 19 | receive and analyze indication request sent by the masters/clients. |
| 20 | 20 | ||
| 21 | -If you need to use another socket than the one defined in the context 'ctx', see | ||
| 22 | -the function linkmb:modbus_receive_from[3]. | 21 | +If you need to use another socket or file descriptor than the one defined in the |
| 22 | +context 'ctx', see the function linkmb:modbus_set_socket[3]. | ||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | RETURN VALUE | 25 | RETURN VALUE |
| @@ -31,7 +31,8 @@ errno. | @@ -31,7 +31,8 @@ errno. | ||
| 31 | 31 | ||
| 32 | SEE ALSO | 32 | SEE ALSO |
| 33 | -------- | 33 | -------- |
| 34 | -linkmb:modbus_receive_from[3] | 34 | +linkmb:modbus_set_socket[3] |
| 35 | +linkmb:modbus_reply[3] | ||
| 35 | 36 | ||
| 36 | 37 | ||
| 37 | AUTHORS | 38 | AUTHORS |
doc/modbus_set_socket.txt
0 → 100644
| 1 | +modbus_set_socket(3) | ||
| 2 | +==================== | ||
| 3 | + | ||
| 4 | + | ||
| 5 | +NAME | ||
| 6 | +---- | ||
| 7 | +modbus_set_socket - set socket of the context | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +SYNOPSIS | ||
| 11 | +-------- | ||
| 12 | +*void modbus_set_socket(modbus_t *'ctx', int 'socket')* | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +DESCRIPTION | ||
| 16 | +----------- | ||
| 17 | +The _modbus_set_socket()_ function shall set the socket or file descriptor in | ||
| 18 | +the libmodbus context. This function is useful for managing multiple client | ||
| 19 | +connections to the same server. | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +RETURN VALUE | ||
| 23 | +------------ | ||
| 24 | +There is no return values. | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +EXAMPLE | ||
| 28 | +------- | ||
| 29 | +[source,c] | ||
| 30 | +------------------- | ||
| 31 | +ctx = modbus_new_tcp("127.0.0.1", 1502); | ||
| 32 | +server_socket = modbus_tcp_listen(ctx, NB_CONNECTION); | ||
| 33 | + | ||
| 34 | +FD_ZERO(&rdset); | ||
| 35 | +FD_SET(server_socket, &rdset); | ||
| 36 | + | ||
| 37 | +/* .... */ | ||
| 38 | + | ||
| 39 | +if (FD_ISSET(master_socket, &rdset)) { | ||
| 40 | + modbus_set_socket(ctx, master_socket); | ||
| 41 | + rc = modbus_receive(ctx, query); | ||
| 42 | + if (rc != -1) { | ||
| 43 | + modbus_reply(ctx, query, rc, mb_mapping); | ||
| 44 | + } | ||
| 45 | +} | ||
| 46 | +------------------- | ||
| 47 | + | ||
| 48 | +SEE ALSO | ||
| 49 | +-------- | ||
| 50 | +linkmb:modbus_get_socket[3] | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +AUTHORS | ||
| 54 | +------- | ||
| 55 | +The libmodbus documentation was written by Stéphane Raimbault | ||
| 56 | +<stephane.raimbault@gmail.com> |
doc/modbus_write_registers.txt
| @@ -23,8 +23,8 @@ The function uses the Modbus function code 0x10 (preset multiple registers). | @@ -23,8 +23,8 @@ The function uses the Modbus function code 0x10 (preset multiple registers). | ||
| 23 | 23 | ||
| 24 | RETURN VALUE | 24 | RETURN VALUE |
| 25 | ------------ | 25 | ------------ |
| 26 | -The _modbus_write_registers()_ function shall return the number of written registers | ||
| 27 | -if successful. Otherwise it shall return -1 and set errno. | 26 | +The _modbus_write_registers()_ function shall return the number of written |
| 27 | +registers if successful. Otherwise it shall return -1 and set errno. | ||
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | SEE ALSO | 30 | SEE ALSO |
src/modbus.c
| @@ -417,23 +417,6 @@ int modbus_receive(modbus_t *ctx, uint8_t *req) | @@ -417,23 +417,6 @@ int modbus_receive(modbus_t *ctx, uint8_t *req) | ||
| 417 | return receive_msg(ctx, req, MSG_INDICATION); | 417 | return receive_msg(ctx, req, MSG_INDICATION); |
| 418 | } | 418 | } |
| 419 | 419 | ||
| 420 | -/* Requires a socket file descriptor with a connection etablished in | ||
| 421 | - argument */ | ||
| 422 | -int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req) | ||
| 423 | -{ | ||
| 424 | - int rc; | ||
| 425 | - const int s_old = ctx->s; | ||
| 426 | - | ||
| 427 | - ctx->s = sockfd; | ||
| 428 | - | ||
| 429 | - rc = receive_msg(ctx, req, MSG_INDICATION); | ||
| 430 | - | ||
| 431 | - /* Restore orignal socket */ | ||
| 432 | - ctx->s = s_old; | ||
| 433 | - | ||
| 434 | - return rc; | ||
| 435 | -} | ||
| 436 | - | ||
| 437 | /* Receives the confirmation. | 420 | /* Receives the confirmation. |
| 438 | 421 | ||
| 439 | The function shall store the read response in rsp and return the number of | 422 | The function shall store the read response in rsp and return the number of |
| @@ -1353,6 +1336,16 @@ int modbus_set_error_recovery(modbus_t *ctx, int enabled) | @@ -1353,6 +1336,16 @@ int modbus_set_error_recovery(modbus_t *ctx, int enabled) | ||
| 1353 | return 0; | 1336 | return 0; |
| 1354 | } | 1337 | } |
| 1355 | 1338 | ||
| 1339 | +void modbus_set_socket(modbus_t *ctx, int socket) | ||
| 1340 | +{ | ||
| 1341 | + ctx->s = socket; | ||
| 1342 | +} | ||
| 1343 | + | ||
| 1344 | +int modbus_get_socket(modbus_t *ctx) | ||
| 1345 | +{ | ||
| 1346 | + return ctx->s; | ||
| 1347 | +} | ||
| 1348 | + | ||
| 1356 | /* Get the timeout of begin of message */ | 1349 | /* Get the timeout of begin of message */ |
| 1357 | void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout) | 1350 | void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout) |
| 1358 | { | 1351 | { |
src/modbus.h
| @@ -135,8 +135,9 @@ typedef struct { | @@ -135,8 +135,9 @@ typedef struct { | ||
| 135 | } modbus_mapping_t; | 135 | } modbus_mapping_t; |
| 136 | 136 | ||
| 137 | int modbus_set_slave(modbus_t* ctx, int slave); | 137 | int modbus_set_slave(modbus_t* ctx, int slave); |
| 138 | - | ||
| 139 | int modbus_set_error_recovery(modbus_t *ctx, int enabled); | 138 | int modbus_set_error_recovery(modbus_t *ctx, int enabled); |
| 139 | +void modbus_set_socket(modbus_t *ctx, int socket); | ||
| 140 | +int modbus_get_socket(modbus_t *ctx); | ||
| 140 | 141 | ||
| 141 | void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout); | 142 | void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout); |
| 142 | void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout); | 143 | void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout); |
tests/bandwidth-client.c
| @@ -167,7 +167,7 @@ int main(int argc, char *argv[]) | @@ -167,7 +167,7 @@ int main(int argc, char *argv[]) | ||
| 167 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 167 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 168 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 168 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 169 | printf("* %d KiB/s\n", rate); | 169 | printf("* %d KiB/s\n", rate); |
| 170 | - printf("\n"); | 170 | + printf("\n\n"); |
| 171 | 171 | ||
| 172 | printf("READ AND WRITE REGISTERS\n\n"); | 172 | printf("READ AND WRITE REGISTERS\n\n"); |
| 173 | 173 |
tests/bandwidth-server-many-up.c
| @@ -118,7 +118,8 @@ int main(void) | @@ -118,7 +118,8 @@ int main(void) | ||
| 118 | /* An already connected master has sent a new query */ | 118 | /* An already connected master has sent a new query */ |
| 119 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 119 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 120 | 120 | ||
| 121 | - rc = modbus_receive_from(ctx, master_socket, query); | 121 | + modbus_set_socket(ctx, master_socket); |
| 122 | + rc = modbus_receive(ctx, query); | ||
| 122 | if (rc != -1) { | 123 | if (rc != -1) { |
| 123 | modbus_reply(ctx, query, rc, mb_mapping); | 124 | modbus_reply(ctx, query, rc, mb_mapping); |
| 124 | } else { | 125 | } else { |