unit-test-master.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * Licensed under the GNU General Public License Version 2
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <modbus/modbus.h>
  24. #include "unit-test.h"
  25. /* Tests based on PI-MBUS-300 documentation */
  26. #define SLAVE 0x11
  27. int main(void)
  28. {
  29. uint8_t *tab_rp_status;
  30. uint16_t *tab_rp_registers;
  31. modbus_param_t mb_param;
  32. int i;
  33. uint8_t value;
  34. uint16_t address;
  35. uint16_t nb_points;
  36. /* RTU parity : none, even, odd */
  37. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  38. /* TCP */
  39. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  40. modbus_connect(&mb_param);
  41. /* Allocate and initialize the memory to store the status */
  42. nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
  43. UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
  44. tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  45. memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
  46. /* Allocate and initialize the memory to store the registers */
  47. nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > UT_INPUT_REGISTERS_NB_POINTS) ?
  48. UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
  49. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  50. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  51. printf("Unit testing\n");
  52. /** COIL STATUS **/
  53. read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  54. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  55. i = 0;
  56. address = UT_COIL_STATUS_ADDRESS;
  57. nb_points = UT_COIL_STATUS_NB_POINTS;
  58. while (nb_points > 0) {
  59. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  60. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  61. if (value != UT_COIL_STATUS_TAB[i]) {
  62. printf("Coil status: FAILED (%0X != %0X)\n",
  63. value, UT_COIL_STATUS_TAB[i]);
  64. goto close;
  65. }
  66. nb_points -= nb_bits;
  67. i++;
  68. }
  69. printf("Coil status: OK\n");
  70. /** INPUT STATUS **/
  71. read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  72. UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
  73. i = 0;
  74. address = UT_INPUT_STATUS_ADDRESS;
  75. nb_points = UT_INPUT_STATUS_NB_POINTS;
  76. while (nb_points > 0) {
  77. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  78. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  79. if (value != UT_INPUT_STATUS_TAB[i]) {
  80. printf("Input status: FAILED (%0X != %0X)\n",
  81. value, UT_INPUT_STATUS_TAB[i]);
  82. goto close;
  83. }
  84. nb_points -= nb_bits;
  85. i++;
  86. }
  87. printf("Input status: OK\n");
  88. /** HOLDING REGISTERS **/
  89. read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  90. UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
  91. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  92. if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
  93. printf("Holding registers: FAILED (%0X != %0X)\n",
  94. tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
  95. goto close;
  96. }
  97. }
  98. printf("Holding registers: OK\n");
  99. /** INPUT REGISTERS **/
  100. read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  101. UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
  102. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  103. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  104. printf("Input registers: FAILED (%0X != %0X)\n",
  105. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  106. goto close;
  107. }
  108. }
  109. printf("Input registers: OK\n");
  110. close:
  111. /* Free the memory */
  112. free(tab_rp_status);
  113. free(tab_rp_registers);
  114. /* Close the connection */
  115. modbus_close(&mb_param);
  116. return 0;
  117. /*
  118. force_single_coil(&mb_param, SLAVE, 0xAC, ON);
  119. */
  120. }