| 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 | #include <string.h> |
| 14 | |
| 15 | #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/ |
| 16 | |
| 17 | #if defined(_WIN32) |
| 18 | #define close closesocket |
| 19 | #endif |
| 20 | |
| 21 | enumcxx . . $ $anonymous_type_e67ed933052f2521_0# { |
| 22 | TCPcxx . . $ TCP., |
| 23 | RTUcxx . . $ RTU. |
| 24 | }; |
| 25 | |
| 26 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 27 | { |
| 28 | int s = -1; |
| 29 | modbus_tcxx . . $ modbus_t# *ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 30 | modbus_mapping_tcxx . . $ modbus_mapping_t# *mb_mapping = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 31 | int rc; |
| 32 | int use_backend; |
| 33 | |
| 34 | /* TCP */ |
| 35 | if (argc > 1) { |
| 36 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcp") == 0) { |
| 37 | use_backend = TCPcxx . . $ TCP.; |
| 38 | } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "rtu") == 0) { |
| 39 | use_backend = RTUcxx . . $ RTU.; |
| 40 | } else { |
| 41 | printfcxx . . $ printf(581fddd72a8db8fc).("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", |
| 42 | argv[0]); |
| 43 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 44 | } |
| 45 | } else { |
| 46 | /* By default */ |
| 47 | use_backend = TCPcxx . . $ TCP.; |
| 48 | } |
| 49 | |
| 50 | if (use_backend == TCPcxx . . $ TCP.) { |
| 51 | ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).("127.0.0.1", 1502); |
| 52 | s = modbus_tcp_listencxx . . $ modbus_tcp_listen(2a7e0e283938d456).(ctx, 1); |
| 53 | modbus_tcp_acceptcxx . . $ modbus_tcp_accept(e415c7f53a3f0b7).(ctx, &s); |
| 54 | |
| 55 | } else { |
| 56 | ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).("/dev/ttyUSB0", 115200, 'N', 8, 1); |
| 57 | modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, 1); |
| 58 | modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(ctx); |
| 59 | } |
| 60 | |
| 61 | mb_mapping = |
| 62 | modbus_mapping_newcxx . . $ modbus_mapping_new(4d1e5ca6ca14e01f).(MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`!, 0, MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`!, 0); |
| 63 | if (mb_mapping == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 64 | 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`!)); |
| 65 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | for (;;) { |
| 70 | uint8_tcxx . . $ uint8_t# query[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!]; |
| 71 | |
| 72 | rc = modbus_receivecxx . . $ modbus_receive(744a031d86f39170).(ctx, query); |
| 73 | if (rc > 0) { |
| 74 | modbus_replycxx . . $ modbus_reply(2bd5663397286dec).(ctx, query, rc, mb_mapping); |
| 75 | } else if (rc == -1) { |
| 76 | /* Connection closed by the client or error */ |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | printfcxx . . $ printf(581fddd72a8db8fc).("Quit the loop: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 82 | |
| 83 | modbus_mapping_freecxx . . $ modbus_mapping_free(456c0c7ef81b32ba).(mb_mapping); |
| 84 | if (s != -1) { |
| 85 | closecxx . . $ close(7864480464b09eea).(s); |
| 86 | } |
| 87 | /* For RTU, skipped by TCP (no TCP connect) */ |
| 88 | modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx); |
| 89 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 90 | |
| 91 | return 0; |
| 92 | } |