unit-test-client.c 16 KB

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