| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #ifndef _MSC_VER |
| 9 | #include <unistd.h> |
| 10 | #endif |
| 11 | #include <errno.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/ |
| 15 | |
| 16 | int maincxx . . $ main(b126dc7c1de90089).(void) |
| 17 | { |
| 18 | int s = -1; |
| 19 | modbus_tcxx . . $ modbus_t# *ctx; |
| 20 | modbus_mapping_tcxx . . $ modbus_mapping_t# *mb_mapping; |
| 21 | |
| 22 | ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).("127.0.0.1", 1502); |
| 23 | /* modbus_set_debug(ctx, TRUE); */ |
| 24 | |
| 25 | mb_mapping = modbus_mapping_newcxx . . $ modbus_mapping_new(4d1e5ca6ca14e01f).(500, 500, 500, 500); |
| 26 | if (mb_mapping == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 27 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Failed to allocate the mapping: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 28 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | s = modbus_tcp_listencxx . . $ modbus_tcp_listen(2a7e0e283938d456).(ctx, 1); |
| 33 | modbus_tcp_acceptcxx . . $ modbus_tcp_accept(e415c7f53a3f0b7).(ctx, &s); |
| 34 | |
| 35 | for (;;) { |
| 36 | uint8_tcxx . . $ uint8_t# query[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!]; |
| 37 | int rc; |
| 38 | |
| 39 | rc = modbus_receivecxx . . $ modbus_receive(744a031d86f39170).(ctx, query); |
| 40 | if (rc > 0) { |
| 41 | /* rc is the query size */ |
| 42 | modbus_replycxx . . $ modbus_reply(2bd5663397286dec).(ctx, query, rc, mb_mapping); |
| 43 | } else if (rc == -1) { |
| 44 | /* Connection closed by the client or error */ |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | printfcxx . . $ printf(581fddd72a8db8fc).("Quit the loop: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 50 | |
| 51 | if (s != -1) { |
| 52 | closecxx . . $ close(7864480464b09eea).(s); |
| 53 | } |
| 54 | modbus_mapping_freecxx . . $ modbus_mapping_free(456c0c7ef81b32ba).(mb_mapping); |
| 55 | modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx); |
| 56 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 57 | |
| 58 | return 0; |
| 59 | } |