unit-test-master.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Copyright © 2008-2010 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 <errno.h>
  22. #include <modbus.h>
  23. #include "unit-test.h"
  24. /* Tests based on PI-MBUS-300 documentation */
  25. #define SLAVE 0x11
  26. int main(void)
  27. {
  28. uint8_t *tab_rp_status;
  29. uint16_t *tab_rp_registers;
  30. uint16_t *tab_rp_registers_bad;
  31. modbus_param_t mb_param;
  32. int i;
  33. uint8_t value;
  34. int address;
  35. int nb_points;
  36. int rc;
  37. float real;
  38. /* RTU parity : none, even, odd */
  39. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, SLAVE); */
  40. /* TCP */
  41. modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  42. modbus_set_debug(&mb_param, TRUE);
  43. if (modbus_connect(&mb_param) == -1) {
  44. fprintf(stderr, "Connection failed: %s\n",
  45. modbus_strerror(errno));
  46. return -1;
  47. }
  48. /* Allocate and initialize the memory to store the status */
  49. nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
  50. UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
  51. tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  52. memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
  53. /* Allocate and initialize the memory to store the registers */
  54. nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
  55. UT_INPUT_REGISTERS_NB_POINTS) ?
  56. UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
  57. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  58. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  59. printf("** UNIT TESTING **\n");
  60. printf("\nTEST WRITE/READ:\n");
  61. /** COIL STATUS **/
  62. /* Single */
  63. rc = force_single_coil(&mb_param, UT_COIL_STATUS_ADDRESS, ON);
  64. printf("1/2 force_single_coil: ");
  65. if (rc == 1) {
  66. printf("OK\n");
  67. } else {
  68. printf("FAILED\n");
  69. goto close;
  70. }
  71. rc = read_coil_status(&mb_param, UT_COIL_STATUS_ADDRESS, 1,
  72. tab_rp_status);
  73. printf("2/2 read_coil_status: ");
  74. if (rc != 1) {
  75. printf("FAILED (nb points %d)\n", rc);
  76. goto close;
  77. }
  78. if (tab_rp_status[0] != ON) {
  79. printf("FAILED (%0X = != %0X)\n", tab_rp_status[0], ON);
  80. goto close;
  81. }
  82. printf("OK\n");
  83. /* End single */
  84. /* Multiple coils */
  85. {
  86. uint8_t tab_value[UT_COIL_STATUS_NB_POINTS];
  87. set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS,
  88. UT_COIL_STATUS_TAB);
  89. rc = force_multiple_coils(&mb_param,
  90. UT_COIL_STATUS_ADDRESS,
  91. UT_COIL_STATUS_NB_POINTS,
  92. tab_value);
  93. printf("1/2 force_multiple_coils: ");
  94. if (rc == UT_COIL_STATUS_NB_POINTS) {
  95. printf("OK\n");
  96. } else {
  97. printf("FAILED\n");
  98. goto close;
  99. }
  100. }
  101. rc = read_coil_status(&mb_param, UT_COIL_STATUS_ADDRESS,
  102. UT_COIL_STATUS_NB_POINTS, tab_rp_status);
  103. printf("2/2 read_coil_status: ");
  104. if (rc != UT_COIL_STATUS_NB_POINTS) {
  105. printf("FAILED (nb points %d)\n", rc);
  106. goto close;
  107. }
  108. i = 0;
  109. address = UT_COIL_STATUS_ADDRESS;
  110. nb_points = UT_COIL_STATUS_NB_POINTS;
  111. while (nb_points > 0) {
  112. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  113. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  114. if (value != UT_COIL_STATUS_TAB[i]) {
  115. printf("FAILED (%0X != %0X)\n",
  116. value, UT_COIL_STATUS_TAB[i]);
  117. goto close;
  118. }
  119. nb_points -= nb_bits;
  120. i++;
  121. }
  122. printf("OK\n");
  123. /* End of multiple coils */
  124. /** INPUT STATUS **/
  125. rc = read_input_status(&mb_param, UT_INPUT_STATUS_ADDRESS,
  126. UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
  127. printf("1/1 read_input_status: ");
  128. if (rc != UT_INPUT_STATUS_NB_POINTS) {
  129. printf("FAILED (nb points %d)\n", rc);
  130. goto close;
  131. }
  132. i = 0;
  133. address = UT_INPUT_STATUS_ADDRESS;
  134. nb_points = UT_INPUT_STATUS_NB_POINTS;
  135. while (nb_points > 0) {
  136. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  137. value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
  138. if (value != UT_INPUT_STATUS_TAB[i]) {
  139. printf("FAILED (%0X != %0X)\n",
  140. value, UT_INPUT_STATUS_TAB[i]);
  141. goto close;
  142. }
  143. nb_points -= nb_bits;
  144. i++;
  145. }
  146. printf("OK\n");
  147. /** HOLDING REGISTERS **/
  148. /* Single register */
  149. rc = preset_single_register(&mb_param,
  150. UT_HOLDING_REGISTERS_ADDRESS, 0x1234);
  151. printf("1/2 preset_single_register: ");
  152. if (rc == 1) {
  153. printf("OK\n");
  154. } else {
  155. printf("FAILED\n");
  156. goto close;
  157. }
  158. rc = read_holding_registers(&mb_param,
  159. UT_HOLDING_REGISTERS_ADDRESS,
  160. 1, tab_rp_registers);
  161. printf("2/2 read_holding_registers: ");
  162. if (rc != 1) {
  163. printf("FAILED (nb points %d)\n", rc);
  164. goto close;
  165. }
  166. if (tab_rp_registers[0] != 0x1234) {
  167. printf("FAILED (%0X != %0X)\n",
  168. tab_rp_registers[0], 0x1234);
  169. goto close;
  170. }
  171. printf("OK\n");
  172. /* End of single register */
  173. /* Many registers */
  174. rc = preset_multiple_registers(&mb_param,
  175. UT_HOLDING_REGISTERS_ADDRESS,
  176. UT_HOLDING_REGISTERS_NB_POINTS,
  177. UT_HOLDING_REGISTERS_TAB);
  178. printf("1/2 preset_multiple_registers: ");
  179. if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
  180. printf("OK\n");
  181. } else {
  182. printf("FAILED\n");
  183. goto close;
  184. }
  185. rc = read_holding_registers(&mb_param,
  186. UT_HOLDING_REGISTERS_ADDRESS,
  187. UT_HOLDING_REGISTERS_NB_POINTS,
  188. tab_rp_registers);
  189. printf("2/2 read_holding_registers: ");
  190. if (rc != UT_HOLDING_REGISTERS_NB_POINTS) {
  191. printf("FAILED (nb points %d)\n", rc);
  192. goto close;
  193. }
  194. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  195. if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
  196. printf("FAILED (%0X != %0X)\n",
  197. tab_rp_registers[i],
  198. UT_HOLDING_REGISTERS_TAB[i]);
  199. goto close;
  200. }
  201. }
  202. printf("OK\n");
  203. /* End of many registers */
  204. /** INPUT REGISTERS **/
  205. rc = read_input_registers(&mb_param,
  206. UT_INPUT_REGISTERS_ADDRESS,
  207. UT_INPUT_REGISTERS_NB_POINTS,
  208. tab_rp_registers);
  209. printf("1/1 read_input_registers: ");
  210. if (rc != UT_INPUT_REGISTERS_NB_POINTS) {
  211. printf("FAILED (nb points %d)\n", rc);
  212. goto close;
  213. }
  214. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  215. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  216. printf("FAILED (%0X != %0X)\n",
  217. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  218. goto close;
  219. }
  220. }
  221. printf("OK\n");
  222. printf("\nTEST FLOATS\n");
  223. /** FLOAT **/
  224. printf("1/2 Write float: ");
  225. modbus_write_float(UT_REAL, tab_rp_registers);
  226. if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
  227. tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
  228. printf("OK\n");
  229. } else {
  230. printf("FAILED (%x != %x)\n",
  231. *((uint32_t *)tab_rp_registers), UT_IREAL);
  232. goto close;
  233. }
  234. printf("2/2 Read float: ");
  235. real = modbus_read_float(tab_rp_registers);
  236. if (real == UT_REAL) {
  237. printf("OK\n");
  238. } else {
  239. printf("FAILED (%f != %f)\n", real, UT_REAL);
  240. goto close;
  241. }
  242. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  243. /** ILLEGAL DATA ADDRESS **/
  244. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  245. /* The mapping begins at 0 and ending at address + nb_points so
  246. * the addresses below are not valid. */
  247. rc = read_coil_status(&mb_param,
  248. UT_COIL_STATUS_ADDRESS,
  249. UT_COIL_STATUS_NB_POINTS + 1,
  250. tab_rp_status);
  251. printf("* read_coil_status: ");
  252. if (rc == -1 && errno == EMBXILADD)
  253. printf("OK\n");
  254. else {
  255. printf("FAILED\n");
  256. goto close;
  257. }
  258. rc = read_input_status(&mb_param,
  259. UT_INPUT_STATUS_ADDRESS,
  260. UT_INPUT_STATUS_NB_POINTS + 1,
  261. tab_rp_status);
  262. printf("* read_input_status: ");
  263. if (rc == -1 && errno == EMBXILADD)
  264. printf("OK\n");
  265. else {
  266. printf("FAILED\n");
  267. goto close;
  268. }
  269. rc = read_holding_registers(&mb_param,
  270. UT_HOLDING_REGISTERS_ADDRESS,
  271. UT_HOLDING_REGISTERS_NB_POINTS + 1,
  272. tab_rp_registers);
  273. printf("* read_holding_registers: ");
  274. if (rc == -1 && errno == EMBXILADD)
  275. printf("OK\n");
  276. else {
  277. printf("FAILED\n");
  278. goto close;
  279. }
  280. rc = read_input_registers(&mb_param,
  281. UT_INPUT_REGISTERS_ADDRESS,
  282. UT_INPUT_REGISTERS_NB_POINTS + 1,
  283. tab_rp_registers);
  284. printf("* read_input_registers: ");
  285. if (rc == -1 && errno == EMBXILADD)
  286. printf("OK\n");
  287. else {
  288. printf("FAILED\n");
  289. goto close;
  290. }
  291. rc = force_single_coil(&mb_param,
  292. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  293. ON);
  294. printf("* force_single_coil: ");
  295. if (rc == -1 && errno == EMBXILADD) {
  296. printf("OK\n");
  297. } else {
  298. printf("FAILED\n");
  299. goto close;
  300. }
  301. rc = force_multiple_coils(&mb_param,
  302. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  303. UT_COIL_STATUS_NB_POINTS,
  304. tab_rp_status);
  305. printf("* force_multiple_coils: ");
  306. if (rc == -1 && errno == EMBXILADD) {
  307. printf("OK\n");
  308. } else {
  309. printf("FAILED\n");
  310. goto close;
  311. }
  312. rc = preset_multiple_registers(&mb_param,
  313. UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
  314. UT_HOLDING_REGISTERS_NB_POINTS,
  315. tab_rp_registers);
  316. printf("* preset_multiple_registers: ");
  317. if (rc == -1 && errno == EMBXILADD) {
  318. printf("OK\n");
  319. } else {
  320. printf("FAILED\n");
  321. goto close;
  322. }
  323. /** TOO MANY DATA **/
  324. printf("\nTEST TOO MANY DATA ERROR:\n");
  325. rc = read_coil_status(&mb_param,
  326. UT_COIL_STATUS_ADDRESS,
  327. MAX_STATUS + 1,
  328. tab_rp_status);
  329. printf("* read_coil_status: ");
  330. if (rc == -1 && errno == EMBMDATA) {
  331. printf("OK\n");
  332. } else {
  333. printf("FAILED\n");
  334. goto close;
  335. }
  336. rc = read_input_status(&mb_param,
  337. UT_INPUT_STATUS_ADDRESS,
  338. MAX_STATUS + 1,
  339. tab_rp_status);
  340. printf("* read_input_status: ");
  341. if (rc == -1 && errno == EMBMDATA) {
  342. printf("OK\n");
  343. } else {
  344. printf("FAILED\n");
  345. goto close;
  346. }
  347. rc = read_holding_registers(&mb_param,
  348. UT_HOLDING_REGISTERS_ADDRESS,
  349. MAX_REGISTERS + 1,
  350. tab_rp_registers);
  351. printf("* read_holding_registers: ");
  352. if (rc == -1 && errno == EMBMDATA) {
  353. printf("OK\n");
  354. } else {
  355. printf("FAILED\n");
  356. goto close;
  357. }
  358. rc = read_input_registers(&mb_param,
  359. UT_INPUT_REGISTERS_ADDRESS,
  360. MAX_REGISTERS + 1,
  361. tab_rp_registers);
  362. printf("* read_input_registers: ");
  363. if (rc == -1 && errno == EMBMDATA) {
  364. printf("OK\n");
  365. } else {
  366. printf("FAILED\n");
  367. goto close;
  368. }
  369. rc = force_multiple_coils(&mb_param,
  370. UT_COIL_STATUS_ADDRESS,
  371. MAX_STATUS + 1,
  372. tab_rp_status);
  373. printf("* force_multiple_coils: ");
  374. if (rc == -1 && errno == EMBMDATA) {
  375. printf("OK\n");
  376. } else {
  377. goto close;
  378. printf("FAILED\n");
  379. }
  380. rc = preset_multiple_registers(&mb_param,
  381. UT_HOLDING_REGISTERS_ADDRESS,
  382. MAX_REGISTERS + 1,
  383. tab_rp_registers);
  384. printf("* preset_multiple_registers: ");
  385. if (rc == -1 && errno == EMBMDATA) {
  386. printf("OK\n");
  387. } else {
  388. printf("FAILED\n");
  389. goto close;
  390. }
  391. /** SLAVE REPLY **/
  392. printf("\nTEST SLAVE REPLY:\n");
  393. modbus_set_slave(&mb_param, 18);
  394. rc = read_holding_registers(&mb_param,
  395. UT_HOLDING_REGISTERS_ADDRESS+1,
  396. UT_HOLDING_REGISTERS_NB_POINTS,
  397. tab_rp_registers);
  398. printf("1/2 No reply from slave %d: ", mb_param.slave);
  399. if (rc == -1 && errno == ETIMEDOUT) {
  400. printf("OK\n");
  401. } else {
  402. printf("FAILED\n");
  403. goto close;
  404. }
  405. modbus_set_slave(&mb_param, MODBUS_BROADCAST_ADDRESS);
  406. rc = read_holding_registers(&mb_param,
  407. UT_HOLDING_REGISTERS_ADDRESS,
  408. UT_HOLDING_REGISTERS_NB_POINTS,
  409. tab_rp_registers);
  410. printf("2/2 Reply after a broadcast query: ");
  411. if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
  412. printf("OK\n");
  413. } else {
  414. goto close;
  415. printf("FAILED\n");
  416. }
  417. /** BAD RESPONSE **/
  418. printf("\nTEST BAD RESPONSE ERROR:\n");
  419. /* Allocate only the required space */
  420. tab_rp_registers_bad = (uint16_t *) malloc(
  421. UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
  422. rc = read_holding_registers(&mb_param,
  423. UT_HOLDING_REGISTERS_ADDRESS,
  424. UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL,
  425. tab_rp_registers_bad);
  426. printf("* read_holding_registers: ");
  427. if (rc == -1 && errno == EMBBADDATA) {
  428. printf("OK\n");
  429. } else {
  430. printf("FAILED\n");
  431. goto close;
  432. }
  433. free(tab_rp_registers_bad);
  434. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  435. close:
  436. /* Free the memory */
  437. free(tab_rp_status);
  438. free(tab_rp_registers);
  439. /* Close the connection */
  440. modbus_close(&mb_param);
  441. return 0;
  442. }