modbus-private.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright © 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 Lesser 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 Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _MODBUS_PRIVATE_H_
  18. #define _MODBUS_PRIVATE_H_
  19. #include "modbus.h"
  20. MODBUS_BEGIN_DECLS
  21. /* It's not really the minimal length (the real one is report slave ID
  22. * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
  23. * communications to read many values or write a single one.
  24. * Maximum between :
  25. * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
  26. * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
  27. */
  28. #define _MIN_REQ_LENGTH 12
  29. #define _REPORT_SLAVE_ID_LENGTH 75
  30. #define _MODBUS_EXCEPTION_RSP_LENGTH 5
  31. /* Time out between trames in microsecond */
  32. #define _TIME_OUT_BEGIN_OF_TRAME 500000
  33. #define _TIME_OUT_END_OF_TRAME 500000
  34. /* Function codes */
  35. #define _FC_READ_COILS 0x01
  36. #define _FC_READ_DISCRETE_INPUTS 0x02
  37. #define _FC_READ_HOLDING_REGISTERS 0x03
  38. #define _FC_READ_INPUT_REGISTERS 0x04
  39. #define _FC_WRITE_SINGLE_COIL 0x05
  40. #define _FC_WRITE_SINGLE_REGISTER 0x06
  41. #define _FC_READ_EXCEPTION_STATUS 0x07
  42. #define _FC_WRITE_MULTIPLE_COILS 0x0F
  43. #define _FC_WRITE_MULTIPLE_REGISTERS 0x10
  44. #define _FC_REPORT_SLAVE_ID 0x11
  45. #define _FC_READ_AND_WRITE_REGISTERS 0x17
  46. typedef enum {
  47. _MODBUS_BACKEND_TYPE_RTU=0,
  48. _MODBUS_BACKEND_TYPE_TCP
  49. } modbus_bakend_type_t;
  50. /* This structure reduces the number of params in functions and so
  51. * optimizes the speed of execution (~ 37%). */
  52. typedef struct _sft {
  53. int slave;
  54. int function;
  55. int t_id;
  56. } sft_t;
  57. typedef struct _modbus_backend {
  58. unsigned int backend_type;
  59. unsigned int header_length;
  60. unsigned int checksum_length;
  61. unsigned int max_adu_length;
  62. int (*set_slave) (modbus_t *ctx, int slave);
  63. int (*build_request_basis) (modbus_t *ctx, int function, int addr,
  64. int nb, uint8_t *req);
  65. int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
  66. int (*prepare_response_tid) (const uint8_t *req, int *req_length);
  67. int (*send_msg_pre) (uint8_t *req, int req_length);
  68. ssize_t (*send) (int s, const uint8_t *req, int req_length);
  69. ssize_t (*recv) (int s, uint8_t *rsp, int rsp_length);
  70. int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
  71. const int msg_length);
  72. int (*connect) (modbus_t *ctx);
  73. void (*close) (modbus_t *ctx);
  74. int (*flush) (modbus_t *ctx);
  75. int (*listen) (modbus_t *ctx, int nb_connection);
  76. int (*accept) (modbus_t *ctx, int *socket);
  77. int (*filter_request) (modbus_t *ctx, int slave);
  78. } modbus_backend_t;
  79. struct _modbus {
  80. /* Slave address */
  81. int slave;
  82. /* Socket or file descriptor */
  83. int s;
  84. int debug;
  85. int error_recovery;
  86. struct timeval timeout_begin;
  87. struct timeval timeout_end;
  88. const modbus_backend_t *backend;
  89. void *backend_data;
  90. };
  91. void _modbus_init_common(modbus_t *ctx);
  92. MODBUS_END_DECLS
  93. #endif /* _MODBUS_PRIVATE_H_ */