unit-test-master.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. if (modbus_connect(&mb_param) == -1) {
  41. printf("ERROR Connection failed\n");
  42. exit(1);
  43. }
  44. /* Allocate and initialize the memory to store the status */
  45. nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
  46. UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
  47. tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  48. memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
  49. /* Allocate and initialize the memory to store the registers */
  50. nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
  51. UT_INPUT_REGISTERS_NB_POINTS) ?
  52. UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
  53. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  54. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  55. printf("** UNIT TESTING **\n");
  56. printf("TEST READ FUNCTIONS:\n");
  57. /** COIL STATUS **/
  58. ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
  59. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  60. printf("* coil status: ");
  61. if (ret != UT_COIL_STATUS_NB_POINTS) {
  62. printf("FAILED (nb points %d)\n", ret);
  63. goto close;
  64. }
  65. i = 0;
  66. address = UT_COIL_STATUS_ADDRESS;
  67. nb_points = UT_COIL_STATUS_NB_POINTS;
  68. while (nb_points > 0) {
  69. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  70. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  71. if (value != UT_COIL_STATUS_TAB[i]) {
  72. printf("FAILED (%0X != %0X)\n",
  73. value, UT_COIL_STATUS_TAB[i]);
  74. goto close;
  75. }
  76. nb_points -= nb_bits;
  77. i++;
  78. }
  79. printf("OK\n");
  80. /** INPUT STATUS **/
  81. ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
  82. UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
  83. printf("* read_input_status: ");
  84. if (ret != UT_INPUT_STATUS_NB_POINTS) {
  85. printf("FAILED (nb points %d)\n", ret);
  86. goto close;
  87. }
  88. i = 0;
  89. address = UT_INPUT_STATUS_ADDRESS;
  90. nb_points = UT_INPUT_STATUS_NB_POINTS;
  91. while (nb_points > 0) {
  92. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  93. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  94. if (value != UT_INPUT_STATUS_TAB[i]) {
  95. printf("FAILED (%0X != %0X)\n",
  96. value, UT_INPUT_STATUS_TAB[i]);
  97. goto close;
  98. }
  99. nb_points -= nb_bits;
  100. i++;
  101. }
  102. printf("OK\n");
  103. /** HOLDING REGISTERS **/
  104. ret = read_holding_registers(&mb_param,
  105. SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
  106. UT_HOLDING_REGISTERS_NB_POINTS,
  107. tab_rp_registers);
  108. printf("* read_holding_registers: ");
  109. if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
  110. printf("FAILED (nb points %d)\n", ret);
  111. goto close;
  112. }
  113. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  114. if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
  115. printf("FAILED (%0X != %0X)\n",
  116. tab_rp_registers[i],
  117. UT_HOLDING_REGISTERS_TAB[i]);
  118. goto close;
  119. }
  120. }
  121. printf("OK\n");
  122. /** INPUT REGISTERS **/
  123. ret = read_input_registers(&mb_param,
  124. SLAVE, UT_INPUT_REGISTERS_ADDRESS,
  125. UT_INPUT_REGISTERS_NB_POINTS,
  126. tab_rp_registers);
  127. printf("* read_input_registers: ");
  128. if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
  129. printf("FAILED (nb points %d)\n", ret);
  130. goto close;
  131. }
  132. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  133. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  134. printf("FAILED (%0X != %0X)\n",
  135. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  136. goto close;
  137. }
  138. }
  139. printf("OK\n");
  140. /** WRITE FUNCTIONS **/
  141. printf("\nTEST WRITE FUNCTIONS:\n");
  142. ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON);
  143. printf("* force_single_coil: ");
  144. if (ret == 1) {
  145. printf("OK\n");
  146. } else {
  147. printf("FAILED\n");
  148. goto close;
  149. }
  150. ret = preset_single_register(&mb_param, SLAVE, 0x01, 0x03);
  151. printf("* preset_single_register: ");
  152. if (ret == 1) {
  153. printf("OK\n");
  154. } else {
  155. printf("FAILED\n");
  156. goto close;
  157. }
  158. {
  159. int nb_points = 0x0A;
  160. uint8_t tab_value[] = { ON, OFF, ON, ON,
  161. OFF, OFF, ON, ON,
  162. ON, OFF };
  163. ret = force_multiple_coils(&mb_param, SLAVE, 0x13,
  164. nb_points, tab_value);
  165. printf("* force_multiple_coils: ");
  166. if (ret == nb_points) {
  167. printf("OK\n");
  168. } else {
  169. printf("FAILED\n");
  170. goto close;
  171. }
  172. }
  173. {
  174. int nb_points = 2;
  175. uint16_t tab_value[] = { 0x000A, 0x0102 };
  176. ret = preset_multiple_registers(&mb_param, SLAVE,
  177. 0x01, nb_points, tab_value);
  178. printf("* preset_multiple_registers: ");
  179. if (ret == nb_points) {
  180. printf("OK\n");
  181. } else {
  182. printf("FAILED\n");
  183. goto close;
  184. }
  185. }
  186. /** ILLEGAL DATA ADDRESS */
  187. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  188. /* The mapping begins at 0 and ending at address + nb_points so
  189. * the addresses below are not valid. */
  190. ret = read_coil_status(&mb_param, SLAVE,
  191. UT_COIL_STATUS_ADDRESS,
  192. UT_COIL_STATUS_NB_POINTS + 1,
  193. tab_rp_status);
  194. printf("* read_coil_status: ");
  195. if (ret == ILLEGAL_DATA_ADDRESS)
  196. printf("OK\n");
  197. else
  198. printf("FAILED\n");
  199. ret = read_input_status(&mb_param, SLAVE,
  200. UT_INPUT_STATUS_ADDRESS,
  201. UT_INPUT_STATUS_NB_POINTS + 1,
  202. tab_rp_status);
  203. printf("* read_input_status: ");
  204. if (ret == ILLEGAL_DATA_ADDRESS)
  205. printf("OK\n");
  206. else
  207. printf("FAILED\n");
  208. ret = read_holding_registers(&mb_param, SLAVE,
  209. UT_HOLDING_REGISTERS_ADDRESS,
  210. UT_HOLDING_REGISTERS_NB_POINTS + 1,
  211. tab_rp_registers);
  212. printf("* read_holding_registers: ");
  213. if (ret == ILLEGAL_DATA_ADDRESS)
  214. printf("OK\n");
  215. else
  216. printf("FAILED\n");
  217. ret = read_input_registers(&mb_param, SLAVE,
  218. UT_INPUT_REGISTERS_ADDRESS,
  219. UT_INPUT_REGISTERS_NB_POINTS + 1,
  220. tab_rp_registers);
  221. printf("* read_input_registers: ");
  222. if (ret == ILLEGAL_DATA_ADDRESS)
  223. printf("OK\n");
  224. else
  225. printf("FAILED\n");
  226. ret = force_single_coil(&mb_param, SLAVE,
  227. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  228. ON);
  229. printf("* force_single_coil: ");
  230. if (ret == ILLEGAL_DATA_ADDRESS) {
  231. printf("OK\n");
  232. } else {
  233. printf("FAILED\n");
  234. }
  235. ret = force_multiple_coils(&mb_param, SLAVE,
  236. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  237. UT_COIL_STATUS_NB_POINTS,
  238. tab_rp_status);
  239. printf("* force_multiple_coils: ");
  240. if (ret == ILLEGAL_DATA_ADDRESS) {
  241. printf("OK\n");
  242. } else {
  243. printf("FAILED\n");
  244. }
  245. ret = preset_multiple_registers(&mb_param, SLAVE,
  246. UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
  247. UT_HOLDING_REGISTERS_NB_POINTS,
  248. tab_rp_registers);
  249. printf("* preset_multiple_registers: ");
  250. if (ret == ILLEGAL_DATA_ADDRESS) {
  251. printf("OK\n");
  252. } else {
  253. printf("FAILED\n");
  254. }
  255. /** TOO MANY DATA */
  256. printf("\nTEST TOO MANY DATA ERROR:\n");
  257. ret = read_coil_status(&mb_param, SLAVE,
  258. UT_COIL_STATUS_ADDRESS,
  259. MAX_STATUS + 1,
  260. tab_rp_status);
  261. printf("* read_coil_status: ");
  262. if (ret == TOO_MANY_DATA)
  263. printf("OK\n");
  264. else
  265. printf("FAILED\n");
  266. ret = read_input_status(&mb_param, SLAVE,
  267. UT_INPUT_STATUS_ADDRESS,
  268. MAX_STATUS + 1,
  269. tab_rp_status);
  270. printf("* read_input_status: ");
  271. if (ret == TOO_MANY_DATA)
  272. printf("OK\n");
  273. else
  274. printf("FAILED\n");
  275. ret = read_holding_registers(&mb_param, SLAVE,
  276. UT_HOLDING_REGISTERS_ADDRESS,
  277. MAX_REGISTERS + 1,
  278. tab_rp_registers);
  279. printf("* read_holding_registers: ");
  280. if (ret == TOO_MANY_DATA)
  281. printf("OK\n");
  282. else
  283. printf("FAILED\n");
  284. ret = read_input_registers(&mb_param, SLAVE,
  285. UT_INPUT_REGISTERS_ADDRESS,
  286. MAX_REGISTERS + 1,
  287. tab_rp_registers);
  288. printf("* read_input_registers: ");
  289. if (ret == TOO_MANY_DATA)
  290. printf("OK\n");
  291. else
  292. printf("FAILED\n");
  293. ret = force_multiple_coils(&mb_param, SLAVE,
  294. UT_COIL_STATUS_ADDRESS,
  295. MAX_STATUS + 1,
  296. tab_rp_status);
  297. printf("* force_multiple_coils: ");
  298. if (ret == TOO_MANY_DATA) {
  299. printf("OK\n");
  300. } else {
  301. printf("FAILED\n");
  302. }
  303. ret = preset_multiple_registers(&mb_param, SLAVE,
  304. UT_HOLDING_REGISTERS_ADDRESS,
  305. MAX_REGISTERS + 1,
  306. tab_rp_registers);
  307. printf("* preset_multiple_registers: ");
  308. if (ret == TOO_MANY_DATA) {
  309. printf("OK\n");
  310. } else {
  311. printf("FAILED\n");
  312. }
  313. close:
  314. /* Free the memory */
  315. free(tab_rp_status);
  316. free(tab_rp_registers);
  317. /* Close the connection */
  318. modbus_close(&mb_param);
  319. return 0;
  320. }