123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /*
- * Copyright © 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <modbus/modbus.h>
- #include "unit-test.h"
- /* Tests based on PI-MBUS-300 documentation */
- #define SLAVE 0x11
- int main(void)
- {
- uint8_t *tab_rp_status;
- uint16_t *tab_rp_registers;
- modbus_param_t mb_param;
- int i;
- uint8_t value;
- int address;
- int nb_points;
- int ret;
- /* RTU parity : none, even, odd */
- /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
- /* TCP */
- modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
- /* modbus_set_debug(&mb_param, TRUE);*/
-
- if (modbus_connect(&mb_param) == -1) {
- printf("ERROR Connection failed\n");
- exit(1);
- }
- /* Allocate and initialize the memory to store the status */
- nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
- UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
- tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
- memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
-
- /* Allocate and initialize the memory to store the registers */
- nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
- UT_INPUT_REGISTERS_NB_POINTS) ?
- UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
- tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
- memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
- printf("** UNIT TESTING **\n");
- printf("TEST READ FUNCTIONS:\n");
- /** COIL STATUS **/
- ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
- UT_COIL_STATUS_NB_POINTS, tab_rp_status);
- printf("* coil status: ");
- if (ret != UT_COIL_STATUS_NB_POINTS) {
- printf("FAILED (nb points %d)\n", ret);
- goto close;
- }
- i = 0;
- address = UT_COIL_STATUS_ADDRESS;
- nb_points = UT_COIL_STATUS_NB_POINTS;
- while (nb_points > 0) {
- int nb_bits = (nb_points > 8) ? 8 : nb_points;
- value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
- if (value != UT_COIL_STATUS_TAB[i]) {
- printf("FAILED (%0X != %0X)\n",
- value, UT_COIL_STATUS_TAB[i]);
- goto close;
- }
- nb_points -= nb_bits;
- i++;
- }
- printf("OK\n");
- /** INPUT STATUS **/
- ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
- UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
- printf("* read_input_status: ");
- if (ret != UT_INPUT_STATUS_NB_POINTS) {
- printf("FAILED (nb points %d)\n", ret);
- goto close;
- }
- i = 0;
- address = UT_INPUT_STATUS_ADDRESS;
- nb_points = UT_INPUT_STATUS_NB_POINTS;
- while (nb_points > 0) {
- int nb_bits = (nb_points > 8) ? 8 : nb_points;
- value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
- if (value != UT_INPUT_STATUS_TAB[i]) {
- printf("FAILED (%0X != %0X)\n",
- value, UT_INPUT_STATUS_TAB[i]);
- goto close;
- }
- nb_points -= nb_bits;
- i++;
- }
- printf("OK\n");
- /** HOLDING REGISTERS **/
- ret = read_holding_registers(&mb_param,
- SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
- UT_HOLDING_REGISTERS_NB_POINTS,
- tab_rp_registers);
- printf("* read_holding_registers: ");
- if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
- printf("FAILED (nb points %d)\n", ret);
- goto close;
- }
- for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
- if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
- printf("FAILED (%0X != %0X)\n",
- tab_rp_registers[i],
- UT_HOLDING_REGISTERS_TAB[i]);
- goto close;
- }
- }
- printf("OK\n");
- /** INPUT REGISTERS **/
- ret = read_input_registers(&mb_param,
- SLAVE, UT_INPUT_REGISTERS_ADDRESS,
- UT_INPUT_REGISTERS_NB_POINTS,
- tab_rp_registers);
- printf("* read_input_registers: ");
- if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
- printf("FAILED (nb points %d)\n", ret);
- goto close;
- }
-
- for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
- if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
- printf("FAILED (%0X != %0X)\n",
- tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
- goto close;
- }
- }
- printf("OK\n");
- /** WRITE FUNCTIONS **/
- printf("\nTEST WRITE FUNCTIONS:\n");
-
- ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON);
- printf("* force_single_coil: ");
- if (ret == 1) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- goto close;
- }
- ret = preset_single_register(&mb_param, SLAVE, 0x01, 0x03);
- printf("* preset_single_register: ");
- if (ret == 1) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- goto close;
- }
- {
- int nb_points = 0x0A;
- uint8_t tab_value[] = { ON, OFF, ON, ON,
- OFF, OFF, ON, ON,
- ON, OFF };
- ret = force_multiple_coils(&mb_param, SLAVE, 0x13,
- nb_points, tab_value);
- printf("* force_multiple_coils: ");
- if (ret == nb_points) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- goto close;
- }
- }
- {
- int nb_points = 2;
- uint16_t tab_value[] = { 0x000A, 0x0102 };
- ret = preset_multiple_registers(&mb_param, SLAVE,
- 0x01, nb_points, tab_value);
- printf("* preset_multiple_registers: ");
- if (ret == nb_points) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- goto close;
- }
- }
- /** ILLEGAL DATA ADDRESS */
- printf("\nTEST ILLEGAL DATA ADDRESS:\n");
- /* The mapping begins at 0 and ending at address + nb_points so
- * the addresses below are not valid. */
-
- ret = read_coil_status(&mb_param, SLAVE,
- UT_COIL_STATUS_ADDRESS,
- UT_COIL_STATUS_NB_POINTS + 1,
- tab_rp_status);
- printf("* read_coil_status: ");
- if (ret == ILLEGAL_DATA_ADDRESS)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = read_input_status(&mb_param, SLAVE,
- UT_INPUT_STATUS_ADDRESS,
- UT_INPUT_STATUS_NB_POINTS + 1,
- tab_rp_status);
- printf("* read_input_status: ");
- if (ret == ILLEGAL_DATA_ADDRESS)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = read_holding_registers(&mb_param, SLAVE,
- UT_HOLDING_REGISTERS_ADDRESS,
- UT_HOLDING_REGISTERS_NB_POINTS + 1,
- tab_rp_registers);
- printf("* read_holding_registers: ");
- if (ret == ILLEGAL_DATA_ADDRESS)
- printf("OK\n");
- else
- printf("FAILED\n");
-
- ret = read_input_registers(&mb_param, SLAVE,
- UT_INPUT_REGISTERS_ADDRESS,
- UT_INPUT_REGISTERS_NB_POINTS + 1,
- tab_rp_registers);
- printf("* read_input_registers: ");
- if (ret == ILLEGAL_DATA_ADDRESS)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = force_single_coil(&mb_param, SLAVE,
- UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
- ON);
- printf("* force_single_coil: ");
- if (ret == ILLEGAL_DATA_ADDRESS) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- }
- ret = force_multiple_coils(&mb_param, SLAVE,
- UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
- UT_COIL_STATUS_NB_POINTS,
- tab_rp_status);
- printf("* force_multiple_coils: ");
- if (ret == ILLEGAL_DATA_ADDRESS) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- }
- ret = preset_multiple_registers(&mb_param, SLAVE,
- UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
- UT_HOLDING_REGISTERS_NB_POINTS,
- tab_rp_registers);
- printf("* preset_multiple_registers: ");
- if (ret == ILLEGAL_DATA_ADDRESS) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- }
- /** TOO MANY DATA */
- printf("\nTEST TOO MANY DATA ERROR:\n");
- ret = read_coil_status(&mb_param, SLAVE,
- UT_COIL_STATUS_ADDRESS,
- MAX_STATUS + 1,
- tab_rp_status);
- printf("* read_coil_status: ");
- if (ret == TOO_MANY_DATA)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = read_input_status(&mb_param, SLAVE,
- UT_INPUT_STATUS_ADDRESS,
- MAX_STATUS + 1,
- tab_rp_status);
- printf("* read_input_status: ");
- if (ret == TOO_MANY_DATA)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = read_holding_registers(&mb_param, SLAVE,
- UT_HOLDING_REGISTERS_ADDRESS,
- MAX_REGISTERS + 1,
- tab_rp_registers);
- printf("* read_holding_registers: ");
- if (ret == TOO_MANY_DATA)
- printf("OK\n");
- else
- printf("FAILED\n");
-
- ret = read_input_registers(&mb_param, SLAVE,
- UT_INPUT_REGISTERS_ADDRESS,
- MAX_REGISTERS + 1,
- tab_rp_registers);
- printf("* read_input_registers: ");
- if (ret == TOO_MANY_DATA)
- printf("OK\n");
- else
- printf("FAILED\n");
- ret = force_multiple_coils(&mb_param, SLAVE,
- UT_COIL_STATUS_ADDRESS,
- MAX_STATUS + 1,
- tab_rp_status);
- printf("* force_multiple_coils: ");
- if (ret == TOO_MANY_DATA) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- }
- ret = preset_multiple_registers(&mb_param, SLAVE,
- UT_HOLDING_REGISTERS_ADDRESS,
- MAX_REGISTERS + 1,
- tab_rp_registers);
- printf("* preset_multiple_registers: ");
- if (ret == TOO_MANY_DATA) {
- printf("OK\n");
- } else {
- printf("FAILED\n");
- }
- close:
- /* Free the memory */
- free(tab_rp_status);
- free(tab_rp_registers);
- /* Close the connection */
- modbus_close(&mb_param);
-
- return 0;
- }
|