unit-test-server.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /* Copied from modbus-private.h */
  25. #define HEADER_LENGTH_TCP 7
  26. enum {
  27. TCP,
  28. RTU
  29. };
  30. int main(int argc, char*argv[])
  31. {
  32. int socket;
  33. modbus_t *ctx;
  34. modbus_mapping_t *mb_mapping;
  35. int rc;
  36. int i;
  37. int use_backend;
  38. if (argc > 1) {
  39. if (strcmp(argv[1], "tcp") == 0) {
  40. use_backend = TCP;
  41. } else if (strcmp(argv[1], "rtu") == 0) {
  42. use_backend = RTU;
  43. } else {
  44. printf("Usage:\n %s [tcp|rtu] - Modbus server for unit testing\n\n");
  45. return -1;
  46. }
  47. } else {
  48. /* By default */
  49. use_backend = TCP;
  50. }
  51. if (use_backend == TCP) {
  52. ctx = modbus_new_tcp("127.0.0.1", 1502);
  53. } else {
  54. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  55. modbus_set_slave(ctx, SERVER_ID);
  56. }
  57. modbus_set_debug(ctx, TRUE);
  58. modbus_set_error_recovery(ctx, TRUE);
  59. mb_mapping = modbus_mapping_new(
  60. UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
  61. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB_POINTS,
  62. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_POINTS,
  63. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
  64. if (mb_mapping == NULL) {
  65. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  66. modbus_strerror(errno));
  67. modbus_free(ctx);
  68. return -1;
  69. }
  70. /* Examples from PI_MODBUS_300.pdf.
  71. Only the read-only input values are assigned. */
  72. /** INPUT STATUS **/
  73. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  74. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB_POINTS,
  75. UT_INPUT_BITS_TAB);
  76. /** INPUT REGISTERS **/
  77. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  78. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  79. UT_INPUT_REGISTERS_TAB[i];;
  80. }
  81. if (use_backend == TCP) {
  82. socket = modbus_tcp_listen(ctx, 1);
  83. modbus_tcp_accept(ctx, &socket);
  84. } else {
  85. rc = modbus_connect(ctx);
  86. if (rc == -1) {
  87. fprintf(stderr, "Unable to connect\n", modbus_strerror(errno));
  88. modbus_free(ctx);
  89. return -1;
  90. }
  91. }
  92. for (;;) {
  93. uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
  94. rc = modbus_receive(ctx, -1, query);
  95. if (rc > 0) {
  96. if (((query[HEADER_LENGTH_TCP + 3] << 8) +
  97. query[HEADER_LENGTH_TCP + 4])
  98. == UT_REGISTERS_NB_POINTS_SPECIAL) {
  99. /* Change the number of values (offset
  100. TCP = 6) */
  101. query[HEADER_LENGTH_TCP + 3] = 0;
  102. query[HEADER_LENGTH_TCP + 4] = UT_REGISTERS_NB_POINTS;
  103. }
  104. rc = modbus_reply(ctx, query, rc, mb_mapping);
  105. if (rc == -1) {
  106. return -1;
  107. }
  108. } else {
  109. /* Connection closed by the client or error */
  110. break;
  111. }
  112. }
  113. printf("Quit the loop: %s\n", modbus_strerror(errno));
  114. if (use_backend == TCP) {
  115. close(socket);
  116. }
  117. modbus_mapping_free(mb_mapping);
  118. modbus_free(ctx);
  119. return 0;
  120. }