unit-test-slave.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright © 2008 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 <modbus/modbus.h>
  22. #include "unit-test.h"
  23. int main(void)
  24. {
  25. int socket;
  26. modbus_param_t mb_param;
  27. modbus_mapping_t mb_mapping;
  28. int ret;
  29. int i;
  30. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  31. modbus_set_debug(&mb_param, TRUE);
  32. ret = modbus_mapping_new(&mb_mapping,
  33. UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
  34. UT_INPUT_STATUS_ADDRESS + UT_INPUT_STATUS_NB_POINTS,
  35. UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
  36. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
  37. if (ret == FALSE) {
  38. printf("Memory allocation failed\n");
  39. exit(1);
  40. }
  41. /* Examples from PI_MODBUS_300.pdf */
  42. /** COIL STATUS **/
  43. set_bits_from_bytes(mb_mapping.tab_coil_status,
  44. UT_COIL_STATUS_ADDRESS, UT_COIL_STATUS_NB_POINTS,
  45. UT_COIL_STATUS_TAB);
  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. /** HOLDING REGISTERS **/
  51. for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
  52. mb_mapping.tab_holding_registers[UT_HOLDING_REGISTERS_ADDRESS+i] =
  53. UT_HOLDING_REGISTERS_TAB[i];;
  54. }
  55. /** INPUT REGISTERS **/
  56. for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
  57. mb_mapping.tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  58. UT_INPUT_REGISTERS_TAB[i];;
  59. }
  60. socket = modbus_init_listen_tcp(&mb_param);
  61. while (1) {
  62. uint8_t query[MAX_MESSAGE_LENGTH];
  63. int query_size;
  64. ret = modbus_listen(&mb_param, query, &query_size);
  65. if (ret == 0) {
  66. manage_query(&mb_param, query, query_size, &mb_mapping);
  67. } else if (ret == CONNECTION_CLOSED) {
  68. /* Connection closed by the client, end of server */
  69. break;
  70. } else {
  71. printf("Error in modbus_listen (%d)\n", ret);
  72. }
  73. }
  74. close(socket);
  75. modbus_mapping_free(&mb_mapping);
  76. modbus_close(&mb_param);
  77. return 0;
  78. }