unit-test-client.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. enum {
  25. TCP,
  26. TCP_PI,
  27. RTU
  28. };
  29. int main(int argc, char *argv[])
  30. {
  31. uint8_t *tab_rp_bits;
  32. uint16_t *tab_rp_registers;
  33. uint16_t *tab_rp_registers_bad;
  34. modbus_t *ctx;
  35. int i;
  36. uint8_t value;
  37. int nb_points;
  38. int rc;
  39. float real;
  40. struct timeval old_response_timeout;
  41. struct timeval response_timeout;
  42. int use_backend;
  43. if (argc > 1) {
  44. if (strcmp(argv[1], "tcp") == 0) {
  45. use_backend = TCP;
  46. } else if (strcmp(argv[1], "tcppi") == 0) {
  47. use_backend = TCP_PI;
  48. } else if (strcmp(argv[1], "rtu") == 0) {
  49. use_backend = RTU;
  50. } else {
  51. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
  52. exit(1);
  53. }
  54. } else {
  55. /* By default */
  56. use_backend = TCP;
  57. }
  58. if (use_backend == TCP) {
  59. ctx = modbus_new_tcp("127.0.0.1", 1502);
  60. } else if (use_backend == TCP_PI) {
  61. ctx = modbus_new_tcp_pi("::1", "1502");
  62. } else {
  63. ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  64. }
  65. if (ctx == NULL) {
  66. fprintf(stderr, "Unable to allocate libmodbus context\n");
  67. return -1;
  68. }
  69. modbus_set_debug(ctx, TRUE);
  70. modbus_set_error_recovery(ctx,
  71. MODBUS_ERROR_RECOVERY_LINK |
  72. MODBUS_ERROR_RECOVERY_PROTOCOL);
  73. if (use_backend == RTU) {
  74. modbus_set_slave(ctx, SERVER_ID);
  75. }
  76. if (modbus_connect(ctx) == -1) {
  77. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  78. modbus_free(ctx);
  79. return -1;
  80. }
  81. /* Allocate and initialize the memory to store the bits */
  82. nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
  83. tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  84. memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
  85. /* Allocate and initialize the memory to store the registers */
  86. nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
  87. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  88. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  89. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  90. printf("** UNIT TESTING **\n");
  91. printf("\nTEST WRITE/READ:\n");
  92. /** COIL BITS **/
  93. /* Single */
  94. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
  95. printf("1/2 modbus_write_bit: ");
  96. if (rc == 1) {
  97. printf("OK\n");
  98. } else {
  99. printf("FAILED\n");
  100. goto close;
  101. }
  102. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
  103. printf("2/2 modbus_read_bits: ");
  104. if (rc != 1) {
  105. printf("FAILED (nb points %d)\n", rc);
  106. goto close;
  107. }
  108. if (tab_rp_bits[0] != ON) {
  109. printf("FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
  110. goto close;
  111. }
  112. printf("OK\n");
  113. /* End single */
  114. /* Multiple bits */
  115. {
  116. uint8_t tab_value[UT_BITS_NB];
  117. modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
  118. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  119. UT_BITS_NB, tab_value);
  120. printf("1/2 modbus_write_bits: ");
  121. if (rc == UT_BITS_NB) {
  122. printf("OK\n");
  123. } else {
  124. printf("FAILED\n");
  125. goto close;
  126. }
  127. }
  128. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
  129. printf("2/2 modbus_read_bits: ");
  130. if (rc != UT_BITS_NB) {
  131. printf("FAILED (nb points %d)\n", rc);
  132. goto close;
  133. }
  134. i = 0;
  135. nb_points = UT_BITS_NB;
  136. while (nb_points > 0) {
  137. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  138. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  139. if (value != UT_BITS_TAB[i]) {
  140. printf("FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
  141. goto close;
  142. }
  143. nb_points -= nb_bits;
  144. i++;
  145. }
  146. printf("OK\n");
  147. /* End of multiple bits */
  148. /** DISCRETE INPUTS **/
  149. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  150. UT_INPUT_BITS_NB, tab_rp_bits);
  151. printf("1/1 modbus_read_input_bits: ");
  152. if (rc != UT_INPUT_BITS_NB) {
  153. printf("FAILED (nb points %d)\n", rc);
  154. goto close;
  155. }
  156. i = 0;
  157. nb_points = UT_INPUT_BITS_NB;
  158. while (nb_points > 0) {
  159. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  160. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  161. if (value != UT_INPUT_BITS_TAB[i]) {
  162. printf("FAILED (%0X != %0X)\n", value, UT_INPUT_BITS_TAB[i]);
  163. goto close;
  164. }
  165. nb_points -= nb_bits;
  166. i++;
  167. }
  168. printf("OK\n");
  169. /** HOLDING REGISTERS **/
  170. /* Single register */
  171. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
  172. printf("1/2 modbus_write_register: ");
  173. if (rc == 1) {
  174. printf("OK\n");
  175. } else {
  176. printf("FAILED\n");
  177. goto close;
  178. }
  179. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  180. 1, tab_rp_registers);
  181. printf("2/2 modbus_read_registers: ");
  182. if (rc != 1) {
  183. printf("FAILED (nb points %d)\n", rc);
  184. goto close;
  185. }
  186. if (tab_rp_registers[0] != 0x1234) {
  187. printf("FAILED (%0X != %0X)\n",
  188. tab_rp_registers[0], 0x1234);
  189. goto close;
  190. }
  191. printf("OK\n");
  192. /* End of single register */
  193. /* Many registers */
  194. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  195. UT_REGISTERS_NB, UT_REGISTERS_TAB);
  196. printf("1/5 modbus_write_registers: ");
  197. if (rc == UT_REGISTERS_NB) {
  198. printf("OK\n");
  199. } else {
  200. printf("FAILED\n");
  201. goto close;
  202. }
  203. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  204. UT_REGISTERS_NB, tab_rp_registers);
  205. printf("2/5 modbus_read_registers: ");
  206. if (rc != UT_REGISTERS_NB) {
  207. printf("FAILED (nb points %d)\n", rc);
  208. goto close;
  209. }
  210. for (i=0; i < UT_REGISTERS_NB; i++) {
  211. if (tab_rp_registers[i] != UT_REGISTERS_TAB[i]) {
  212. printf("FAILED (%0X != %0X)\n",
  213. tab_rp_registers[i],
  214. UT_REGISTERS_TAB[i]);
  215. goto close;
  216. }
  217. }
  218. printf("OK\n");
  219. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  220. 0, tab_rp_registers);
  221. printf("3/5 modbus_read_registers (0): ");
  222. if (rc != 0) {
  223. printf("FAILED (nb points %d)\n", rc);
  224. goto close;
  225. }
  226. printf("OK\n");
  227. nb_points = (UT_REGISTERS_NB >
  228. UT_INPUT_REGISTERS_NB) ?
  229. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  230. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  231. /* Write registers to zero from tab_rp_registers and store read registers
  232. into tab_rp_registers. So the read registers must set to 0, except the
  233. first one because there is an offset of 1 register on write. */
  234. rc = modbus_write_and_read_registers(ctx,
  235. UT_REGISTERS_ADDRESS + 1, UT_REGISTERS_NB - 1,
  236. tab_rp_registers,
  237. UT_REGISTERS_ADDRESS,
  238. UT_REGISTERS_NB,
  239. tab_rp_registers);
  240. printf("4/5 modbus_write_and_read_registers: ");
  241. if (rc != UT_REGISTERS_NB) {
  242. printf("FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
  243. goto close;
  244. }
  245. if (tab_rp_registers[0] != UT_REGISTERS_TAB[0]) {
  246. printf("FAILED (%0X != %0X)\n",
  247. tab_rp_registers[0], UT_REGISTERS_TAB[0]);
  248. }
  249. for (i=1; i < UT_REGISTERS_NB; i++) {
  250. if (tab_rp_registers[i] != 0) {
  251. printf("FAILED (%0X != %0X)\n",
  252. tab_rp_registers[i], 0);
  253. goto close;
  254. }
  255. }
  256. printf("OK\n");
  257. /* End of many registers */
  258. /** INPUT REGISTERS **/
  259. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  260. UT_INPUT_REGISTERS_NB,
  261. tab_rp_registers);
  262. printf("1/1 modbus_read_input_registers: ");
  263. if (rc != UT_INPUT_REGISTERS_NB) {
  264. printf("FAILED (nb points %d)\n", rc);
  265. goto close;
  266. }
  267. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  268. if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
  269. printf("FAILED (%0X != %0X)\n",
  270. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  271. goto close;
  272. }
  273. }
  274. printf("OK\n");
  275. printf("\nTEST FLOATS\n");
  276. /** FLOAT **/
  277. printf("1/2 Set float: ");
  278. modbus_set_float(UT_REAL, tab_rp_registers);
  279. if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
  280. tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
  281. printf("OK\n");
  282. } else {
  283. printf("FAILED (%x != %x)\n",
  284. *((uint32_t *)tab_rp_registers), UT_IREAL);
  285. goto close;
  286. }
  287. printf("2/2 Get float: ");
  288. real = modbus_get_float(tab_rp_registers);
  289. if (real == UT_REAL) {
  290. printf("OK\n");
  291. } else {
  292. printf("FAILED (%f != %f)\n", real, UT_REAL);
  293. goto close;
  294. }
  295. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  296. /** ILLEGAL DATA ADDRESS **/
  297. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  298. /* The mapping begins at 0 and ends at address + nb_points so
  299. * the addresses are not valid. */
  300. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  301. UT_BITS_NB + 1, tab_rp_bits);
  302. printf("* modbus_read_bits: ");
  303. if (rc == -1 && errno == EMBXILADD) {
  304. printf("OK\n");
  305. } else {
  306. printf("FAILED\n");
  307. goto close;
  308. }
  309. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  310. UT_INPUT_BITS_NB + 1, tab_rp_bits);
  311. printf("* modbus_read_input_bits: ");
  312. if (rc == -1 && errno == EMBXILADD)
  313. printf("OK\n");
  314. else {
  315. printf("FAILED\n");
  316. goto close;
  317. }
  318. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  319. UT_REGISTERS_NB + 1, tab_rp_registers);
  320. printf("* modbus_read_registers: ");
  321. if (rc == -1 && errno == EMBXILADD)
  322. printf("OK\n");
  323. else {
  324. printf("FAILED\n");
  325. goto close;
  326. }
  327. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  328. UT_INPUT_REGISTERS_NB + 1,
  329. tab_rp_registers);
  330. printf("* modbus_read_input_registers: ");
  331. if (rc == -1 && errno == EMBXILADD)
  332. printf("OK\n");
  333. else {
  334. printf("FAILED\n");
  335. goto close;
  336. }
  337. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  338. printf("* modbus_write_bit: ");
  339. if (rc == -1 && errno == EMBXILADD) {
  340. printf("OK\n");
  341. } else {
  342. printf("FAILED\n");
  343. goto close;
  344. }
  345. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  346. UT_BITS_NB, tab_rp_bits);
  347. printf("* modbus_write_coils: ");
  348. if (rc == -1 && errno == EMBXILADD) {
  349. printf("OK\n");
  350. } else {
  351. printf("FAILED\n");
  352. goto close;
  353. }
  354. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  355. UT_REGISTERS_NB, tab_rp_registers);
  356. printf("* modbus_write_registers: ");
  357. if (rc == -1 && errno == EMBXILADD) {
  358. printf("OK\n");
  359. } else {
  360. printf("FAILED\n");
  361. goto close;
  362. }
  363. /** TOO MANY DATA **/
  364. printf("\nTEST TOO MANY DATA ERROR:\n");
  365. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  366. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  367. printf("* modbus_read_bits: ");
  368. if (rc == -1 && errno == EMBMDATA) {
  369. printf("OK\n");
  370. } else {
  371. printf("FAILED\n");
  372. goto close;
  373. }
  374. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  375. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  376. printf("* modbus_read_input_bits: ");
  377. if (rc == -1 && errno == EMBMDATA) {
  378. printf("OK\n");
  379. } else {
  380. printf("FAILED\n");
  381. goto close;
  382. }
  383. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  384. MODBUS_MAX_READ_REGISTERS + 1,
  385. tab_rp_registers);
  386. printf("* modbus_read_registers: ");
  387. if (rc == -1 && errno == EMBMDATA) {
  388. printf("OK\n");
  389. } else {
  390. printf("FAILED\n");
  391. goto close;
  392. }
  393. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  394. MODBUS_MAX_READ_REGISTERS + 1,
  395. tab_rp_registers);
  396. printf("* modbus_read_input_registers: ");
  397. if (rc == -1 && errno == EMBMDATA) {
  398. printf("OK\n");
  399. } else {
  400. printf("FAILED\n");
  401. goto close;
  402. }
  403. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  404. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  405. printf("* modbus_write_bits: ");
  406. if (rc == -1 && errno == EMBMDATA) {
  407. printf("OK\n");
  408. } else {
  409. goto close;
  410. printf("FAILED\n");
  411. }
  412. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  413. MODBUS_MAX_WRITE_REGISTERS + 1,
  414. tab_rp_registers);
  415. printf("* modbus_write_registers: ");
  416. if (rc == -1 && errno == EMBMDATA) {
  417. printf("OK\n");
  418. } else {
  419. printf("FAILED\n");
  420. goto close;
  421. }
  422. /** SLAVE REPLY **/
  423. printf("\nTEST SLAVE REPLY:\n");
  424. modbus_set_slave(ctx, INVALID_SERVER_ID);
  425. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  426. UT_REGISTERS_NB, tab_rp_registers);
  427. if (use_backend == RTU) {
  428. const int RAW_REQ_LENGTH = 6;
  429. uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 };
  430. /* Too many points */
  431. uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
  432. const int RAW_REP_LENGTH = 7;
  433. uint8_t raw_rep[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 };
  434. uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
  435. /* No response in RTU mode */
  436. printf("1/5-A No response from slave %d: ", INVALID_SERVER_ID);
  437. if (rc == -1 && errno == ETIMEDOUT) {
  438. printf("OK\n");
  439. } else {
  440. printf("FAILED\n");
  441. goto close;
  442. }
  443. /* The slave raises a timeout on a confirmation to ignore because if an
  444. * indication for another slave is received, a confirmation must follow */
  445. /* Send a pair of indication/confimration to the slave with a different
  446. * slave ID to simulate a communication on a RS485 bus. At first, the
  447. * slave will see the indication message then the confirmation, and it must
  448. * ignore both. */
  449. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  450. modbus_send_raw_request(ctx, raw_rep, RAW_REP_LENGTH * sizeof(uint8_t));
  451. rc = modbus_receive_confirmation(ctx, rsp);
  452. printf("1/5-B No response from slave %d on indication/confirmation messages: ",
  453. INVALID_SERVER_ID);
  454. if (rc == -1 && errno == ETIMEDOUT) {
  455. printf("OK\n");
  456. } else {
  457. printf("FAILED (%d)\n", rc);
  458. goto close;
  459. }
  460. /* Send an INVALID request for another slave */
  461. modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  462. rc = modbus_receive_confirmation(ctx, rsp);
  463. printf("1/5-C No response from slave %d with invalid request: ",
  464. INVALID_SERVER_ID);
  465. if (rc == -1 && errno == ETIMEDOUT) {
  466. printf("OK\n");
  467. } else {
  468. printf("FAILED (%d)\n", rc);
  469. goto close;
  470. }
  471. } else {
  472. /* Response in TCP mode */
  473. printf("1/4 Response from slave %d: ", INVALID_SERVER_ID);
  474. if (rc == UT_REGISTERS_NB) {
  475. printf("OK\n");
  476. } else {
  477. printf("FAILED\n");
  478. goto close;
  479. }
  480. }
  481. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  482. if (rc == -1) {
  483. printf("Invalid broacast address\n");
  484. goto close;
  485. }
  486. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  487. UT_REGISTERS_NB, tab_rp_registers);
  488. printf("2/5 Reply after a broadcast query: ");
  489. if (rc == UT_REGISTERS_NB) {
  490. printf("OK\n");
  491. } else {
  492. printf("FAILED\n");
  493. goto close;
  494. }
  495. /* Restore slave */
  496. if (use_backend == RTU) {
  497. modbus_set_slave(ctx, SERVER_ID);
  498. } else {
  499. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  500. }
  501. printf("3/5 Report slave ID: \n");
  502. /* tab_rp_bits is used to store bytes */
  503. rc = modbus_report_slave_id(ctx, tab_rp_bits);
  504. if (rc == -1) {
  505. printf("FAILED\n");
  506. goto close;
  507. }
  508. /* Slave ID is an arbitraty number for libmodbus */
  509. if (rc > 0) {
  510. printf("OK Slave ID is %d\n", tab_rp_bits[0]);
  511. } else {
  512. printf("FAILED\n");
  513. goto close;
  514. }
  515. /* Run status indicator */
  516. if (rc > 1 && tab_rp_bits[1] == 0xFF) {
  517. printf("OK Run Status Indicator is %s\n", tab_rp_bits[1] ? "ON" : "OFF");
  518. } else {
  519. printf("FAILED\n");
  520. goto close;
  521. }
  522. /* Print additional data as string */
  523. if (rc > 2) {
  524. printf("Additional data: ");
  525. for (i=2; i < rc; i++) {
  526. printf("%c", tab_rp_bits[i]);
  527. }
  528. printf("\n");
  529. }
  530. printf("5/5 Response with an invalid TID or slave: ");
  531. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
  532. 1, tab_rp_registers);
  533. if (rc == -1) {
  534. printf("OK\n");
  535. } else {
  536. printf("FAILED\n");
  537. goto close;
  538. }
  539. /* Save original timeout */
  540. modbus_get_response_timeout(ctx, &old_response_timeout);
  541. /* Define a new and too short timeout */
  542. response_timeout.tv_sec = 0;
  543. response_timeout.tv_usec = 0;
  544. modbus_set_response_timeout(ctx, &response_timeout);
  545. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  546. UT_REGISTERS_NB, tab_rp_registers);
  547. printf("4/4 Too short timeout: ");
  548. if (rc == -1 && errno == ETIMEDOUT) {
  549. printf("OK\n");
  550. } else {
  551. printf("FAILED (can fail on slow systems or Windows)\n");
  552. }
  553. /* Restore original timeout */
  554. modbus_set_response_timeout(ctx, &old_response_timeout);
  555. /* A wait and flush operation is done by the error recovery code of
  556. * libmodbus */
  557. /** BAD RESPONSE **/
  558. printf("\nTEST BAD RESPONSE ERROR:\n");
  559. /* Allocate only the required space */
  560. tab_rp_registers_bad = (uint16_t *) malloc(
  561. UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
  562. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  563. UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
  564. printf("* modbus_read_registers: ");
  565. if (rc == -1 && errno == EMBBADDATA) {
  566. printf("OK\n");
  567. } else {
  568. printf("FAILED\n");
  569. goto close;
  570. }
  571. free(tab_rp_registers_bad);
  572. /** MANUAL EXCEPTION **/
  573. printf("\nTEST MANUAL EXCEPTION:\n");
  574. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
  575. UT_REGISTERS_NB, tab_rp_registers);
  576. printf("* modbus_read_registers at special address: ");
  577. if (rc == -1 && errno == EMBXSBUSY) {
  578. printf("OK\n");
  579. } else {
  580. printf("FAILED\n");
  581. goto close;
  582. }
  583. /** RAW REQUEST */
  584. printf("\nTEST RAW REQUEST:\n");
  585. {
  586. const int RAW_REQ_LENGTH = 6;
  587. uint8_t raw_req[] = { (use_backend == RTU) ? SERVER_ID : 0xFF,
  588. 0x03, 0x00, 0x01, 0x0, 0x05 };
  589. int req_length;
  590. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  591. req_length = modbus_send_raw_request(ctx, raw_req,
  592. RAW_REQ_LENGTH * sizeof(uint8_t));
  593. printf("* modbus_send_raw_request: ");
  594. if ((use_backend == RTU && req_length == (RAW_REQ_LENGTH + 2)) ||
  595. ((use_backend == TCP || use_backend == TCP_PI) &&
  596. req_length == (RAW_REQ_LENGTH + 6))) {
  597. printf("OK\n");
  598. } else {
  599. printf("FAILED (%d)\n", req_length);
  600. goto close;
  601. }
  602. printf("* modbus_receive_confirmation: ");
  603. rc = modbus_receive_confirmation(ctx, rsp);
  604. if ((use_backend == RTU && rc == 15) ||
  605. ((use_backend == TCP || use_backend == TCP_PI) &&
  606. rc == 19)) {
  607. printf("OK\n");
  608. } else {
  609. printf("FAILED (%d)\n", rc);
  610. goto close;
  611. }
  612. }
  613. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  614. close:
  615. /* Free the memory */
  616. free(tab_rp_bits);
  617. free(tab_rp_registers);
  618. /* Close the connection */
  619. modbus_close(ctx);
  620. modbus_free(ctx);
  621. return 0;
  622. }