unit-test-server.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. int socket;
  32. modbus_t *ctx;
  33. modbus_mapping_t *mb_mapping;
  34. int rc;
  35. int i;
  36. int use_backend;
  37. uint8_t *query;
  38. int header_length;
  39. if (argc > 1) {
  40. if (strcmp(argv[1], "tcp") == 0) {
  41. use_backend = TCP;
  42. } else if (strcmp(argv[1], "tcppi") == 0) {
  43. use_backend = TCP_PI;
  44. } else if (strcmp(argv[1], "rtu") == 0) {
  45. use_backend = RTU;
  46. } else {
  47. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
  48. return -1;
  49. }
  50. } else {
  51. /* By default */
  52. use_backend = TCP;
  53. }
  54. if (use_backend == TCP) {
  55. ctx = modbus_new_tcp("127.0.0.1", 1502);
  56. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  57. } else if (use_backend == TCP_PI) {
  58. ctx = modbus_new_tcp_pi("::0", "1502");
  59. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  60. } else {
  61. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  62. modbus_set_slave(ctx, SERVER_ID);
  63. query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
  64. }
  65. header_length = modbus_get_header_length(ctx);
  66. modbus_set_debug(ctx, TRUE);
  67. mb_mapping = modbus_mapping_new(
  68. UT_BITS_ADDRESS + UT_BITS_NB,
  69. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
  70. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  71. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
  72. if (mb_mapping == NULL) {
  73. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  74. modbus_strerror(errno));
  75. modbus_free(ctx);
  76. return -1;
  77. }
  78. /* Examples from PI_MODBUS_300.pdf.
  79. Only the read-only input values are assigned. */
  80. /** INPUT STATUS **/
  81. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  82. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
  83. UT_INPUT_BITS_TAB);
  84. /** INPUT REGISTERS **/
  85. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  86. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  87. UT_INPUT_REGISTERS_TAB[i];;
  88. }
  89. if (use_backend == TCP) {
  90. socket = modbus_tcp_listen(ctx, 1);
  91. modbus_tcp_accept(ctx, &socket);
  92. } else if (use_backend == TCP_PI) {
  93. socket = modbus_tcp_pi_listen(ctx, 1);
  94. modbus_tcp_pi_accept(ctx, &socket);
  95. } else {
  96. rc = modbus_connect(ctx);
  97. if (rc == -1) {
  98. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  99. modbus_free(ctx);
  100. return -1;
  101. }
  102. }
  103. for (;;) {
  104. do {
  105. rc = modbus_receive(ctx, query);
  106. /* Filtered queries return 0 */
  107. } while (rc == 0);
  108. if (rc == -1) {
  109. /* Connection closed by the client or error */
  110. break;
  111. }
  112. /* Read holding registers */
  113. if (query[header_length] == 0x03) {
  114. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  115. == UT_REGISTERS_NB_SPECIAL) {
  116. printf("Set an incorrect number of values\n");
  117. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  118. UT_REGISTERS_NB_SPECIAL - 1);
  119. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  120. == UT_REGISTERS_ADDRESS_SPECIAL) {
  121. printf("Reply to this special register address by an exception\n");
  122. modbus_reply_exception(ctx, query,
  123. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  124. continue;
  125. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  126. == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
  127. const int RAW_REQ_LENGTH = 5;
  128. uint8_t raw_req[] = {
  129. (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  130. 0x03,
  131. 0x02, 0x00, 0x00
  132. };
  133. printf("Reply with an invalid TID or slave\n");
  134. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  135. continue;
  136. }
  137. }
  138. rc = modbus_reply(ctx, query, rc, mb_mapping);
  139. if (rc == -1) {
  140. break;
  141. }
  142. }
  143. printf("Quit the loop: %s\n", modbus_strerror(errno));
  144. if (use_backend == TCP) {
  145. close(socket);
  146. }
  147. modbus_mapping_free(mb_mapping);
  148. free(query);
  149. modbus_free(ctx);
  150. return 0;
  151. }