bandwidth-slave-many-up.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright © 2009-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 <signal.h>
  23. #include <modbus.h>
  24. #define SLAVE 0x11
  25. #define NB_CONNECTION 5
  26. int slave_socket;
  27. modbus_mapping_t mb_mapping;
  28. static void close_sigint(int dummy)
  29. {
  30. modbus_slave_close_tcp(slave_socket);
  31. modbus_mapping_free(&mb_mapping);
  32. exit(dummy);
  33. }
  34. int main(void)
  35. {
  36. int master_socket;
  37. modbus_param_t mb_param;
  38. int rc;
  39. fd_set refset;
  40. fd_set rdset;
  41. /* Maximum file descriptor number */
  42. int fdmax;
  43. modbus_init_tcp(&mb_param, "127.0.0.1", 1502, SLAVE);
  44. rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
  45. if (rc == -1) {
  46. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  47. modbus_strerror(errno));
  48. return -1;
  49. }
  50. slave_socket = modbus_slave_listen_tcp(&mb_param, NB_CONNECTION);
  51. signal(SIGINT, close_sigint);
  52. /* Clear the reference set of socket */
  53. FD_ZERO(&refset);
  54. /* Add the slave socket */
  55. FD_SET(slave_socket, &refset);
  56. /* Keep track of the max file descriptor */
  57. fdmax = slave_socket;
  58. for (;;) {
  59. rdset = refset;
  60. if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
  61. perror("Slave select() failure.");
  62. close_sigint(1);
  63. }
  64. /* Run through the existing connections looking for data to be
  65. * read */
  66. for (master_socket = 0; master_socket <= fdmax; master_socket++) {
  67. if (FD_ISSET(master_socket, &rdset)) {
  68. if (master_socket == slave_socket) {
  69. /* A client is asking a new connection */
  70. socklen_t addrlen;
  71. struct sockaddr_in clientaddr;
  72. int newfd;
  73. /* Handle new connections */
  74. addrlen = sizeof(clientaddr);
  75. memset(&clientaddr, 0, sizeof(clientaddr));
  76. newfd = accept(slave_socket, (struct sockaddr *)&clientaddr, &addrlen);
  77. if (newfd == -1) {
  78. perror("Server accept() error");
  79. } else {
  80. FD_SET(newfd, &refset);
  81. if (newfd > fdmax) {
  82. /* Keep track of the maximum */
  83. fdmax = newfd;
  84. }
  85. printf("New connection from %s:%d on socket %d\n",
  86. inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
  87. }
  88. } else {
  89. /* An already connected master has sent a new query */
  90. uint8_t query[MAX_MESSAGE_LENGTH];
  91. rc = modbus_slave_receive(&mb_param, master_socket, query);
  92. if (rc != -1) {
  93. modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
  94. } else {
  95. /* Connection closed by the client, end of server */
  96. printf("Connection closed on socket %d\n", master_socket);
  97. modbus_slave_close_tcp(master_socket);
  98. /* Remove from reference set */
  99. FD_CLR(master_socket, &refset);
  100. if (master_socket == fdmax) {
  101. fdmax--;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return 0;
  109. }