unit-test-server.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. int main(void)
  27. {
  28. int socket;
  29. modbus_t *ctx;
  30. modbus_mapping_t *mb_mapping;
  31. int rc;
  32. int i;
  33. ctx = modbus_new_tcp("127.0.0.1", 1502);
  34. modbus_set_debug(ctx, TRUE);
  35. modbus_set_error_recovery(ctx, TRUE);
  36. mb_mapping = modbus_mapping_new(UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
  37. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB_POINTS,
  38. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_POINTS,
  39. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
  40. if (mb_mapping == NULL) {
  41. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  42. modbus_strerror(errno));
  43. modbus_free(ctx);
  44. return -1;
  45. }
  46. /* Examples from PI_MODBUS_300.pdf.
  47. Only the read-only input values are assigned. */
  48. /** INPUT STATUS **/
  49. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  50. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB_POINTS,
  51. UT_INPUT_BITS_TAB);
  52. /** INPUT REGISTERS **/
  53. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  54. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  55. UT_INPUT_REGISTERS_TAB[i];;
  56. }
  57. socket = modbus_listen(ctx, 1);
  58. modbus_accept(ctx, &socket);
  59. for (;;) {
  60. uint8_t query[MODBUS_MAX_ADU_LENGTH_TCP];
  61. rc = modbus_receive(ctx, -1, query);
  62. if (rc > 0) {
  63. if (((query[HEADER_LENGTH_TCP + 3] << 8) + query[HEADER_LENGTH_TCP + 4])
  64. == UT_REGISTERS_NB_POINTS_SPECIAL) {
  65. /* Change the number of values (offset
  66. TCP = 6) */
  67. query[HEADER_LENGTH_TCP + 3] = 0;
  68. query[HEADER_LENGTH_TCP + 4] = UT_REGISTERS_NB_POINTS;
  69. }
  70. rc = modbus_reply(ctx, query, rc, mb_mapping);
  71. if (rc == -1) {
  72. return -1;
  73. }
  74. } else {
  75. /* Connection closed by the client or error */
  76. break;
  77. }
  78. }
  79. printf("Quit the loop: %s\n", modbus_strerror(errno));
  80. close(socket);
  81. modbus_mapping_free(mb_mapping);
  82. modbus_free(ctx);
  83. return 0;
  84. }