unit-test-master.c 19 KB

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