/* * Copyright © 2001-2010 Stéphane Raimbault * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include /* The goal of this program is to check all major functions of libmodbus: - write_coil - read_bits - write_coils - write_register - read_registers - write_registers - read_registers All these functions are called with random values on a address range defined by the following defines. */ #define LOOP 1 #define SERVER_ID 17 #define ADDRESS_START 0 #define ADDRESS_END 99 /* At each loop, the program works in the range ADDRESS_START to * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on. */ int main(void) { modbus_t *ctx; int rc; int nb_fail; int nb_loop; int addr; int nb; uint8_t *tab_rq_bits; uint8_t *tab_rp_bits; uint16_t *tab_rq_registers; uint16_t *tab_rw_rq_registers; uint16_t *tab_rp_registers; /* RTU */ /* ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1); modbus_set_slave(ctx, SERVER_ID); */ /* TCP */ ctx = modbus_new_tcp("127.0.0.1", 1502); modbus_set_debug(ctx, TRUE); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); modbus_free(ctx); return -1; } /* Allocate and initialize the different memory spaces */ nb = ADDRESS_END - ADDRESS_START; tab_rq_bits = (uint8_t *) malloc(nb * sizeof(uint8_t)); memset(tab_rq_bits, 0, nb * sizeof(uint8_t)); tab_rp_bits = (uint8_t *) malloc(nb * sizeof(uint8_t)); memset(tab_rp_bits, 0, nb * sizeof(uint8_t)); tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t)); memset(tab_rq_registers, 0, nb * sizeof(uint16_t)); tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t)); memset(tab_rp_registers, 0, nb * sizeof(uint16_t)); tab_rw_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t)); memset(tab_rw_rq_registers, 0, nb * sizeof(uint16_t)); nb_loop = nb_fail = 0; while (nb_loop++ < LOOP) { for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { int i; /* Random numbers (short) */ for (i=0; i