modbus.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef __MODBUS_H__
  2. #define __MODBUS_H__
  3. #include "define.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define MODBUS_DATA_SIZE 512
  8. #define COLL_MAX_COUNT 32
  9. #define COLL_NOT_READY_STAT (uint16_t)0x0000
  10. #define COLL_READY_STAT (uint16_t)0x0001
  11. #define CMD_RD_REG (uint8_t)0x03
  12. #define CMD_WR_REG (uint8_t)0x06
  13. #define CMD_WR_MULTI_REG (uint8_t)0x10
  14. #define DEV_TYPE (uint16_t)0x0065
  15. #define REG_DEV_TYPE_ADDR (uint16_t)0x0000
  16. #define REG_VERSION_ADDR (uint16_t)0x0003
  17. #define REG_COLL_STAT_ADDR (uint16_t)0x0010
  18. #define REG_COLL_COUNT_ADDR (uint16_t)0x0011
  19. #define REG_COLL_SW_HIGH_STAT_ADDR (uint16_t)0x0012
  20. #define REG_COLL_SW_LOW_STAT_ADDR (uint16_t)0x0013
  21. typedef struct{
  22. uint8_t addr;
  23. uint8_t func;
  24. uint8_t regAddr[2];
  25. uint8_t regCount[2];
  26. uint8_t count;
  27. uint8_t *data;
  28. }__attribute__((packed))rtu_wr_multi_s;
  29. typedef struct{
  30. uint8_t addr;
  31. uint8_t func;
  32. uint8_t regAddr[2];
  33. uint8_t data[MODBUS_DATA_SIZE];
  34. uint8_t dataSize;
  35. uint16_t crc;
  36. }__attribute__((packed))rtu_rec_s;
  37. typedef struct{
  38. uint8_t addr;
  39. uint8_t func;
  40. uint8_t dataSize;
  41. uint8_t data[MODBUS_DATA_SIZE];
  42. uint16_t crc;
  43. }__attribute__((packed))rtu_rd_s;
  44. typedef struct{
  45. uint8_t rtu_addr;
  46. uint16_t tx_size;
  47. uint16_t rx_size;
  48. uint8_t tx_buffer[MODBUS_DATA_SIZE];
  49. uint8_t rx_buffer[MODBUS_DATA_SIZE];
  50. rtu_rec_s rtu_rec;
  51. rtu_rd_s rtu_rd;
  52. bool modbusRx_stat;
  53. bool modbusTx_stat;
  54. }modbus_work_s;
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif //__MODBUS_H__