modbus-private.h 3.8 KB

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