From cc0d44e2534bc553e31f566c10c7858b7a7a2142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 4 Jan 2026 21:57:18 +0100 Subject: [PATCH] bess test regs --- src/main.c | 61 +++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/main.c b/src/main.c index 1ce35ba..7e98adb 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,9 @@ #include "modbus.h" +#define PORT 5002 +#define MTU_SIZE 1024 + ModbusException_t startOfAccess(ModbusSlave_t *ms, ModbusFunction_t fcx, uint8_t slave_id) { printf("Start of access: fc%02d, slave %d\n", fcx, slave_id); return 0; @@ -22,45 +25,26 @@ typedef struct { bool value; } coil_slot_t; +// pattern indicating end of the register array +#define ENDSLOT {0xffff, 0} + //@formatter:off static register_slot_t holdings[] = { - {1, 10}, - {2, 20}, - {3, 30}, - {4, 40}, - {5, 50}, - {7, 70}, - {0xffff, 0} + {2, 15}, + ENDSLOT }; static register_slot_t inputs[] = { - {1, 11}, - {2, 21}, - {3, 31}, - {4, 41}, - {5, 51}, - {7, 71}, - {0xffff, 0} + {103, 67}, + ENDSLOT }; static coil_slot_t coils[] = { - {1, 1}, - {2, 0}, - {3, 1}, - {4, 0}, - {5, 1}, - {7, 1}, - {0xffff, 0} + ENDSLOT }; static coil_slot_t discretes[] = { - {1, 0}, - {2, 1}, - {3, 1}, - {4, 1}, - {5, 1}, - {7, 0}, - {0xffff, 0} + ENDSLOT }; //@formatter:on @@ -184,15 +168,26 @@ int msg_received_fce(int fd, const simple_msg_t *msg) { return 0; } +// need a wrapper to adapt the return type to atexit() void atexitcb() { simple_tcp_server_stop(); } -int main() { - // ModbusError_t e = mb_handleRequest(&ms, buf, pldlen, buf2, buflen, &respsize); - +int main(int argc, char *argv[]) { atexit(atexitcb); - printf("Starting TCP server\n"); - simple_tcp_server_start(5002, 1024, msg_received_fce, NULL, NULL); + int port = PORT; // default port + + if (argc > 1) { + // this is unsafe + port = atoi(argv[1]); + } + + if (port <= 0 || port > 65535) { + printf("Invalid port: %d\n", PORT); + return 1; + } + + printf("Starting TCP server on port %d\n", port); + simple_tcp_server_start(port, MTU_SIZE, msg_received_fce, NULL, NULL); printf("Thread created, waiting\n"); while (!simple_tcp_server_ended()) {