gfambrtumst.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*! \file gfambrtumst.h
  2. \brief Header file of the \b gfambrtumst library.
  3. */
  4. #if !defined(AGD_GFAMBRTUMST_H__F97088DB_3E1B_4FC8_A313_476634890514__INCLUDED_)
  5. #define AGD_GFAMBRTUMST_H__F97088DB_3E1B_4FC8_A313_476634890514__INCLUDED_
  6. #include <gfautils.h>
  7. #include <gfambrtucom.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif // __cplusplus
  11. /////////////////////////////////////////////////////////////////////////////
  12. // mbrtumast.h - Declarations:
  13. /*! \typedef HMBRTUMST
  14. \brief Defines a Handle to a Modbus RTU Master.
  15. */
  16. typedef void *HMBRTUMST;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // error codes
  19. /*! \def GFA_MB_MST_ERROR_INVALID_SLAVE_ID
  20. \brief Invalid Slave ID.
  21. */
  22. #define GFA_MB_MST_ERROR_INVALID_SLAVE_ID 0x0001
  23. /*! \def GFA_MB_MST_ERROR_INVALID_FUNCTION
  24. \brief Invalid function code.
  25. */
  26. #define GFA_MB_MST_ERROR_INVALID_FUNCTION 0x0002
  27. /*! \def GFA_MB_MST_ERROR_SLAVE_ERROR
  28. \brief The slave has responded with an error-function-code (0x80).
  29. */
  30. #define GFA_MB_MST_ERROR_SLAVE_ERROR 0x0003
  31. /*! \def GFA_MB_MST_ERROR_SLAVE_RESPONSE_TIMEOUT
  32. \brief The slave did not respond within a certain time frame (currently 500 milliseconds).
  33. */
  34. #define GFA_MB_MST_ERROR_SLAVE_RESPONSE_TIMEOUT 0x0004
  35. /*! \def GFA_MB_MST_ERROR_SLAVE_CHAR_TIMEOUT
  36. \brief The master detected a time interval greater than 1.5 characters between two bytes received.
  37. */
  38. #define GFA_MB_MST_ERROR_SLAVE_CHAR_TIMEOUT 0x0005
  39. /*! \def GFA_MB_MST_ERROR_SLAVE_INVALID_CRC
  40. \brief CRC error in response.
  41. */
  42. #define GFA_MB_MST_ERROR_SLAVE_INVALID_CRC 0x0006
  43. /*! \def GFA_MB_MST_ERROR_SLAVE_INVALID_DATA
  44. \brief The response contained invalid data.
  45. */
  46. #define GFA_MB_MST_ERROR_SLAVE_INVALID_DATA 0x0007
  47. /*! \def GFA_MB_MST_ERROR_UNEXPECTED
  48. \brief An unexpected error occured.
  49. */
  50. #define GFA_MB_MST_ERROR_UNEXPECTED 0xFFFF
  51. /////////////////////////////////////////////////////////////////////////////
  52. /*! \typedef GFA_MODBUS_RTU_MST_STATES
  53. \brief Defines the Master's states used in the state machine.
  54. \verbatim
  55. typedef enum _GFA_MODBUS_RTU_MST_STATES
  56. {
  57. MB_RTU_MST_Void = -1,
  58. MB_RTU_MST_Idle,
  59. MB_RTU_MST_TxStart,
  60. MB_RTU_MST_TxWaitEnd,
  61. MB_RTU_MST_TxDone,
  62. MB_RTU_MST_WaitRxFifo,
  63. MB_RTU_MST_RxSlvID,
  64. MB_RTU_MST_RxFunc,
  65. MB_RTU_MST_RxData,
  66. MB_RTU_MST_RxError,
  67. MB_RTU_MST_RxCRC,
  68. MB_RTU_MST_RxFinalize,
  69. MB_RTU_MST_ReportError
  70. }GFA_MODBUS_RTU_MST_STATES;
  71. \endverbatim
  72. */
  73. typedef enum _GFA_MODBUS_RTU_MST_STATES
  74. {
  75. MB_RTU_MST_Void = -1,
  76. MB_RTU_MST_Idle,
  77. MB_RTU_MST_TxStart,
  78. MB_RTU_MST_TxWaitEnd,
  79. MB_RTU_MST_TxDone,
  80. MB_RTU_MST_WaitRxFifo,
  81. MB_RTU_MST_RxSlvID,
  82. MB_RTU_MST_RxFunc,
  83. MB_RTU_MST_RxData,
  84. MB_RTU_MST_RxError,
  85. MB_RTU_MST_RxCRC,
  86. MB_RTU_MST_RxFinalize,
  87. MB_RTU_MST_ReportError
  88. }GFA_MODBUS_RTU_MST_STATES;
  89. /////////////////////////////////////////////////////////////////////////////
  90. /*! \typedef typedef void (*PFN_GFA_MASTER_RAW_REQUEST_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err, LPCMODBUS_RTU_ADU padu, size_t nCbData, void *pParam)
  91. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstSendRawRequest has completed.
  92. \param nTID The transaction id of the request.
  93. \param nSlvID The slave id of the target of the request.
  94. \param err The error code, if an error has occured or 0 if successful.
  95. \param padu A pointer to the \b MODBUS_RTU_ADU (Application Data Unit) structure that on success holds the complete received data. \b MODBUS_RTU_ADU is defined in \b gfambrtucom.h.
  96. \param nCbData The number of data bytes received not counting the Slave ID, the Function code and the CRC.
  97. \param pParam The user defined parameter that has been passed to \ref GfaModbusRTUMstSendRawRequest.
  98. \note The completion routine will be called both on success and on error (or timeout)! Thus the \b err parameter should be evaluated, before performing other tasks!
  99. In case of an error \b err comprises one of the \b GFA_MB_MST_ERROR_* error codes and \b padu is \b NULL. On success \b err is 0.
  100. */
  101. typedef void (*PFN_GFA_MASTER_RAW_REQUEST_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, LPCMODBUS_RTU_ADU padu, size_t nCbData, void *pParam);
  102. /////////////////////////////////////////////////////////////////////////////
  103. /*! \typedef typedef void (*PFN_GFA_MASTER_READ_REGISTERS_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err, const uint16_t *pRegs, size_t nRegCount)
  104. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstReadHoldingRegisters or \ref GfaModbusRTUMstReadInputRegisters has completed.
  105. \param nTID The transaction id of the request.
  106. \param nSlvID The slave id of the target of the request.
  107. \param err The error code, if an error has occured or 0 if successful.
  108. \param pRegs Pointer to an array of registers.
  109. \param nRegCount Number of elements in \b pRegs.
  110. \note The completion routine will be called both on success and on error (or timeout)! See \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE for details.
  111. */
  112. typedef void (*PFN_GFA_MASTER_READ_REGISTERS_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, const uint16_t *pRegs, size_t nRegCount);
  113. /////////////////////////////////////////////////////////////////////////////
  114. /*! \typedef typedef void (*PFN_GFA_MASTER_PRESET_SINGLE_REGISTER_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err)
  115. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstPresetSingleRegister has completed.
  116. \param nTID The transaction id of the request.
  117. \param nSlvID The slave id of the target of the request.
  118. \param err The error code, if an error has occured or 0 if successful.
  119. \note The completion routine will be called both on success and on error (or timeout)! See \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE for details.
  120. */
  121. typedef void (*PFN_GFA_MASTER_PRESET_SINGLE_REGISTER_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err);
  122. /////////////////////////////////////////////////////////////////////////////
  123. /*! \typedef typedef void (*PFN_GFA_MASTER_WRITE_REGISTERS_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nRegStart, uint16_t nRegsWritten)
  124. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstWriteMultipleRegisters has completed.
  125. \param nTID The transaction id of the request.
  126. \param nSlvID The slave id of the target of the request.
  127. \param err The error code, if an error has occured or 0 if successful.
  128. \param nRegStart Address of the first register that has been written.
  129. \param nRegsWritten Number of registers written.
  130. \note The completion routine will be called both on success and on error (or timeout)! See \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE for details.
  131. */
  132. typedef void (*PFN_GFA_MASTER_WRITE_REGISTERS_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nRegStart, uint16_t nRegsWritten);
  133. /////////////////////////////////////////////////////////////////////////////
  134. /*! \typedef typedef void (*PFN_GFA_MASTER_DIAGNOSIS_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nSubFunc, uint16_t nData)
  135. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstDiagnosis has completed.
  136. \param nTID The transaction id of the request.
  137. \param nSlvID The slave id of the target of the request.
  138. \param err The error code, if an error has occured or 0 if successful.
  139. \param nSubFunc The requested sub-function code.
  140. \param nData The result of the request depending on the sub-function.
  141. \note The completion routine will be called both on success and on error (or timeout)! See \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE for details.
  142. */
  143. typedef void (*PFN_GFA_MASTER_DIAGNOSIS_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nSubFunc, uint16_t nData);
  144. /////////////////////////////////////////////////////////////////////////////
  145. /*! \typedef typedef void (*PFN_GFA_MASTER_REPORT_SLAVE_ID_COMPLETE)(uint32_t nTID, uint8_t nSlvID, uint16_t err, const void *pData, size_t nCbData)
  146. \brief Defines the pointer to a callback function, that is called when a request initiated by \ref GfaModbusRTUMstReportSlaveID has completed.
  147. \param nTID The transaction id of the request.
  148. \param nSlvID The slave id of the target of the request.
  149. \param err The error code, if an error has occured or 0 if successful.
  150. \param pData The slave's response data.
  151. \param nCbData Number of bytes of the response data.
  152. \note The completion routine will be called both on success and on error (or timeout)! See \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE for details.
  153. */
  154. typedef void (*PFN_GFA_MASTER_REPORT_SLAVE_ID_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, const void *pData, size_t nCbData);
  155. /////////////////////////////////////////////////////////////////////////////
  156. /*! \typedef typedef void (*PFN_GFA_MASTER_STATE_CHANGED)(GFA_MODBUS_RTU_MST_STATES newState, GFA_MODBUS_RTU_MST_STATES oldState)
  157. \brief Defines the pointer to a callback function, that is called after the master's state has changed.
  158. \param newState The new state of the master.
  159. \param oldState The previous state of the master.
  160. */
  161. typedef void (*PFN_GFA_MASTER_STATE_CHANGED) (GFA_MODBUS_RTU_MST_STATES newState, GFA_MODBUS_RTU_MST_STATES oldState);
  162. /////////////////////////////////////////////////////////////////////////////
  163. /*! \typedef typedef void (*PFN_GFA_MASTER_PRE_TRANSMIT)(uint32_t nTID, uint8_t nSlvID)
  164. \brief Defines the pointer to a callback function, that is called before the master starts to transmit a request.
  165. \param nTID The transaction id of the request that is about to be sent.
  166. \param nSlvID The slave id of the target of the request that is about to be sent.
  167. */
  168. typedef void (*PFN_GFA_MASTER_PRE_TRANSMIT) (uint32_t nTID, uint8_t nSlvID);
  169. /////////////////////////////////////////////////////////////////////////////
  170. /*! \typedef typedef void (*PFN_GFA_MASTER_POST_TRANSMIT)(uint32_t nTID, uint8_t nSlvID)
  171. \brief Defines the pointer to a callback function, that is called when the master has finished transmitting a request and all data has been sent.
  172. \param nTID The transaction id of the request that has been sent.
  173. \param nSlvID The slave id of the target of the request that has been sent.
  174. */
  175. typedef void (*PFN_GFA_MASTER_POST_TRANSMIT) (uint32_t nTID, uint8_t nSlvID);
  176. /////////////////////////////////////////////////////////////////////////////
  177. /*! \typedef typedef void (*PFN_GFA_MASTER_RECEIVE_START)(uint32_t nTID)
  178. \brief Defines the pointer to a callback function, that is called before the master starts reading a response from a slave and data is already available.
  179. \param nTID The transaction id of the request which response is expected.
  180. */
  181. typedef void (*PFN_GFA_MASTER_RECEIVE_START) (uint32_t nTID);
  182. /////////////////////////////////////////////////////////////////////////////
  183. /*! \typedef typedef void (*PFN_GFA_MASTER_RECEIVE_END)(uint32_t nTID)
  184. \brief Defines the pointer to a callback function, that is called after the master has received a response from a slave.
  185. \param nTID The transaction id of the request which response has been received.
  186. \note This callback function is actually redundant and will be called right before the completion routine is called, which will provide more info about the response.
  187. */
  188. typedef void (*PFN_GFA_MASTER_RECEIVE_END) (uint32_t nTID);
  189. /////////////////////////////////////////////////////////////////////////////
  190. /*! \typedef GFA_MODBUS_MASTER_APP_INTERFACE
  191. \brief Defines a set of functions to interact with a Modbus application. Any of these function pointers may be NULL, if not needed.
  192. \verbatim
  193. typedef struct _GFA_MODBUS_MASTER_APP_INTERFACE
  194. {
  195. PFN_GFA_MASTER_STATE_CHANGED pfnStateChanged; // called after the master's state has changed
  196. PFN_GFA_MASTER_PRE_TRANSMIT pfnPreTx; // called before the master starts to transmit a request
  197. PFN_GFA_MASTER_POST_TRANSMIT pfnPostTx; // called when the master has finished transmitting a request and all data has been sent
  198. PFN_GFA_MASTER_RECEIVE_START pfnRxStart; // called before the master starts reading a response from a slave and data is already available
  199. PFN_GFA_MASTER_RECEIVE_END pfnRxEnd; // called after the master has received a response from a slave
  200. }GFA_MODBUS_MASTER_APP_INTERFACE, *LPGFA_MODBUS_MASTER_APP_INTERFACE;
  201. typedef const GFA_MODBUS_MASTER_APP_INTERFACE *LPCGFA_MODBUS_MASTER_APP_INTERFACE;
  202. \endverbatim
  203. */
  204. typedef struct _GFA_MODBUS_MASTER_APP_INTERFACE
  205. {
  206. PFN_GFA_MASTER_STATE_CHANGED pfnStateChanged;
  207. PFN_GFA_MASTER_PRE_TRANSMIT pfnPreTx;
  208. PFN_GFA_MASTER_POST_TRANSMIT pfnPostTx;
  209. PFN_GFA_MASTER_RECEIVE_START pfnRxStart;
  210. PFN_GFA_MASTER_RECEIVE_END pfnRxEnd;
  211. }GFA_MODBUS_MASTER_APP_INTERFACE, *LPGFA_MODBUS_MASTER_APP_INTERFACE;
  212. typedef const GFA_MODBUS_MASTER_APP_INTERFACE *LPCGFA_MODBUS_MASTER_APP_INTERFACE;
  213. /////////////////////////////////////////////////////////////////////////////
  214. /*! \typedef GFA_MODBUS_RTU_MASTER_PARAMETERS
  215. \brief Used to create a Modbus RTU Master
  216. \verbatim
  217. typedef struct _GFA_MODBUS_RTU_MASTER_PARAMETERS
  218. {
  219. HFIFO hFifoRX; // handle to the receive fifo buffer
  220. HFIFO hFifoTX; // handle to the transmit fifo buffer
  221. GFA_MODBUS_PROTOCOL_TIMEOUTS mpt; // modbus protocol timeouts
  222. GFA_MODBUS_MASTER_APP_INTERFACE appItf; // app interface
  223. }GFA_MODBUS_RTU_MASTER_PARAMETERS, *LPGFA_MODBUS_RTU_MASTER_PARAMETERS;
  224. typedef const GFA_MODBUS_RTU_MASTER_PARAMETERS *LPCGFA_MODBUS_RTU_MASTER_PARAMETERS;
  225. \endverbatim
  226. */
  227. typedef struct _GFA_MODBUS_RTU_MASTER_PARAMETERS
  228. {
  229. HFIFO hFifoRX;
  230. HFIFO hFifoTX;
  231. uint64_t nRxTimeoutUs;
  232. GFA_MODBUS_PROTOCOL_TIMEOUTS mpt;
  233. GFA_MODBUS_MASTER_APP_INTERFACE appItf;
  234. }GFA_MODBUS_RTU_MASTER_PARAMETERS, *LPGFA_MODBUS_RTU_MASTER_PARAMETERS;
  235. typedef const GFA_MODBUS_RTU_MASTER_PARAMETERS *LPCGFA_MODBUS_RTU_MASTER_PARAMETERS;
  236. /////////////////////////////////////////////////////////////////////////////
  237. /*! \fn HMBRTUMST GfaModbusRTUMstCreate(LPCGFA_MODBUS_RTU_MASTER_PARAMETERS pmap)
  238. \brief Creates and initializes a Modbus RTU Master.
  239. \param pmap A const pointer to a \ref GFA_MODBUS_RTU_MASTER_PARAMETERS structure.
  240. \return If successful, returns a handle to the newly created master, or NULL in the case of an error.
  241. */
  242. HMBRTUMST GfaModbusRTUMstCreate(LPCGFA_MODBUS_RTU_MASTER_PARAMETERS pmap);
  243. /////////////////////////////////////////////////////////////////////////////
  244. /*! \fn void GfaModbusRTUMstRelease(HMBRTUMST hMbMst)
  245. \brief Releases a modbus master, that was previously created with \ref GfaModbusRTUMstCreate.
  246. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  247. */
  248. void GfaModbusRTUMstRelease(HMBRTUMST hMbMst);
  249. /////////////////////////////////////////////////////////////////////////////
  250. /*! \fn bool GfaModbusRTUMstStateMachine(HMBRTUMST hMbMst)
  251. \brief The modbus master's state machine. This function is the heart beat of the master and must be called repeatedly.
  252. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  253. \return \b true if successful, or \b false only if hMbMst is NULL or an invalid state was detected.
  254. */
  255. bool GfaModbusRTUMstStateMachine(HMBRTUMST hMbMst);
  256. /////////////////////////////////////////////////////////////////////////////
  257. /*! \fn bool GfaModbusRTUMstSendRawRequest(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint8_t nFunc, const void *pData, size_t nCbData, size_t nCbDataExpected, PFN_GFA_MASTER_RAW_REQUEST_COMPLETE pfnRawReqComplete, void *pParam)
  258. \brief Creates a generic Modbus RTU Request and sends it to a Slave.
  259. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  260. \param nTID User defined transaction id. This id will be passed to the completion routine.
  261. \param nSlvID ID of the target slave.
  262. \param nFunc Modbus function code.
  263. \param pData Pointer to the data payload. May be \b NULL if no additional data is sent.
  264. \param nCbData Number of bytes of the data payload. May be 0.
  265. \param nCbDataExpected Number of bytes expected in the response not counting the slave id, the function code and the CRC.
  266. \param pfnRawReqComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_RAW_REQUEST_COMPLETE that will be called when the response has been received or a timeout occured.
  267. \param pParam User defined parameter, that will be passed to the completion function.
  268. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  269. \note The actual state of the master can be tracked by implementing the \ref PFN_GFA_MASTER_STATE_CHANGED
  270. callback function or it can be requested on demand by calling \ref GfaModbusRTUMstGetState. \b GfaModbusRTUMstSendRawRequest returns immediately after the request has been sent and thus cannot fail in the case of a
  271. response error. If the response fails, the completion routine will be called with an according error code.
  272. */
  273. bool GfaModbusRTUMstSendRawRequest(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint8_t nFunc, const void *pData, size_t nCbData, size_t nCbDataExpected, PFN_GFA_MASTER_RAW_REQUEST_COMPLETE pfnRawReqComplete, void *pParam);
  274. /////////////////////////////////////////////////////////////////////////////
  275. /*! \fn bool GfaModbusRTUMstReadHoldingRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, PFN_GFA_MASTER_READ_REGISTERS_COMPLETE pfnReadRegsComplete)
  276. \brief Reads registers from a Slave.
  277. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  278. \param nTID User defined transaction id. This id will be passed to the completion routine.
  279. \param nSlvID ID of the target slave.
  280. \param nRegStart Address of the first register to be read.
  281. \param nRegCount Number of registers to be read.
  282. \param pfnReadRegsComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_READ_REGISTERS_COMPLETE that will be called when the response has been received or a timeout occured.
  283. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  284. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  285. */
  286. bool GfaModbusRTUMstReadHoldingRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, PFN_GFA_MASTER_READ_REGISTERS_COMPLETE pfnReadRegsComplete);
  287. /////////////////////////////////////////////////////////////////////////////
  288. /*! \fn bool GfaModbusRTUMstReadInputRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, PFN_GFA_MASTER_READ_REGISTERS_COMPLETE pfnReadRegsComplete)
  289. \brief Reads input registers from a Slave.
  290. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  291. \param nTID User defined transaction id. This id will be passed to the completion routine.
  292. \param nSlvID ID of the target slave.
  293. \param nRegStart Address of the first register to be read.
  294. \param nRegCount Number of registers to be read.
  295. \param pfnReadRegsComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_READ_REGISTERS_COMPLETE that will be called when the response has been received or a timeout occured.
  296. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  297. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  298. */
  299. bool GfaModbusRTUMstReadInputRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, PFN_GFA_MASTER_READ_REGISTERS_COMPLETE pfnReadRegsComplete);
  300. /////////////////////////////////////////////////////////////////////////////
  301. /*! \fn bool GfaModbusRTUMstWriteMultipleRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, const void *pRegData, PFN_GFA_MASTER_WRITE_REGISTERS_COMPLETE pfnWriteRegsComplete)
  302. \brief Writes multiple registers to a Slave.
  303. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  304. \param nTID User defined transaction id. This id will be passed to the completion routine.
  305. \param nSlvID ID of the target slave.
  306. \param nRegStart Address of the first register to to be written to.
  307. \param nRegCount Number of registers to write.
  308. \param pRegData Pointer to a buffer that holds the data to be written. The buffer must be at least of size nRegCount * sizeof(uint16_t).
  309. \param pfnWriteRegsComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_WRITE_REGISTERS_COMPLETE that will be called when the response has been received or a timeout occured.
  310. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  311. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  312. */
  313. bool GfaModbusRTUMstWriteMultipleRegisters(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegStart, uint16_t nRegCount, const void *pRegData, PFN_GFA_MASTER_WRITE_REGISTERS_COMPLETE pfnWriteRegsComplete);
  314. /////////////////////////////////////////////////////////////////////////////
  315. /*! \fn bool GfaModbusRTUMstPresetSingleRegister(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegAddr, uint16_t nRegVal, PFN_GFA_MASTER_PRESET_SINGLE_REGISTER_COMPLETE pfnPresetRegComplete)
  316. \brief Writes a single register to a Slave.
  317. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  318. \param nTID User defined transaction id. This id will be passed to the completion routine.
  319. \param nSlvID ID of the target slave.
  320. \param nRegAddr Address of the register to to be written to.
  321. \param nRegVal Value to write.
  322. \param pfnPresetRegComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_PRESET_SINGLE_REGISTER_COMPLETE that will be called when the response has been received or a timeout occured.
  323. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  324. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  325. */
  326. bool GfaModbusRTUMstPresetSingleRegister(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nRegAddr, uint16_t nRegVal, PFN_GFA_MASTER_PRESET_SINGLE_REGISTER_COMPLETE pfnPresetRegComplete);
  327. /////////////////////////////////////////////////////////////////////////////
  328. /*! \fn bool GfaModbusRTUMstDiagnosis(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nSubFunc, uint16_t nData, PFN_GFA_MASTER_DIAGNOSIS_COMPLETE pfnDiagComplete)
  329. \brief Reads diagnostic information from a Slave.
  330. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  331. \param nTID User defined transaction id. This id will be passed to the completion routine.
  332. \param nSlvID ID of the target slave.
  333. \param nSubFunc Sub function code of the requested diagnostic data as defined in \b gfambrtucom.h.
  334. \param nData Input value used to send diagnostic data or control information to the slave.
  335. \param pfnDiagComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_DIAGNOSIS_COMPLETE that will be called when the response has been received or a timeout occured.
  336. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  337. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  338. */
  339. bool GfaModbusRTUMstDiagnosis(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint16_t nSubFunc, uint16_t nData, PFN_GFA_MASTER_DIAGNOSIS_COMPLETE pfnDiagComplete);
  340. /////////////////////////////////////////////////////////////////////////////
  341. /*! \fn bool GfaModbusRTUMstReportSlaveID(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, size_t nCbDataExpected, PFN_GFA_MASTER_REPORT_SLAVE_ID_COMPLETE pfnRepSlvIDComplete)
  342. \brief Reads a description of the type of controller present at the slave address, the current status of the slave Run indicator, and other information specific to the slave device.
  343. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  344. \param nTID User defined transaction id. This id will be passed to the completion routine.
  345. \param nSlvID ID of the target slave.
  346. \param nCbDataExpected Number of payload bytes expected in the response, not counting the slave address, the function code and the CRC.
  347. \param pfnRepSlvIDComplete Pointer to a completion function of type \ref PFN_GFA_MASTER_REPORT_SLAVE_ID_COMPLETE that will be called when the response has been received or a timeout occured.
  348. \return \b true if successful, \b false if an invalid parameter was passed or the master is not in idle state at the time of the call.
  349. \note See \ref GfaModbusRTUMstSendRawRequest for more information on how to track the master's state.
  350. */
  351. bool GfaModbusRTUMstReportSlaveID(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, size_t nCbDataExpected, PFN_GFA_MASTER_REPORT_SLAVE_ID_COMPLETE pfnRepSlvIDComplete);
  352. /*! \fn GFA_MODBUS_RTU_MST_STATES GfaModbusRTUMstGetState(HMBRTUMST hMbMst)
  353. \brief Returns the current state of the master.
  354. \param hMbMst Handle to the modbus master that has been acquired by a call to \ref GfaModbusRTUMstCreate.
  355. \return The master's current state or \b MB_RTU_MST_Void if \b hMbMst is invalid.
  356. */
  357. GFA_MODBUS_RTU_MST_STATES GfaModbusRTUMstGetState(HMBRTUMST hMbMst);
  358. /////////////////////////////////////////////////////////////////////////////
  359. /////////////////////////////////////////////////////////////////////////////
  360. /////////////////////////////////////////////////////////////////////////////
  361. // Metronik support
  362. /*
  363. *
  364. * ACHTUNG:
  365. * Befindet sich der Sensors am Bus mit anderen Teilnehmern führt ein Pollen der Slaves mit einem Frame-Delay < 10 ms zu Timeout-Problemen des Sensors!
  366. *
  367. */
  368. #define GFA_MB_ILM_FUNC_READ_UV ((uint8_t)0x41)
  369. #define GFA_MB_ILM_FUNC_READ_TEMP ((uint8_t)0x42)
  370. #define GFA_MB_ILM_FUNC_UNLOCK ((uint8_t)0x43)
  371. #define GFA_MB_ILM_FUNC_NEW_ADDR ((uint8_t)0x44)
  372. #define GFA_MB_ILM_FUNC_READ_RANGE ((uint8_t)0x45)
  373. #define GFA_MB_ILM_FUNC_READ_SERIAL ((uint8_t)0x46)
  374. /////////////////////////////////////////////////////////////////////////////
  375. typedef void (*PFN_GFA_ILM_READ_UV_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nVal);
  376. typedef void (*PFN_GFA_ILM_READ_TEMP_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint8_t nVal);
  377. typedef void (*PFN_GFA_ILM_READ_UNLOCK_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint8_t nVal);
  378. typedef void (*PFN_GFA_ILM_NEW_ADDR_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint8_t nVal);
  379. typedef void (*PFN_GFA_ILM_READ_RANGE_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, uint16_t nVal);
  380. typedef void (*PFN_GFA_ILM_READ_SERIAL_COMPLETE) (uint32_t nTID, uint8_t nSlvID, uint16_t err, const uint8_t *pVal, size_t nCbVal);
  381. /////////////////////////////////////////////////////////////////////////////
  382. bool GfaModbusRTUMstILMReadUV(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, PFN_GFA_ILM_READ_UV_COMPLETE pfnReadUvComplete);
  383. bool GfaModbusRTUMstILMReadTemp(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, PFN_GFA_ILM_READ_TEMP_COMPLETE pfnReadTempComplete);
  384. bool GfaModbusRTUMstILMUnlock(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, PFN_GFA_ILM_READ_UNLOCK_COMPLETE pfnUnlockComplete);
  385. bool GfaModbusRTUMstILMReadRange(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, PFN_GFA_ILM_READ_RANGE_COMPLETE pfnReadRangeComplete);
  386. bool GfaModbusRTUMstILMReadSerial(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, PFN_GFA_ILM_READ_SERIAL_COMPLETE pfnReadSerialComplete);
  387. /*
  388. * GfaModbusRTUMstILMNewAddr: Weist dem Sensor eine neue Slave-Adresse zu.
  389. * Achtung: Der Sensor antwortet bei Erfolg bereits mit der neuen Slave-Adresse im Antwort-Frame!!!
  390. * Der Master ruft im Anschluss die Completion-Funktion mit dem Fehlercode \ref GFA_MB_MST_ERROR_INVALID_SLAVE_ID auf!
  391. * Die neue Slave-Adresse wurde in diesem Fall aber erfolgreich im Slave gesetzt!
  392. *
  393. * Achtung: Der Sensor akzeptiert die Broadcast-Adresse 0 als Slave-ID! Diese sollte niemals gesetzt werden!
  394. */
  395. bool GfaModbusRTUMstILMNewAddr(HMBRTUMST hMbMst, uint32_t nTID, uint8_t nSlvID, uint8_t nNewAddr, PFN_GFA_ILM_NEW_ADDR_COMPLETE pfnNewAddrComplete);
  396. /////////////////////////////////////////////////////////////////////////////
  397. #ifdef __cplusplus
  398. }
  399. #endif // __cplusplus
  400. #endif // !defined(AGD_GFAMBRTUMST_H__F97088DB_3E1B_4FC8_A313_476634890514__INCLUDED_)