unit-test-server.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. RTU
  27. };
  28. int main(int argc, char*argv[])
  29. {
  30. int socket;
  31. modbus_t *ctx;
  32. modbus_mapping_t *mb_mapping;
  33. int rc;
  34. int i;
  35. int use_backend;
  36. uint8_t *query;
  37. int header_length;
  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", argv[0]);
  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. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  54. } else {
  55. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  56. modbus_set_slave(ctx, SERVER_ID);
  57. query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
  58. }
  59. header_length = modbus_get_header_length(ctx);
  60. modbus_set_debug(ctx, TRUE);
  61. modbus_set_error_recovery(ctx, TRUE);
  62. mb_mapping = modbus_mapping_new(
  63. UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
  64. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB_POINTS,
  65. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_POINTS,
  66. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
  67. if (mb_mapping == NULL) {
  68. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  69. modbus_strerror(errno));
  70. modbus_free(ctx);
  71. return -1;
  72. }
  73. /* Examples from PI_MODBUS_300.pdf.
  74. Only the read-only input values are assigned. */
  75. /** INPUT STATUS **/
  76. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  77. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB_POINTS,
  78. UT_INPUT_BITS_TAB);
  79. /** INPUT REGISTERS **/
  80. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  81. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  82. UT_INPUT_REGISTERS_TAB[i];;
  83. }
  84. if (use_backend == TCP) {
  85. socket = modbus_tcp_listen(ctx, 1);
  86. modbus_tcp_accept(ctx, &socket);
  87. } else {
  88. rc = modbus_connect(ctx);
  89. if (rc == -1) {
  90. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  91. modbus_free(ctx);
  92. return -1;
  93. }
  94. }
  95. for (;;) {
  96. rc = modbus_receive(ctx, -1, query);
  97. if (rc == -1) {
  98. /* Connection closed by the client or error */
  99. break;
  100. }
  101. /* Read holding registers */
  102. if (query[header_length] == 0x03) {
  103. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  104. == UT_REGISTERS_NB_POINTS_SPECIAL) {
  105. printf("Set an incorrect number of values\n");
  106. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  107. UT_REGISTERS_NB_POINTS);
  108. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  109. == UT_REGISTERS_ADDRESS_SPECIAL) {
  110. modbus_reply_exception(ctx, query,
  111. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  112. continue;
  113. }
  114. }
  115. rc = modbus_reply(ctx, query, rc, mb_mapping);
  116. if (rc == -1) {
  117. break;
  118. }
  119. }
  120. printf("Quit the loop: %s\n", modbus_strerror(errno));
  121. if (use_backend == TCP) {
  122. close(socket);
  123. }
  124. modbus_mapping_free(mb_mapping);
  125. free(query);
  126. modbus_free(ctx);
  127. return 0;
  128. }