unit-test-client.c 22 KB

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