unit-test-client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/2 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/2 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. /* End of many registers */
  202. /** INPUT REGISTERS **/
  203. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  204. UT_INPUT_REGISTERS_NB_POINTS,
  205. tab_rp_registers);
  206. printf("1/1 modbus_read_input_registers: ");
  207. if (rc != UT_INPUT_REGISTERS_NB_POINTS) {
  208. printf("FAILED (nb points %d)\n", rc);
  209. goto close;
  210. }
  211. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  212. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  213. printf("FAILED (%0X != %0X)\n",
  214. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  215. goto close;
  216. }
  217. }
  218. printf("OK\n");
  219. printf("\nTEST FLOATS\n");
  220. /** FLOAT **/
  221. printf("1/2 Set float: ");
  222. modbus_set_float(UT_REAL, tab_rp_registers);
  223. if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
  224. tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
  225. printf("OK\n");
  226. } else {
  227. printf("FAILED (%x != %x)\n",
  228. *((uint32_t *)tab_rp_registers), UT_IREAL);
  229. goto close;
  230. }
  231. printf("2/2 Get float: ");
  232. real = modbus_get_float(tab_rp_registers);
  233. if (real == UT_REAL) {
  234. printf("OK\n");
  235. } else {
  236. printf("FAILED (%f != %f)\n", real, UT_REAL);
  237. goto close;
  238. }
  239. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  240. /** ILLEGAL DATA ADDRESS **/
  241. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  242. /* The mapping begins at 0 and ending at address + nb_points so
  243. * the addresses below are not valid. */
  244. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  245. UT_BITS_NB_POINTS + 1,
  246. tab_rp_bits);
  247. printf("* modbus_read_bits: ");
  248. if (rc == -1 && errno == EMBXILADD) {
  249. printf("OK\n");
  250. } else {
  251. printf("FAILED\n");
  252. goto close;
  253. }
  254. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  255. UT_INPUT_BITS_NB_POINTS + 1,
  256. tab_rp_bits);
  257. printf("* modbus_read_input_bits: ");
  258. if (rc == -1 && errno == EMBXILADD)
  259. printf("OK\n");
  260. else {
  261. printf("FAILED\n");
  262. goto close;
  263. }
  264. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  265. UT_REGISTERS_NB_POINTS + 1,
  266. tab_rp_registers);
  267. printf("* modbus_read_registers: ");
  268. if (rc == -1 && errno == EMBXILADD)
  269. printf("OK\n");
  270. else {
  271. printf("FAILED\n");
  272. goto close;
  273. }
  274. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  275. UT_INPUT_REGISTERS_NB_POINTS + 1,
  276. tab_rp_registers);
  277. printf("* modbus_read_input_registers: ");
  278. if (rc == -1 && errno == EMBXILADD)
  279. printf("OK\n");
  280. else {
  281. printf("FAILED\n");
  282. goto close;
  283. }
  284. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB_POINTS, ON);
  285. printf("* modbus_write_bit: ");
  286. if (rc == -1 && errno == EMBXILADD) {
  287. printf("OK\n");
  288. } else {
  289. printf("FAILED\n");
  290. goto close;
  291. }
  292. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
  293. UT_BITS_NB_POINTS,
  294. tab_rp_bits);
  295. printf("* modbus_write_coils: ");
  296. if (rc == -1 && errno == EMBXILADD) {
  297. printf("OK\n");
  298. } else {
  299. printf("FAILED\n");
  300. goto close;
  301. }
  302. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS +
  303. UT_REGISTERS_NB_POINTS,
  304. UT_REGISTERS_NB_POINTS,
  305. tab_rp_registers);
  306. printf("* modbus_write_registers: ");
  307. if (rc == -1 && errno == EMBXILADD) {
  308. printf("OK\n");
  309. } else {
  310. printf("FAILED\n");
  311. goto close;
  312. }
  313. /** TOO MANY DATA **/
  314. printf("\nTEST TOO MANY DATA ERROR:\n");
  315. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_BITS + 1,
  316. tab_rp_bits);
  317. printf("* modbus_read_bits: ");
  318. if (rc == -1 && errno == EMBMDATA) {
  319. printf("OK\n");
  320. } else {
  321. printf("FAILED\n");
  322. goto close;
  323. }
  324. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  325. MODBUS_MAX_BITS + 1,
  326. tab_rp_bits);
  327. printf("* modbus_read_input_bits: ");
  328. if (rc == -1 && errno == EMBMDATA) {
  329. printf("OK\n");
  330. } else {
  331. printf("FAILED\n");
  332. goto close;
  333. }
  334. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  335. MODBUS_MAX_REGISTERS + 1,
  336. tab_rp_registers);
  337. printf("* modbus_read_registers: ");
  338. if (rc == -1 && errno == EMBMDATA) {
  339. printf("OK\n");
  340. } else {
  341. printf("FAILED\n");
  342. goto close;
  343. }
  344. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  345. MODBUS_MAX_REGISTERS + 1,
  346. tab_rp_registers);
  347. printf("* modbus_read_input_registers: ");
  348. if (rc == -1 && errno == EMBMDATA) {
  349. printf("OK\n");
  350. } else {
  351. printf("FAILED\n");
  352. goto close;
  353. }
  354. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  355. MODBUS_MAX_BITS + 1,
  356. tab_rp_bits);
  357. printf("* modbus_write_bits: ");
  358. if (rc == -1 && errno == EMBMDATA) {
  359. printf("OK\n");
  360. } else {
  361. goto close;
  362. printf("FAILED\n");
  363. }
  364. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  365. MODBUS_MAX_REGISTERS + 1,
  366. tab_rp_registers);
  367. printf("* modbus_write_registers: ");
  368. if (rc == -1 && errno == EMBMDATA) {
  369. printf("OK\n");
  370. } else {
  371. printf("FAILED\n");
  372. goto close;
  373. }
  374. /** SLAVE REPLY **/
  375. printf("\nTEST SLAVE REPLY:\n");
  376. modbus_set_slave(ctx, 18);
  377. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  378. UT_REGISTERS_NB_POINTS,
  379. tab_rp_registers);
  380. printf("1/3 No or response from slave %d: ", 18);
  381. if (is_mode_rtu) {
  382. /* No response in RTU mode */
  383. if (rc == -1 && errno == ETIMEDOUT) {
  384. printf("OK\n");
  385. } else {
  386. printf("FAILED\n");
  387. goto close;
  388. }
  389. } else {
  390. /* Response in TCP mode */
  391. if (rc == UT_REGISTERS_NB_POINTS) {
  392. printf("OK\n");
  393. } else {
  394. printf("FAILED\n");
  395. goto close;
  396. }
  397. }
  398. modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  399. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  400. UT_REGISTERS_NB_POINTS,
  401. tab_rp_registers);
  402. printf("2/3 Reply after a broadcast query: ");
  403. if (rc == UT_REGISTERS_NB_POINTS) {
  404. printf("OK\n");
  405. } else {
  406. printf("FAILED\n");
  407. goto close;
  408. }
  409. /* Restore slave */
  410. if (is_mode_rtu) {
  411. modbus_set_slave(ctx, SERVER_ID);
  412. } else {
  413. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  414. }
  415. /* Save original timeout */
  416. modbus_get_timeout_begin(ctx, &timeout_begin_old);
  417. /* Define a new and too short timeout */
  418. timeout_begin_new.tv_sec = 0;
  419. timeout_begin_new.tv_usec = 0;
  420. modbus_set_timeout_begin(ctx, &timeout_begin_new);
  421. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  422. UT_REGISTERS_NB_POINTS,
  423. tab_rp_registers);
  424. printf("3/3 Too short timeout: ");
  425. if (rc == -1 && errno == ETIMEDOUT) {
  426. printf("OK\n");
  427. } else {
  428. printf("FAILED\n");
  429. goto close;
  430. }
  431. /* Restore original timeout */
  432. modbus_set_timeout_begin(ctx, &timeout_begin_old);
  433. /** BAD RESPONSE **/
  434. printf("\nTEST BAD RESPONSE ERROR:\n");
  435. /* Allocate only the required space */
  436. tab_rp_registers_bad = (uint16_t *) malloc(
  437. UT_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
  438. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  439. UT_REGISTERS_NB_POINTS_SPECIAL,
  440. tab_rp_registers_bad);
  441. printf("* modbus_read_registers: ");
  442. if (rc == -1 && errno == EMBBADDATA) {
  443. printf("OK\n");
  444. } else {
  445. printf("FAILED\n");
  446. goto close;
  447. }
  448. free(tab_rp_registers_bad);
  449. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  450. close:
  451. /* Free the memory */
  452. free(tab_rp_bits);
  453. free(tab_rp_registers);
  454. /* Close the connection */
  455. modbus_close(ctx);
  456. modbus_free(ctx);
  457. return 0;
  458. }