unit-test-client.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 address;
  38. int nb_points;
  39. int rc;
  40. float real;
  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. if (use_backend == RTU) {
  72. modbus_set_slave(ctx, SERVER_ID);
  73. }
  74. if (modbus_connect(ctx) == -1) {
  75. fprintf(stderr, "Connection failed: %s\n",
  76. modbus_strerror(errno));
  77. modbus_free(ctx);
  78. return -1;
  79. }
  80. /* Allocate and initialize the memory to store the bits */
  81. nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
  82. tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  83. memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
  84. /* Allocate and initialize the memory to store the registers */
  85. nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
  86. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  87. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  88. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  89. printf("** UNIT TESTING **\n");
  90. printf("\nTEST WRITE/READ:\n");
  91. /** COIL BITS **/
  92. /* Single */
  93. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
  94. printf("1/2 modbus_write_bit: ");
  95. if (rc == 1) {
  96. printf("OK\n");
  97. } else {
  98. printf("FAILED\n");
  99. goto close;
  100. }
  101. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
  102. printf("2/2 modbus_read_bits: ");
  103. if (rc != 1) {
  104. printf("FAILED (nb points %d)\n", rc);
  105. goto close;
  106. }
  107. if (tab_rp_bits[0] != ON) {
  108. printf("FAILED (%0X = != %0X)\n", tab_rp_bits[0], ON);
  109. goto close;
  110. }
  111. printf("OK\n");
  112. /* End single */
  113. /* Multiple bits */
  114. {
  115. uint8_t tab_value[UT_BITS_NB];
  116. modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
  117. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  118. UT_BITS_NB, tab_value);
  119. printf("1/2 modbus_write_bits: ");
  120. if (rc == UT_BITS_NB) {
  121. printf("OK\n");
  122. } else {
  123. printf("FAILED\n");
  124. goto close;
  125. }
  126. }
  127. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
  128. printf("2/2 modbus_read_bits: ");
  129. if (rc != UT_BITS_NB) {
  130. printf("FAILED (nb points %d)\n", rc);
  131. goto close;
  132. }
  133. i = 0;
  134. address = UT_BITS_ADDRESS;
  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. address = UT_INPUT_BITS_ADDRESS;
  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_read_and_write_registers(ctx,
  236. UT_REGISTERS_ADDRESS, UT_REGISTERS_NB,
  237. tab_rp_registers,
  238. UT_REGISTERS_ADDRESS + 1,
  239. UT_REGISTERS_NB - 1,
  240. tab_rp_registers);
  241. printf("4/5 modbus_read_and_write_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, 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, tab_rp_registers);
  329. printf("* modbus_read_input_registers: ");
  330. if (rc == -1 && errno == EMBXILADD)
  331. printf("OK\n");
  332. else {
  333. printf("FAILED\n");
  334. goto close;
  335. }
  336. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  337. printf("* modbus_write_bit: ");
  338. if (rc == -1 && errno == EMBXILADD) {
  339. printf("OK\n");
  340. } else {
  341. printf("FAILED\n");
  342. goto close;
  343. }
  344. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  345. UT_BITS_NB, tab_rp_bits);
  346. printf("* modbus_write_coils: ");
  347. if (rc == -1 && errno == EMBXILADD) {
  348. printf("OK\n");
  349. } else {
  350. printf("FAILED\n");
  351. goto close;
  352. }
  353. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  354. UT_REGISTERS_NB, tab_rp_registers);
  355. printf("* modbus_write_registers: ");
  356. if (rc == -1 && errno == EMBXILADD) {
  357. printf("OK\n");
  358. } else {
  359. printf("FAILED\n");
  360. goto close;
  361. }
  362. /** TOO MANY DATA **/
  363. printf("\nTEST TOO MANY DATA ERROR:\n");
  364. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  365. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  366. printf("* modbus_read_bits: ");
  367. if (rc == -1 && errno == EMBMDATA) {
  368. printf("OK\n");
  369. } else {
  370. printf("FAILED\n");
  371. goto close;
  372. }
  373. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  374. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  375. printf("* modbus_read_input_bits: ");
  376. if (rc == -1 && errno == EMBMDATA) {
  377. printf("OK\n");
  378. } else {
  379. printf("FAILED\n");
  380. goto close;
  381. }
  382. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  383. MODBUS_MAX_READ_REGISTERS + 1,
  384. tab_rp_registers);
  385. printf("* modbus_read_registers: ");
  386. if (rc == -1 && errno == EMBMDATA) {
  387. printf("OK\n");
  388. } else {
  389. printf("FAILED\n");
  390. goto close;
  391. }
  392. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  393. MODBUS_MAX_READ_REGISTERS + 1,
  394. tab_rp_registers);
  395. printf("* modbus_read_input_registers: ");
  396. if (rc == -1 && errno == EMBMDATA) {
  397. printf("OK\n");
  398. } else {
  399. printf("FAILED\n");
  400. goto close;
  401. }
  402. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  403. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  404. printf("* modbus_write_bits: ");
  405. if (rc == -1 && errno == EMBMDATA) {
  406. printf("OK\n");
  407. } else {
  408. goto close;
  409. printf("FAILED\n");
  410. }
  411. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  412. MODBUS_MAX_WRITE_REGISTERS + 1,
  413. tab_rp_registers);
  414. printf("* modbus_write_registers: ");
  415. if (rc == -1 && errno == EMBMDATA) {
  416. printf("OK\n");
  417. } else {
  418. printf("FAILED\n");
  419. goto close;
  420. }
  421. /** SLAVE REPLY **/
  422. printf("\nTEST SLAVE REPLY:\n");
  423. modbus_set_slave(ctx, 18);
  424. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  425. UT_REGISTERS_NB, tab_rp_registers);
  426. if (use_backend == RTU) {
  427. /* No response in RTU mode */
  428. printf("1/4 No response from slave %d: ", 18);
  429. if (rc == -1 && errno == ETIMEDOUT) {
  430. printf("OK\n");
  431. } else {
  432. printf("FAILED\n");
  433. goto close;
  434. }
  435. } else {
  436. /* Response in TCP mode */
  437. printf("1/4 Response from slave %d: ", 18);
  438. if (rc == UT_REGISTERS_NB) {
  439. printf("OK\n");
  440. } else {
  441. printf("FAILED\n");
  442. goto close;
  443. }
  444. }
  445. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  446. if (rc == -1) {
  447. printf("Invalid broacast address\n");
  448. goto close;
  449. }
  450. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  451. UT_REGISTERS_NB, tab_rp_registers);
  452. printf("2/4 Reply after a broadcast query: ");
  453. if (rc == UT_REGISTERS_NB) {
  454. printf("OK\n");
  455. } else {
  456. printf("FAILED\n");
  457. goto close;
  458. }
  459. /* Restore slave */
  460. if (use_backend == RTU) {
  461. modbus_set_slave(ctx, SERVER_ID);
  462. } else {
  463. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  464. }
  465. printf("3/4 Report slave ID: \n");
  466. /* tab_rp_bits is used to store bytes */
  467. rc = modbus_report_slave_id(ctx, tab_rp_bits);
  468. if (rc == -1) {
  469. printf("FAILED\n");
  470. goto close;
  471. }
  472. if (((use_backend == RTU) && (tab_rp_bits[0] == SERVER_ID))
  473. || tab_rp_bits[0] == 0xFF) {
  474. printf("OK\n");
  475. } else {
  476. printf("FAILED\n");
  477. goto close;
  478. }
  479. /* Save original timeout */
  480. modbus_get_response_timeout(ctx, &old_response_timeout);
  481. /* Define a new and too short timeout */
  482. response_timeout.tv_sec = 0;
  483. response_timeout.tv_usec = 0;
  484. modbus_set_response_timeout(ctx, &response_timeout);
  485. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  486. UT_REGISTERS_NB, tab_rp_registers);
  487. printf("4/4 Too short timeout: ");
  488. if (rc == -1 && errno == ETIMEDOUT) {
  489. printf("OK\n");
  490. } else {
  491. printf("FAILED\n");
  492. goto close;
  493. }
  494. /* Restore original timeout */
  495. modbus_set_response_timeout(ctx, &old_response_timeout);
  496. /* Wait for data before flushing */
  497. usleep(250000);
  498. modbus_flush(ctx);
  499. /** BAD RESPONSE **/
  500. printf("\nTEST BAD RESPONSE ERROR:\n");
  501. /* Allocate only the required space */
  502. tab_rp_registers_bad = (uint16_t *) malloc(
  503. UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
  504. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  505. UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
  506. printf("* modbus_read_registers: ");
  507. if (rc == -1 && errno == EMBBADDATA) {
  508. printf("OK\n");
  509. } else {
  510. printf("FAILED\n");
  511. goto close;
  512. }
  513. free(tab_rp_registers_bad);
  514. /** MANUAL EXCEPTION **/
  515. printf("\nTEST MANUAL EXCEPTION:\n");
  516. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
  517. UT_REGISTERS_NB, tab_rp_registers);
  518. printf("* modbus_read_registers at special address: ");
  519. if (rc == -1 && errno == EMBXSBUSY) {
  520. printf("OK\n");
  521. } else {
  522. printf("FAILED\n");
  523. goto close;
  524. }
  525. /** RAW REQUEST */
  526. printf("\nTEST RAW REQUEST:\n");
  527. {
  528. const int RAW_REQ_LENGTH = 6;
  529. uint8_t raw_req[] = { (use_backend == RTU) ? SERVER_ID : 0xFF,
  530. 0x03, 0x00, 0x01, 0x0, 0x05 };
  531. int req_length;
  532. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  533. req_length = modbus_send_raw_request(ctx, raw_req,
  534. RAW_REQ_LENGTH * sizeof(uint8_t));
  535. printf("* modbus_send_raw_request: ");
  536. if ((use_backend == RTU && req_length == (RAW_REQ_LENGTH + 2)) ||
  537. ((use_backend == TCP || use_backend == TCP_PI) &&
  538. req_length == (RAW_REQ_LENGTH + 6))) {
  539. printf("OK\n");
  540. } else {
  541. printf("FAILED (%d)\n", req_length);
  542. goto close;
  543. }
  544. printf("* modbus_receive_confirmation: ");
  545. rc = modbus_receive_confirmation(ctx, rsp);
  546. if ((use_backend == RTU && rc == 15) ||
  547. ((use_backend == TCP || use_backend == TCP_PI) &&
  548. rc == 19)) {
  549. printf("OK\n");
  550. } else {
  551. printf("FAILED (%d)\n", rc);
  552. goto close;
  553. }
  554. }
  555. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  556. close:
  557. /* Free the memory */
  558. free(tab_rp_bits);
  559. free(tab_rp_registers);
  560. /* Close the connection */
  561. modbus_close(ctx);
  562. modbus_free(ctx);
  563. return 0;
  564. }