modbmst.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // modbmst.h :
  2. //
  3. #if !defined(AGD_MODBMST_H__7E9090EE_6B4D_4499_8080_03241FAC527B__INCLUDED_)
  4. #define AGD_MODBMST_H__7E9090EE_6B4D_4499_8080_03241FAC527B__INCLUDED_
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <gfa/gfamininetmst.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif // __cplusplus
  11. /////////////////////////////////////////////////////////////////////////////
  12. // modbmst.h - Declarations:
  13. #define MB_FUNC_READ_COIL_STATUS ((uint8_t)0x01)
  14. #define MB_FUNC_READ_INPUT_STATUS ((uint8_t)0x02)
  15. #define MB_FUNC_READ_HOLDING_REGISTERS ((uint8_t)0x03)
  16. #define MB_FUNC_READ_INPUT_REGISTERS ((uint8_t)0x04)
  17. #define MB_FUNC_FORCE_SINGLE_COIL ((uint8_t)0x05)
  18. #define MB_FUNC_PRESET_SINGLE_REGISTER ((uint8_t)0x06)
  19. #define MB_FUNC_READ_EXCEPTION_STATUS ((uint8_t)0x07)
  20. #define MB_FUNC_DIAGNOSTIC ((uint8_t)0x08)
  21. #define MB_FUNC_FETCH_COMM_EVENT_CTR ((uint8_t)0x0B)
  22. #define MB_FUNC_FETCH_COMM_EVENT_LOG ((uint8_t)0x0C)
  23. #define MB_FUNC_FORCE_MULTIPLE_COILS ((uint8_t)0x0F)
  24. #define MB_FUNC_WRITE_MULTIPLE_REGISTERS ((uint8_t)0x10)
  25. #define MB_FUNC_REPORT_SLAVE_ID ((uint8_t)0x11)
  26. #define MB_FUNC_READ_GENERAL_REFERENCE ((uint8_t)0x14)
  27. #define MB_FUNC_WRITE_GENERAL_REFERENCE ((uint8_t)0x15)
  28. #define MB_FUNC_MASK_WRITE_4X_REGISTER ((uint8_t)0x16)
  29. #define MB_FUNC_READ_WRITE_4X_REGISTERS ((uint8_t)0x17)
  30. #define MB_FUNC_READ_FIFO_QUEUE ((uint8_t)0x18)
  31. #define MB_SUBFUNC_RETURN_QUERY_DATA ((uint16_t)0x00)
  32. #define MB_SUBFUNC_RESTART_COMM_OPTION ((uint16_t)0x01)
  33. #define MB_SUBFUNC_RETURN_DIAGNOSTIC_REGISTER ((uint16_t)0x02)
  34. #define MB_SUBFUNC_FORCE_LISTEN_ONLY_MODE ((uint16_t)0x04)
  35. #define MB_SUBFUNC_CLEAR_CTRS_AND_DIAGNOSTIC_REG ((uint16_t)0x0A)
  36. #define MB_SUBFUNC_RETURN_BUS_MESSAGE_COUNT ((uint16_t)0x0B)
  37. #define MB_SUBFUNC_RETURN_BUS_COMM_ERROR_COUNT ((uint16_t)0x0C)
  38. #define MB_SUBFUNC_RETURN_BUS_EXCEPTION_ERROR_COUNT ((uint16_t)0x0D)
  39. #define MB_SUBFUNC_RETURN_SLAVE_MESSAGE_COUNT ((uint16_t)0x0E)
  40. #define MB_SUBFUNC_RETURN_SLAVE_NO_RESPONSE_COUNT ((uint16_t)0x0F)
  41. #define MB_SUBFUNC_RETURN_SLAVE_NAK_COUNT ((uint16_t)0x10)
  42. #define MB_SUBFUNC_RETURN_SLAVE_BUSY_COUNT ((uint16_t)0x11)
  43. #define MB_SUBFUNC_RETURN_BUS_CHAR_OVERRUN_COUNT ((uint16_t)0x12)
  44. #define MB_SUBFUNC_RETURN_OVERRUN_ERROR_COUNT ((uint16_t)0x13)
  45. #define MB_SUBFUNC_CLEAR_OVERRUN_COUNTER_AND_FLAG ((uint16_t)0x14)
  46. #define MB_FUNC_ERROR_FLAG ((uint8_t)0x80)
  47. #define MB_ERROR_ILLEGAL_FUNCTION ((uint8_t)0x01)
  48. #define MB_ERROR_ILLEGAL_DATA_ADDRESS ((uint8_t)0x02)
  49. #define MB_ERROR_ILLEGAL_DATA_VALUE ((uint8_t)0x03)
  50. #define MB_ERROR_SLAVE_DEVICE_FAILURE ((uint8_t)0x04)
  51. #define MB_ERROR_ACKNOWLEDGE ((uint8_t)0x05)
  52. #define MB_ERROR_SLAVE_DEVICE_BUSY ((uint8_t)0x06)
  53. #define MB_ERROR_NEGATIVE_ACKNOWLEDGE ((uint8_t)0x07)
  54. #define MB_ERROR_MEMORY_PARITY_ERROR ((uint8_t)0x08)
  55. #define MODBUS_BROADCAST_ADDRESS 0
  56. #define MODBUS_MAX_SLAVE_ID 247
  57. #define MODBUS_MAX_READ_BITS 2000
  58. #define MODBUS_MAX_WRITE_BITS 1968
  59. #define MODBUS_MAX_READ_REGISTERS 125
  60. #define MODBUS_MAX_WRITE_REGISTERS 123
  61. #define MODBUS_MAX_RW_WRITE_REGISTERS 121
  62. #define MODBUS_MAX_DATA_PAYLOAD 252
  63. #define MODBUS_IS_VALID_SLAVE_ID(id) (((id) != MODBUS_BROADCAST_ADDRESS) && ((id) <= MODBUS_MAX_SLAVE_ID))
  64. /////////////////////////////////////////////////////////////////////////////
  65. typedef struct _MODBUS_RTU_ADU
  66. {
  67. uint8_t slvID;
  68. uint8_t func;
  69. union
  70. {
  71. uint8_t b[254];
  72. uint16_t w[127];
  73. };
  74. }__attribute__ ((__aligned__(2),__packed__)) MODBUS_RTU_ADU, *LPMODBUS_RTU_ADU;
  75. typedef const MODBUS_RTU_ADU *LPCMODBUS_RTU_ADU;
  76. /////////////////////////////////////////////////////////////////////////////
  77. typedef void *HGFAMBMST;
  78. /////////////////////////////////////////////////////////////////////////////
  79. HGFAMBMST GfaMbMstOpen(HGFAMINEMST hMiNeMst, uint32_t nBaudrate, uint8_t parity, uint16_t nModbusCtrlReg);
  80. void GfaMbMstClose(HGFAMBMST hMst);
  81. ssize_t GfaMbMstSendADU(HGFAMBMST hMst, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu);
  82. ssize_t GfaMbMstRecvADU(HGFAMBMST hMst, LPMODBUS_RTU_ADU pAdu, size_t nCbAdu);
  83. ssize_t GfaMbMstReadHoldingRegisters(HGFAMBMST hMst, uint8_t slvID, uint16_t nRegStart, uint16_t nRegCount, void *pRegs);
  84. ssize_t GfaMbMstWriteMultipleRegisters(HGFAMBMST hMst, uint8_t slvID, uint16_t nRegStart, uint16_t nRegCount, const void *pRegs);
  85. void GfaMbMstWaitFrameDelay(HGFAMBMST hMst);
  86. int GfaMbMstPing(HGFAMBMST hMst, uint8_t slvID);
  87. int GfaMbMstBootloaderExecute(HGFAMBMST hMst, uint8_t slvID);
  88. int GfaMbMstSetVerbosity(HGFAMBMST hMst, int nVerbosity);
  89. void GfaMbMstDumpADU(HGFAMBMST hMst, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu, bool bTX, const char *pszAnnotation);
  90. ssize_t GfaMbMstCreateADU(uint8_t slvID, uint8_t func, const void *pData, size_t nCbData, LPMODBUS_RTU_ADU pAdu);
  91. bool GfaMbMstValidateADU(uint8_t slvID, uint8_t func, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu, int *pnMbErr);
  92. /////////////////////////////////////////////////////////////////////////////
  93. #ifdef __cplusplus
  94. }
  95. #endif // __cplusplus
  96. #endif // !defined(AGD_MODBMST_H__7E9090EE_6B4D_4499_8080_03241FAC527B__INCLUDED_)