unit-test-master.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. int ret;
  37. /* RTU parity : none, even, odd */
  38. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  39. /* TCP */
  40. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  41. modbus_set_debug(&mb_param, TRUE);
  42. modbus_connect(&mb_param);
  43. /* Allocate and initialize the memory to store the status */
  44. nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
  45. UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
  46. tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  47. memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
  48. /* Allocate and initialize the memory to store the registers */
  49. nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > UT_INPUT_REGISTERS_NB_POINTS) ?
  50. UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
  51. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  52. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  53. printf("UNIT TESTING\n");
  54. printf("Test read functions:\n");
  55. /** COIL STATUS **/
  56. ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  57. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  58. printf("* coil status: ");
  59. if (ret != UT_COIL_STATUS_NB_POINTS) {
  60. printf("FAILED (nb points %d)\n", ret);
  61. goto close;
  62. }
  63. i = 0;
  64. address = UT_COIL_STATUS_ADDRESS;
  65. nb_points = UT_COIL_STATUS_NB_POINTS;
  66. while (nb_points > 0) {
  67. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  68. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  69. if (value != UT_COIL_STATUS_TAB[i]) {
  70. printf("FAILED (%0X != %0X)\n",
  71. value, UT_COIL_STATUS_TAB[i]);
  72. goto close;
  73. }
  74. nb_points -= nb_bits;
  75. i++;
  76. }
  77. printf("OK\n");
  78. /** INPUT STATUS **/
  79. ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  80. UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
  81. printf("* input status :");
  82. if (ret != UT_INPUT_STATUS_NB_POINTS) {
  83. printf("FAILED (nb points %d)\n", ret);
  84. goto close;
  85. }
  86. i = 0;
  87. address = UT_INPUT_STATUS_ADDRESS;
  88. nb_points = UT_INPUT_STATUS_NB_POINTS;
  89. while (nb_points > 0) {
  90. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  91. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  92. if (value != UT_INPUT_STATUS_TAB[i]) {
  93. printf("FAILED (%0X != %0X)\n",
  94. value, UT_INPUT_STATUS_TAB[i]);
  95. goto close;
  96. }
  97. nb_points -= nb_bits;
  98. i++;
  99. }
  100. printf("OK\n");
  101. /** HOLDING REGISTERS **/
  102. ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  103. UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
  104. printf("* holding registers: ");
  105. if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
  106. printf("FAILED (nb points %d)\n", ret);
  107. goto close;
  108. }
  109. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  110. if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
  111. printf("FAILED (%0X != %0X)\n",
  112. tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
  113. goto close;
  114. }
  115. }
  116. printf("OK\n");
  117. /** INPUT REGISTERS **/
  118. ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  119. UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
  120. printf("* input registers: ");
  121. if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
  122. printf("FAILED (nb points %d)\n", ret);
  123. goto close;
  124. }
  125. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  126. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  127. printf("FAILED (%0X != %0X)\n",
  128. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  129. goto close;
  130. }
  131. }
  132. printf("OK\n");
  133. /** WRITE FUNCTIONS **/
  134. printf("\nTest write functions:\n");
  135. ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON);
  136. printf("* force single coil: ");
  137. if (ret == 1) {
  138. printf("OK\n");
  139. } else {
  140. printf("FAILED\n");
  141. goto close;
  142. }
  143. ret = preset_single_register(&mb_param, SLAVE, 0x01, 0x03);
  144. printf("* preset_single_register: ");
  145. if (ret == 1) {
  146. printf("OK\n");
  147. } else {
  148. printf("FAILED\n");
  149. goto close;
  150. }
  151. /** ILLEGAL DATA ADDRESS */
  152. printf("\nTest illegal data address:");
  153. /* The mapping begins at 0 and end at adresse + nb_points so
  154. * the adresses above are not valid. */
  155. ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  156. UT_COIL_STATUS_NB_POINTS + 1, tab_rp_status);
  157. printf("* coil status: ");
  158. if (ret == ILLEGAL_DATA_ADDRESS)
  159. printf("OK");
  160. else
  161. printf("FAILED");
  162. ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  163. UT_INPUT_STATUS_NB_POINTS + 1, tab_rp_status);
  164. printf("* input status: ");
  165. if (ret == ILLEGAL_DATA_ADDRESS)
  166. printf("OK");
  167. else
  168. printf("FAILED");
  169. ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  170. UT_HOLDING_REGISTERS_NB_POINTS + 1, tab_rp_registers);
  171. printf("* holding registers: ");
  172. if (ret == ILLEGAL_DATA_ADDRESS)
  173. printf("OK");
  174. else
  175. printf("FAILED");
  176. ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  177. UT_INPUT_REGISTERS_NB_POINTS + 1, tab_rp_registers);
  178. printf("* input registers: ");
  179. if (ret == ILLEGAL_DATA_ADDRESS)
  180. printf("OK");
  181. else
  182. printf("FAILED");
  183. ret = force_single_coil(&mb_param, SLAVE,
  184. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS, ON);
  185. printf("* force single coil: ");
  186. if (ret == ILLEGAL_DATA_ADDRESS) {
  187. printf("OK\n");
  188. } else {
  189. printf("FAILED\n");
  190. }
  191. close:
  192. /* Free the memory */
  193. free(tab_rp_status);
  194. free(tab_rp_registers);
  195. /* Close the connection */
  196. modbus_close(&mb_param);
  197. return 0;
  198. /*
  199. force_single_coil(&mb_param, SLAVE, 0xAC, ON);
  200. */
  201. }