bench-bandwidth-slave.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. int main(void)
  23. {
  24. int socket;
  25. modbus_param_t mb_param;
  26. modbus_mapping_t mb_mapping;
  27. int ret;
  28. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  29. ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
  30. if (ret == FALSE) {
  31. printf("Memory allocation failed\n");
  32. exit(1);
  33. }
  34. socket = modbus_init_listen_tcp(&mb_param);
  35. while (1) {
  36. uint8_t query[MAX_MESSAGE_LENGTH];
  37. int query_size;
  38. ret = modbus_listen(&mb_param, query, &query_size);
  39. if (ret == 0) {
  40. manage_query(&mb_param, query, query_size, &mb_mapping);
  41. } else if (ret == CONNECTION_CLOSED) {
  42. /* Connection closed by the client, end of server */
  43. break;
  44. } else {
  45. printf("Error in modbus_listen (%d)\n", ret);
  46. }
  47. }
  48. close(socket);
  49. modbus_mapping_free(&mb_mapping);
  50. modbus_close(&mb_param);
  51. return 0;
  52. }