unit-test-master.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright © 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <modbus/modbus.h>
  22. #include "unit-test.h"
  23. /* Tests based on PI-MBUS-300 documentation */
  24. #define SLAVE 0x11
  25. int main(void)
  26. {
  27. uint8_t *tab_rp_status;
  28. uint16_t *tab_rp_registers;
  29. modbus_param_t mb_param;
  30. int i;
  31. uint8_t value;
  32. int address;
  33. int nb_points;
  34. int ret;
  35. /* RTU parity : none, even, odd */
  36. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  37. /* TCP */
  38. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  39. /* modbus_set_debug(&mb_param, TRUE);*/
  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. printf("Test read functions:\n");
  53. /** COIL STATUS **/
  54. ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  55. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  56. printf("* coil status: ");
  57. if (ret != UT_COIL_STATUS_NB_POINTS) {
  58. printf("FAILED (nb points %d)\n", ret);
  59. goto close;
  60. }
  61. i = 0;
  62. address = UT_COIL_STATUS_ADDRESS;
  63. nb_points = UT_COIL_STATUS_NB_POINTS;
  64. while (nb_points > 0) {
  65. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  66. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  67. if (value != UT_COIL_STATUS_TAB[i]) {
  68. printf("FAILED (%0X != %0X)\n",
  69. value, UT_COIL_STATUS_TAB[i]);
  70. goto close;
  71. }
  72. nb_points -= nb_bits;
  73. i++;
  74. }
  75. printf("OK\n");
  76. /** INPUT STATUS **/
  77. ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  78. UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
  79. printf("* input status :");
  80. if (ret != UT_INPUT_STATUS_NB_POINTS) {
  81. printf("FAILED (nb points %d)\n", ret);
  82. goto close;
  83. }
  84. i = 0;
  85. address = UT_INPUT_STATUS_ADDRESS;
  86. nb_points = UT_INPUT_STATUS_NB_POINTS;
  87. while (nb_points > 0) {
  88. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  89. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  90. if (value != UT_INPUT_STATUS_TAB[i]) {
  91. printf("FAILED (%0X != %0X)\n",
  92. value, UT_INPUT_STATUS_TAB[i]);
  93. goto close;
  94. }
  95. nb_points -= nb_bits;
  96. i++;
  97. }
  98. printf("OK\n");
  99. /** HOLDING REGISTERS **/
  100. ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  101. UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
  102. printf("* holding registers: ");
  103. if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
  104. printf("FAILED (nb points %d)\n", ret);
  105. goto close;
  106. }
  107. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  108. if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
  109. printf("FAILED (%0X != %0X)\n",
  110. tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
  111. goto close;
  112. }
  113. }
  114. printf("OK\n");
  115. /** INPUT REGISTERS **/
  116. ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  117. UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
  118. printf("* input registers: ");
  119. if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
  120. printf("FAILED (nb points %d)\n", ret);
  121. goto close;
  122. }
  123. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  124. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  125. printf("FAILED (%0X != %0X)\n",
  126. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  127. goto close;
  128. }
  129. }
  130. printf("OK\n");
  131. /** WRITE FUNCTIONS **/
  132. printf("\nTest write functions:\n");
  133. ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON);
  134. printf("* force single coil: ");
  135. if (ret == 1) {
  136. printf("OK\n");
  137. } else {
  138. printf("FAILED\n");
  139. goto close;
  140. }
  141. ret = preset_single_register(&mb_param, SLAVE, 0x01, 0x03);
  142. printf("* preset_single_register: ");
  143. if (ret == 1) {
  144. printf("OK\n");
  145. } else {
  146. printf("FAILED\n");
  147. goto close;
  148. }
  149. {
  150. int nb_points = 0x0A;
  151. uint8_t tab_value[] = { ON, OFF, ON, ON, OFF, OFF, ON, ON, ON, OFF };
  152. ret = force_multiple_coils(&mb_param, SLAVE, 0x13, nb_points, tab_value);
  153. printf("* force_multiple_coils: ");
  154. if (ret == nb_points) {
  155. printf("OK\n");
  156. } else {
  157. printf("FAILED\n");
  158. goto close;
  159. }
  160. }
  161. {
  162. int nb_points = 2;
  163. uint16_t tab_value[] = { 0x000A, 0x0102 };
  164. ret = preset_multiple_registers(&mb_param, SLAVE, 0x01, nb_points, tab_value);
  165. printf("* preset_multiple_registers: ");
  166. if (ret == nb_points) {
  167. printf("OK\n");
  168. } else {
  169. printf("FAILED\n");
  170. goto close;
  171. }
  172. }
  173. /** ILLEGAL DATA ADDRESS */
  174. printf("\nTest illegal data address:\n");
  175. /* The mapping begins at 0 and end at adresse + nb_points so
  176. * the adresses above are not valid. */
  177. ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  178. UT_COIL_STATUS_NB_POINTS + 1, tab_rp_status);
  179. printf("* coil status: ");
  180. if (ret == ILLEGAL_DATA_ADDRESS)
  181. printf("OK\n");
  182. else
  183. printf("FAILED\n");
  184. ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  185. UT_INPUT_STATUS_NB_POINTS + 1, tab_rp_status);
  186. printf("* input status: ");
  187. if (ret == ILLEGAL_DATA_ADDRESS)
  188. printf("OK\n");
  189. else
  190. printf("FAILED\n");
  191. ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  192. UT_HOLDING_REGISTERS_NB_POINTS + 1, tab_rp_registers);
  193. printf("* holding registers: ");
  194. if (ret == ILLEGAL_DATA_ADDRESS)
  195. printf("OK\n");
  196. else
  197. printf("FAILED\n");
  198. ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  199. UT_INPUT_REGISTERS_NB_POINTS + 1, tab_rp_registers);
  200. printf("* input registers: ");
  201. if (ret == ILLEGAL_DATA_ADDRESS)
  202. printf("OK\n");
  203. else
  204. printf("FAILED\n");
  205. ret = force_single_coil(&mb_param, SLAVE,
  206. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS, ON);
  207. printf("* force single coil: ");
  208. if (ret == ILLEGAL_DATA_ADDRESS) {
  209. printf("OK\n");
  210. } else {
  211. printf("FAILED\n");
  212. }
  213. ret = force_multiple_coils(&mb_param, SLAVE,
  214. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  215. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  216. printf("* force multipls coils: ");
  217. if (ret == ILLEGAL_DATA_ADDRESS) {
  218. printf("OK\n");
  219. } else {
  220. printf("FAILED\n");
  221. }
  222. ret = preset_multiple_registers(&mb_param, SLAVE,
  223. UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
  224. UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
  225. printf("* preset multiple registers: ");
  226. if (ret == ILLEGAL_DATA_ADDRESS) {
  227. printf("OK\n");
  228. } else {
  229. printf("FAILED\n");
  230. }
  231. close:
  232. /* Free the memory */
  233. free(tab_rp_status);
  234. free(tab_rp_registers);
  235. /* Close the connection */
  236. modbus_close(&mb_param);
  237. return 0;
  238. }