unit-test-server.c 4.1 KB

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