123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <modbus.h>
- int main(void)
- {
- int socket;
- modbus_t *ctx;
- modbus_mapping_t *mb_mapping;
- ctx = modbus_new_tcp("127.0.0.1", 1502);
-
- mb_mapping = modbus_mapping_new(500, 500, 500, 500);
- if (mb_mapping == NULL) {
- fprintf(stderr, "Failed to allocate the mapping: %s\n",
- modbus_strerror(errno));
- modbus_free(ctx);
- return -1;
- }
- socket = modbus_listen(ctx, 1);
- modbus_accept(ctx, &socket);
- for (;;) {
- uint8_t query[MODBUS_MAX_ADU_LENGTH_TCP];
- int rc;
- rc = modbus_receive(ctx, -1, query);
- if (rc != -1) {
-
- modbus_reply(ctx, query, rc, mb_mapping);
- } else {
-
- break;
- }
- }
- printf("Quit the loop: %s\n", modbus_strerror(errno));
- modbus_mapping_free(mb_mapping);
- modbus_close(ctx);
- modbus_free(ctx);
- return 0;
- }
|