unit-test-slave.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. int main(void)
  25. {
  26. int socket;
  27. modbus_param_t mb_param;
  28. modbus_mapping_t mb_mapping;
  29. int rc;
  30. int i;
  31. modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  32. modbus_set_debug(&mb_param, TRUE);
  33. modbus_set_error_recovery(&mb_param, TRUE);
  34. rc = modbus_mapping_new(&mb_mapping,
  35. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  36. UT_INPUT_STATUS_ADDRESS + UT_INPUT_STATUS_NB_POINTS,
  37. UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
  38. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
  39. if (rc == -1) {
  40. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  41. modbus_strerror(errno));
  42. return -1;
  43. }
  44. /* Examples from PI_MODBUS_300.pdf.
  45. Only the read-only input values are assigned. */
  46. /** INPUT STATUS **/
  47. set_bits_from_bytes(mb_mapping.tab_input_status,
  48. UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS,
  49. UT_INPUT_STATUS_TAB);
  50. /** INPUT REGISTERS **/
  51. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  52. mb_mapping.tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  53. UT_INPUT_REGISTERS_TAB[i];;
  54. }
  55. socket = modbus_slave_listen_tcp(&mb_param, 1);
  56. modbus_slave_accept_tcp(&mb_param, &socket);
  57. for (;;) {
  58. uint8_t query[MAX_MESSAGE_LENGTH];
  59. rc = modbus_slave_receive(&mb_param, -1, query);
  60. if (rc > 0) {
  61. if (((query[HEADER_LENGTH_TCP + 3] << 8) + query[HEADER_LENGTH_TCP + 4])
  62. == UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL) {
  63. /* Change the number of values (offset
  64. TCP = 6) */
  65. query[HEADER_LENGTH_TCP + 3] = 0;
  66. query[HEADER_LENGTH_TCP + 4] = UT_HOLDING_REGISTERS_NB_POINTS;
  67. }
  68. rc = modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
  69. if (rc == -1) {
  70. return -1;
  71. }
  72. } else {
  73. /* Connection closed by the client or error */
  74. break;
  75. }
  76. }
  77. printf("Quit the loop: %s\n", modbus_strerror(errno));
  78. close(socket);
  79. modbus_mapping_free(&mb_mapping);
  80. modbus_close(&mb_param);
  81. return 0;
  82. }