unit-test-server.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. modbus_set_error_recovery(ctx, TRUE);
  68. mb_mapping = modbus_mapping_new(
  69. UT_BITS_ADDRESS + UT_BITS_NB,
  70. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
  71. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  72. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
  73. if (mb_mapping == NULL) {
  74. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  75. modbus_strerror(errno));
  76. modbus_free(ctx);
  77. return -1;
  78. }
  79. /* Examples from PI_MODBUS_300.pdf.
  80. Only the read-only input values are assigned. */
  81. /** INPUT STATUS **/
  82. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  83. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
  84. UT_INPUT_BITS_TAB);
  85. /** INPUT REGISTERS **/
  86. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  87. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  88. UT_INPUT_REGISTERS_TAB[i];;
  89. }
  90. if (use_backend == TCP) {
  91. socket = modbus_tcp_listen(ctx, 1);
  92. modbus_tcp_accept(ctx, &socket);
  93. } else if (use_backend == TCP_PI) {
  94. socket = modbus_tcp_pi_listen(ctx, 1);
  95. modbus_tcp_pi_accept(ctx, &socket);
  96. } else {
  97. rc = modbus_connect(ctx);
  98. if (rc == -1) {
  99. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  100. modbus_free(ctx);
  101. return -1;
  102. }
  103. }
  104. for (;;) {
  105. rc = modbus_receive(ctx, query);
  106. if (rc == -1) {
  107. /* Connection closed by the client or error */
  108. break;
  109. }
  110. /* Read holding registers */
  111. if (query[header_length] == 0x03) {
  112. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  113. == UT_REGISTERS_NB_SPECIAL) {
  114. printf("Set an incorrect number of values\n");
  115. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  116. UT_REGISTERS_NB_SPECIAL - 1);
  117. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  118. == UT_REGISTERS_ADDRESS_SPECIAL) {
  119. printf("Reply to this special register address by an exception\n");
  120. modbus_reply_exception(ctx, query,
  121. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  122. continue;
  123. }
  124. }
  125. rc = modbus_reply(ctx, query, rc, mb_mapping);
  126. if (rc == -1) {
  127. break;
  128. }
  129. }
  130. printf("Quit the loop: %s\n", modbus_strerror(errno));
  131. if (use_backend == TCP) {
  132. close(socket);
  133. }
  134. modbus_mapping_free(mb_mapping);
  135. free(query);
  136. modbus_free(ctx);
  137. return 0;
  138. }