modbus.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 REG_DEV_TYPE_ADDR (uint16_t)0x0000
  15. #define REG_VERSION_ADDR (uint16_t)0x0003
  16. #define REG_COLL_STAT_ADDR (uint16_t)0x0010
  17. #define REG_COLL_COUNT_ADDR (uint16_t)0x0011
  18. #define REG_COLL_SW_HIGH_STAT_ADDR (uint16_t)0x0012
  19. #define REG_COLL_SW_LOW_STAT_ADDR (uint16_t)0x0013
  20. typedef struct{
  21. uint8_t addr;
  22. uint8_t func;
  23. uint8_t regAddr[2];
  24. uint8_t data[MODBUS_DATA_SIZE];
  25. uint8_t dataSize;
  26. uint16_t crc;
  27. }__attribute__((packed))rtu_rec_s;
  28. typedef struct{
  29. uint8_t addr;
  30. uint8_t func;
  31. uint8_t dataSize;
  32. uint8_t data[MODBUS_DATA_SIZE];
  33. uint16_t crc;
  34. }__attribute__((packed))rtu_rd_s;
  35. typedef struct{
  36. uint8_t rtu_addr;
  37. uint16_t tx_size;
  38. uint16_t rx_size;
  39. uint8_t tx_buffer[MODBUS_DATA_SIZE];
  40. uint8_t rx_buffer[MODBUS_DATA_SIZE];
  41. rtu_rec_s rtu_rec;
  42. rtu_rd_s rtu_rd;
  43. bool modbusRx_stat;
  44. bool modbusTx_stat;
  45. }modbus_work_s;
  46. #ifdef __cplusplus
  47. }
  48. #endif
  49. #endif //__MODBUS_H__