Remove constraints on baud rate values
[libmodbus.git] / tests / random-test-server.c
bloba064fa8a3323c4606dcfb92ec70768bb0f423ebb
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <stdio.h>
8 #ifndef _MSC_VER
9 #include <unistd.h>
10 #endif
11 #include <errno.h>
12 #include <stdlib.h>
14 #include <modbus.h>
16 int main(void)
18 int s = -1;
19 modbus_t *ctx;
20 modbus_mapping_t *mb_mapping;
22 ctx = modbus_new_tcp("127.0.0.1", 1502);
23 /* modbus_set_debug(ctx, TRUE); */
25 mb_mapping = modbus_mapping_new(500, 500, 500, 500);
26 if (mb_mapping == NULL) {
27 fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
28 modbus_free(ctx);
29 return -1;
32 s = modbus_tcp_listen(ctx, 1);
33 modbus_tcp_accept(ctx, &s);
35 for (;;) {
36 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
37 int rc;
39 rc = modbus_receive(ctx, query);
40 if (rc > 0) {
41 /* rc is the query size */
42 modbus_reply(ctx, query, rc, mb_mapping);
43 } else if (rc == -1) {
44 /* Connection closed by the client or error */
45 break;
49 printf("Quit the loop: %s\n", modbus_strerror(errno));
51 if (s != -1) {
52 close(s);
54 modbus_mapping_free(mb_mapping);
55 modbus_close(ctx);
56 modbus_free(ctx);
58 return 0;