unit-test-client.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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",
  78. 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/2 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. printf("FAILED (%x != %x)\n",
  285. *((uint32_t *)tab_rp_registers), UT_IREAL);
  286. goto close;
  287. }
  288. printf("2/2 Get float: ");
  289. real = modbus_get_float(tab_rp_registers);
  290. if (real == UT_REAL) {
  291. printf("OK\n");
  292. } else {
  293. printf("FAILED (%f != %f)\n", real, UT_REAL);
  294. goto close;
  295. }
  296. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  297. /** ILLEGAL DATA ADDRESS **/
  298. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  299. /* The mapping begins at 0 and ends at address + nb_points so
  300. * the addresses are not valid. */
  301. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  302. UT_BITS_NB + 1, tab_rp_bits);
  303. printf("* modbus_read_bits: ");
  304. if (rc == -1 && errno == EMBXILADD) {
  305. printf("OK\n");
  306. } else {
  307. printf("FAILED\n");
  308. goto close;
  309. }
  310. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  311. UT_INPUT_BITS_NB + 1, tab_rp_bits);
  312. printf("* modbus_read_input_bits: ");
  313. if (rc == -1 && errno == EMBXILADD)
  314. printf("OK\n");
  315. else {
  316. printf("FAILED\n");
  317. goto close;
  318. }
  319. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  320. UT_REGISTERS_NB + 1, tab_rp_registers);
  321. printf("* modbus_read_registers: ");
  322. if (rc == -1 && errno == EMBXILADD)
  323. printf("OK\n");
  324. else {
  325. printf("FAILED\n");
  326. goto close;
  327. }
  328. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  329. UT_INPUT_REGISTERS_NB + 1,
  330. tab_rp_registers);
  331. printf("* modbus_read_input_registers: ");
  332. if (rc == -1 && errno == EMBXILADD)
  333. printf("OK\n");
  334. else {
  335. printf("FAILED\n");
  336. goto close;
  337. }
  338. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  339. printf("* modbus_write_bit: ");
  340. if (rc == -1 && errno == EMBXILADD) {
  341. printf("OK\n");
  342. } else {
  343. printf("FAILED\n");
  344. goto close;
  345. }
  346. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  347. UT_BITS_NB, tab_rp_bits);
  348. printf("* modbus_write_coils: ");
  349. if (rc == -1 && errno == EMBXILADD) {
  350. printf("OK\n");
  351. } else {
  352. printf("FAILED\n");
  353. goto close;
  354. }
  355. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  356. UT_REGISTERS_NB, tab_rp_registers);
  357. printf("* modbus_write_registers: ");
  358. if (rc == -1 && errno == EMBXILADD) {
  359. printf("OK\n");
  360. } else {
  361. printf("FAILED\n");
  362. goto close;
  363. }
  364. /** TOO MANY DATA **/
  365. printf("\nTEST TOO MANY DATA ERROR:\n");
  366. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  367. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  368. printf("* modbus_read_bits: ");
  369. if (rc == -1 && errno == EMBMDATA) {
  370. printf("OK\n");
  371. } else {
  372. printf("FAILED\n");
  373. goto close;
  374. }
  375. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  376. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  377. printf("* modbus_read_input_bits: ");
  378. if (rc == -1 && errno == EMBMDATA) {
  379. printf("OK\n");
  380. } else {
  381. printf("FAILED\n");
  382. goto close;
  383. }
  384. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  385. MODBUS_MAX_READ_REGISTERS + 1,
  386. tab_rp_registers);
  387. printf("* modbus_read_registers: ");
  388. if (rc == -1 && errno == EMBMDATA) {
  389. printf("OK\n");
  390. } else {
  391. printf("FAILED\n");
  392. goto close;
  393. }
  394. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  395. MODBUS_MAX_READ_REGISTERS + 1,
  396. tab_rp_registers);
  397. printf("* modbus_read_input_registers: ");
  398. if (rc == -1 && errno == EMBMDATA) {
  399. printf("OK\n");
  400. } else {
  401. printf("FAILED\n");
  402. goto close;
  403. }
  404. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  405. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  406. printf("* modbus_write_bits: ");
  407. if (rc == -1 && errno == EMBMDATA) {
  408. printf("OK\n");
  409. } else {
  410. goto close;
  411. printf("FAILED\n");
  412. }
  413. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  414. MODBUS_MAX_WRITE_REGISTERS + 1,
  415. tab_rp_registers);
  416. printf("* modbus_write_registers: ");
  417. if (rc == -1 && errno == EMBMDATA) {
  418. printf("OK\n");
  419. } else {
  420. printf("FAILED\n");
  421. goto close;
  422. }
  423. /** SLAVE REPLY **/
  424. printf("\nTEST SLAVE REPLY:\n");
  425. modbus_set_slave(ctx, INVALID_SERVER_ID);
  426. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  427. UT_REGISTERS_NB, tab_rp_registers);
  428. if (use_backend == RTU) {
  429. const int RAW_REQ_LENGTH = 6;
  430. uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
  431. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  432. /* No response in RTU mode */
  433. printf("1/4-A No response from slave %d: ", INVALID_SERVER_ID);
  434. if (rc == -1 && errno == ETIMEDOUT) {
  435. printf("OK\n");
  436. } else {
  437. printf("FAILED\n");
  438. goto close;
  439. }
  440. /* Send an invalid query with a wrong slave ID */
  441. modbus_send_raw_request(ctx, raw_req,
  442. RAW_REQ_LENGTH * sizeof(uint8_t));
  443. rc = modbus_receive_confirmation(ctx, rsp);
  444. printf("1/4-B No response from slave %d with invalid request: ",
  445. INVALID_SERVER_ID);
  446. if (rc == -1 && errno == ETIMEDOUT) {
  447. printf("OK\n");
  448. } else {
  449. printf("FAILED (%d)\n", rc);
  450. goto close;
  451. }
  452. } else {
  453. /* Response in TCP mode */
  454. printf("1/4 Response from slave %d: ", 18);
  455. if (rc == UT_REGISTERS_NB) {
  456. printf("OK\n");
  457. } else {
  458. printf("FAILED\n");
  459. goto close;
  460. }
  461. }
  462. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  463. if (rc == -1) {
  464. printf("Invalid broacast address\n");
  465. goto close;
  466. }
  467. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  468. UT_REGISTERS_NB, tab_rp_registers);
  469. printf("2/4 Reply after a broadcast query: ");
  470. if (rc == UT_REGISTERS_NB) {
  471. printf("OK\n");
  472. } else {
  473. printf("FAILED\n");
  474. goto close;
  475. }
  476. /* Restore slave */
  477. if (use_backend == RTU) {
  478. modbus_set_slave(ctx, SERVER_ID);
  479. } else {
  480. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  481. }
  482. printf("3/4 Report slave ID: \n");
  483. /* tab_rp_bits is used to store bytes */
  484. rc = modbus_report_slave_id(ctx, tab_rp_bits);
  485. if (rc == -1) {
  486. printf("FAILED\n");
  487. goto close;
  488. }
  489. if (((use_backend == RTU) && (tab_rp_bits[0] == SERVER_ID))
  490. || tab_rp_bits[0] == 0xFF) {
  491. printf("OK\n");
  492. } else {
  493. printf("FAILED\n");
  494. goto close;
  495. }
  496. /* Save original timeout */
  497. modbus_get_response_timeout(ctx, &old_response_timeout);
  498. /* Define a new and too short timeout */
  499. response_timeout.tv_sec = 0;
  500. response_timeout.tv_usec = 0;
  501. modbus_set_response_timeout(ctx, &response_timeout);
  502. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  503. UT_REGISTERS_NB, tab_rp_registers);
  504. printf("4/4 Too short timeout: ");
  505. if (rc == -1 && errno == ETIMEDOUT) {
  506. printf("OK\n");
  507. } else {
  508. printf("FAILED (can fail on slow systems or Windows)\n");
  509. }
  510. /* Restore original timeout */
  511. modbus_set_response_timeout(ctx, &old_response_timeout);
  512. /* A wait and flush operation is done by the error recovery code of
  513. * libmodbus */
  514. /** BAD RESPONSE **/
  515. printf("\nTEST BAD RESPONSE ERROR:\n");
  516. /* Allocate only the required space */
  517. tab_rp_registers_bad = (uint16_t *) malloc(
  518. UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
  519. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  520. UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
  521. printf("* modbus_read_registers: ");
  522. if (rc == -1 && errno == EMBBADDATA) {
  523. printf("OK\n");
  524. } else {
  525. printf("FAILED\n");
  526. goto close;
  527. }
  528. free(tab_rp_registers_bad);
  529. /** MANUAL EXCEPTION **/
  530. printf("\nTEST MANUAL EXCEPTION:\n");
  531. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
  532. UT_REGISTERS_NB, tab_rp_registers);
  533. printf("* modbus_read_registers at special address: ");
  534. if (rc == -1 && errno == EMBXSBUSY) {
  535. printf("OK\n");
  536. } else {
  537. printf("FAILED\n");
  538. goto close;
  539. }
  540. /** RAW REQUEST */
  541. printf("\nTEST RAW REQUEST:\n");
  542. {
  543. const int RAW_REQ_LENGTH = 6;
  544. uint8_t raw_req[] = { (use_backend == RTU) ? SERVER_ID : 0xFF,
  545. 0x03, 0x00, 0x01, 0x0, 0x05 };
  546. int req_length;
  547. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  548. req_length = modbus_send_raw_request(ctx, raw_req,
  549. RAW_REQ_LENGTH * sizeof(uint8_t));
  550. printf("* modbus_send_raw_request: ");
  551. if ((use_backend == RTU && req_length == (RAW_REQ_LENGTH + 2)) ||
  552. ((use_backend == TCP || use_backend == TCP_PI) &&
  553. req_length == (RAW_REQ_LENGTH + 6))) {
  554. printf("OK\n");
  555. } else {
  556. printf("FAILED (%d)\n", req_length);
  557. goto close;
  558. }
  559. printf("* modbus_receive_confirmation: ");
  560. rc = modbus_receive_confirmation(ctx, rsp);
  561. if ((use_backend == RTU && rc == 15) ||
  562. ((use_backend == TCP || use_backend == TCP_PI) &&
  563. rc == 19)) {
  564. printf("OK\n");
  565. } else {
  566. printf("FAILED (%d)\n", rc);
  567. goto close;
  568. }
  569. }
  570. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  571. close:
  572. /* Free the memory */
  573. free(tab_rp_bits);
  574. free(tab_rp_registers);
  575. /* Close the connection */
  576. modbus_close(ctx);
  577. modbus_free(ctx);
  578. return 0;
  579. }