test-modbus-slave.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * Licensed under the GNU General Public License Version 2
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <modbus/modbus.h>
  24. int main(void)
  25. {
  26. const int nb_coil_status = 500;
  27. const int nb_input_status = 500;
  28. const int nb_input_registers = 500;
  29. const int nb_holding_registers = 500;
  30. int socket;
  31. modbus_param_t mb_param;
  32. modbus_mapping_t mb_mapping;
  33. int ret;
  34. int i;
  35. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  36. modbus_set_debug(&mb_param, TRUE);
  37. modbus_mapping_new(&mb_mapping,
  38. nb_coil_status, nb_input_status,
  39. nb_input_registers, nb_holding_registers);
  40. /* Examples from PI_MODBUS_300.pdf */
  41. /* Coil status */
  42. mb_mapping.tab_coil_status[26] = ON;
  43. mb_mapping.tab_coil_status[25] = ON;
  44. mb_mapping.tab_coil_status[24] = OFF;
  45. mb_mapping.tab_coil_status[23] = OFF;
  46. mb_mapping.tab_coil_status[22] = ON;
  47. mb_mapping.tab_coil_status[21] = ON;
  48. mb_mapping.tab_coil_status[20] = OFF;
  49. mb_mapping.tab_coil_status[19] = ON;
  50. /* Input status */
  51. mb_mapping.tab_input_status[203] = ON;
  52. mb_mapping.tab_input_status[202] = OFF;
  53. mb_mapping.tab_input_status[201] = ON;
  54. mb_mapping.tab_input_status[200] = OFF;
  55. mb_mapping.tab_input_status[199] = ON;
  56. mb_mapping.tab_input_status[198] = ON;
  57. mb_mapping.tab_input_status[197] = OFF;
  58. mb_mapping.tab_input_status[196] = OFF;
  59. mb_mapping.tab_input_status[211] = ON;
  60. mb_mapping.tab_input_status[210] = ON;
  61. mb_mapping.tab_input_status[209] = OFF;
  62. mb_mapping.tab_input_status[208] = ON;
  63. mb_mapping.tab_input_status[207] = ON;
  64. mb_mapping.tab_input_status[206] = OFF;
  65. mb_mapping.tab_input_status[205] = ON;
  66. mb_mapping.tab_input_status[204] = ON;
  67. /* Incomplete byte */
  68. mb_mapping.tab_input_status[217] = ON;
  69. mb_mapping.tab_input_status[216] = ON;
  70. mb_mapping.tab_input_status[215] = OFF;
  71. mb_mapping.tab_input_status[214] = ON;
  72. mb_mapping.tab_input_status[213] = OFF;
  73. mb_mapping.tab_input_status[212] = ON;
  74. /* Holding registers */
  75. mb_mapping.tab_holding_registers[107] = 0x022B;
  76. mb_mapping.tab_holding_registers[108] = 0x0000;
  77. mb_mapping.tab_holding_registers[109] = 0x0064;
  78. /* Input registers */
  79. mb_mapping.tab_input_registers[8] = 0x000A;
  80. socket = modbus_init_listen_tcp(&mb_param);
  81. i = 0;
  82. while (i++ < 5) {
  83. unsigned char query[MAX_PACKET_SIZE];
  84. int query_size;
  85. ret = modbus_listen(&mb_param, query, &query_size);
  86. if (ret == 0)
  87. manage_query(&mb_param, query, query_size, &mb_mapping);
  88. else
  89. printf("Error in modbus_listen (%d)\n", ret);
  90. }
  91. close(socket);
  92. modbus_mapping_free(&mb_mapping);
  93. modbus_close(&mb_param);
  94. return 0;
  95. }