modbus.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #ifndef __MODBUS__H_
  3. #define __MODBUS__H_
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include "Arduino.h"
  8. #define MB_ID 0xAA
  9. /* ----------------------- Defines ------------------------------------------*/
  10. #define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
  11. #define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
  12. #define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
  13. #define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
  14. #define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
  15. #define MB_ADDRESS_BROADCAST (0) /*! Modbus broadcast address. */
  16. #define MB_ADDRESS_MIN (1) /*! Smallest possible slave address. */
  17. #define MB_ADDRESS_MAX (247) /*! Biggest possible slave address. */
  18. #define MB_FUNC_NONE (0)
  19. #define MB_FUNC_READ_COILS (1)
  20. #define MB_FUNC_READ_DISCRETE_INPUTS (2)
  21. #define MB_FUNC_WRITE_SINGLE_COIL (5)
  22. #define MB_FUNC_WRITE_MULTIPLE_COILS (15)
  23. #define MB_FUNC_READ_HOLDING_REGISTER (3)
  24. #define MB_FUNC_READ_INPUT_REGISTER (4)
  25. #define MB_FUNC_WRITE_REGISTER (6)
  26. #define MB_FUNC_WRITE_MULTIPLE_REGISTERS (16)
  27. #define MB_FUNC_READWRITE_MULTIPLE_REGISTERS (23)
  28. #define MB_FUNC_DIAG_READ_EXCEPTION (7)
  29. #define MB_FUNC_DIAG_DIAGNOSTIC (8)
  30. #define MB_FUNC_DIAG_GET_COM_EVENT_CNT (11)
  31. #define MB_FUNC_DIAG_GET_COM_EVENT_LOG (12)
  32. #define MB_FUNC_OTHER_REPORT_SLAVEID (17)
  33. #define MB_FUNC_ERROR (128)
  34. enum MB_FC {
  35. MB_FC_NONE = 0, /*!< null operator */
  36. MB_FC_READ_COILS = 1, /*!< FCT=1 -> read coils or digital outputs */
  37. MB_FC_READ_DISCRETE_INPUT = 2, /*!< FCT=2 -> read digital inputs */
  38. MB_FC_READ_REGISTERS = 3, /*!< FCT=3 -> read registers or analog outputs */
  39. MB_FC_READ_INPUT_REGISTER = 4, /*!< FCT=4 -> read analog inputs */
  40. MB_FC_WRITE_COIL = 5, /*!< FCT=5 -> write single coil or output */
  41. MB_FC_WRITE_REGISTER = 6, /*!< FCT=6 -> write single register */
  42. MB_FC_WRITE_MULTIPLE_COILS =
  43. 15, /*!< FCT=15 -> write multiple coils or outputs */
  44. MB_FC_WRITE_MULTIPLE_REGISTERS =
  45. 16 /*!< FCT=16 -> write multiple registers */
  46. };
  47. enum MB_STATUS {
  48. MB_OK = 0,
  49. MB_FRAME = -1,
  50. MB_BUFF_OVERFLOW = -2,
  51. MB_CRC_ERROR = -3,
  52. };
  53. typedef enum {
  54. MB_ENOERR, /*!< no error. */
  55. MB_ENOREG, /*!< illegal register address. */
  56. MB_EINVAL, /*!< illegal argument. */
  57. MB_EPORTERR, /*!< porting layer error. */
  58. MB_ENORES, /*!< insufficient resources. */
  59. MB_EIO, /*!< I/O error. */
  60. MB_EILLSTATE, /*!< protocol stack in illegal state. */
  61. MB_ETIMEDOUT /*!< timeout error occurred. */
  62. } eMBErrorCode;
  63. void mb_put_rec_data(uint8_t data);
  64. void mb_init(uint8_t id, uint32_t boudrate, volatile uint32_t *time_base);
  65. void mb_poll();
  66. void mb_send_frame(uint8_t *frame, uint16_t length);
  67. __attribute__((weak)) void mb_send_one_byte(uint8_t data);
  68. __attribute__((weak)) void mb_get_frame_cb(uint8_t rcvAddress, uint8_t *frame,
  69. uint16_t length);
  70. __attribute__((weak)) void mb_get_frame_error_cb(eMBErrorCode);
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif