bandwidth-slave-one.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. int main(void)
  24. {
  25. int socket;
  26. modbus_param_t mb_param;
  27. modbus_mapping_t mb_mapping;
  28. int rc;
  29. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  30. rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
  31. if (rc == -1) {
  32. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  33. modbus_strerror(errno));
  34. return -1;
  35. }
  36. socket = modbus_slave_listen_tcp(&mb_param, 1);
  37. modbus_slave_accept_tcp(&mb_param, &socket);
  38. for(;;) {
  39. uint8_t query[MAX_MESSAGE_LENGTH];
  40. rc = modbus_slave_receive(&mb_param, -1, query);
  41. if (rc >= 0) {
  42. modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
  43. } else {
  44. /* Connection closed by the client or server */
  45. break;
  46. }
  47. }
  48. printf("Quit the loop: %s\n", modbus_strerror(errno));
  49. close(socket);
  50. modbus_mapping_free(&mb_mapping);
  51. modbus_close(&mb_param);
  52. return 0;
  53. }