xref: /nrf52832-nimble/rt-thread/components/net/freemodbus/modbus/include/mb_m.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3  * Copyright (C) 2013 Armink <[email protected]>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * File: $Id: mb_m.h,v 1.60 2013/09/03 10:20:05 Armink Add Master Functions $
29  */
30 
31 #ifndef _MB_M_H
32 #define _MB_M_H
33 
34 #ifdef __cplusplus
35 PR_BEGIN_EXTERN_C
36 #endif
37 
38 /*! \defgroup modbus Modbus
39  * \code #include "mb_m.h" \endcode
40  *
41  * This module defines the interface for the application. It contains
42  * the basic functions and types required to use the Modbus Master protocol stack.
43  * A typical application will want to call eMBMasterInit() first. If the device
44  * is ready to answer network requests it must then call eMBEnable() to activate
45  * the protocol stack. In the main loop the function eMBMasterPoll() must be called
46  * periodically. The time interval between pooling depends on the configured
47  * Modbus timeout. If an RTOS is available a separate task should be created
48  * and the task should always call the function eMBMasterPoll().
49  *
50  * \code
51  * // Initialize protocol stack in RTU mode for a Master
52  * eMBMasterInit( MB_RTU, 38400, MB_PAR_EVEN );
53  * // Enable the Modbus Protocol Stack.
54  * eMBMasterEnable(  );
55  * for( ;; )
56  * {
57  *     // Call the main polling loop of the Modbus Master protocol stack.
58  *     eMBMasterPoll(  );
59  *     ...
60  * }
61  * \endcode
62  */
63 
64 /* ----------------------- Defines ------------------------------------------*/
65 
66 /*! \ingroup modbus
67  * \brief Use the default Modbus Master TCP port (502)
68  */
69 #define MB_MASTER_TCP_PORT_USE_DEFAULT 0
70 
71 /* ----------------------- Type definitions ---------------------------------*/
72 /*! \ingroup modbus
73  * \brief Errorcodes used by all function in the Master request.
74  */
75 typedef enum
76 {
77     MB_MRE_NO_ERR,                  /*!< no error. */
78     MB_MRE_NO_REG,                  /*!< illegal register address. */
79     MB_MRE_ILL_ARG,                 /*!< illegal argument. */
80     MB_MRE_REV_DATA,                /*!< receive data error. */
81     MB_MRE_TIMEDOUT,                /*!< timeout error occurred. */
82     MB_MRE_MASTER_BUSY,             /*!< master is busy now. */
83     MB_MRE_EXE_FUN                  /*!< execute function error. */
84 } eMBMasterReqErrCode;
85 /*! \ingroup modbus
86  *  \brief TimerMode is Master 3 kind of Timer modes.
87  */
88 typedef enum
89 {
90 	MB_TMODE_T35,                   /*!< Master receive frame T3.5 timeout. */
91 	MB_TMODE_RESPOND_TIMEOUT,       /*!< Master wait respond for slave. */
92 	MB_TMODE_CONVERT_DELAY          /*!< Master sent broadcast ,then delay sometime.*/
93 }eMBMasterTimerMode;
94 
95 /* ----------------------- Function prototypes ------------------------------*/
96 /*! \ingroup modbus
97  * \brief Initialize the Modbus Master protocol stack.
98  *
99  * This functions initializes the ASCII or RTU module and calls the
100  * init functions of the porting layer to prepare the hardware. Please
101  * note that the receiver is still disabled and no Modbus frames are
102  * processed until eMBMasterEnable( ) has been called.
103  *
104  * \param eMode If ASCII or RTU mode should be used.
105  * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
106  *   is platform dependent and some ports simply choose to ignore it.
107  * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
108  *   on the porting layer.
109  * \param eParity Parity used for serial transmission.
110  *
111  * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
112  *   The protocol is then in the disabled state and ready for activation
113  *   by calling eMBMasterEnable( ). Otherwise one of the following error codes
114  *   is returned:
115  *    - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
116  */
117 eMBErrorCode    eMBMasterInit( eMBMode eMode, UCHAR ucPort,
118 		                 ULONG ulBaudRate, eMBParity eParity );
119 
120 /*! \ingroup modbus
121  * \brief Initialize the Modbus Master protocol stack for Modbus TCP.
122  *
123  * This function initializes the Modbus TCP Module. Please note that
124  * frame processing is still disabled until eMBEnable( ) is called.
125  *
126  * \param usTCPPort The TCP port to listen on.
127  * \return If the protocol stack has been initialized correctly the function
128  *   returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
129  *   codes is returned:
130  *    - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
131  *        slave addresses are in the range 1 - 247.
132  *    - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
133  */
134 eMBErrorCode    eMBMasterTCPInit( USHORT usTCPPort );
135 
136 /*! \ingroup modbus
137  * \brief Release resources used by the protocol stack.
138  *
139  * This function disables the Modbus Master protocol stack and release all
140  * hardware resources. It must only be called when the protocol stack
141  * is disabled.
142  *
143  * \note Note all ports implement this function. A port which wants to
144  *   get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
145  *
146  * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
147  *   If the protocol stack is not in the disabled state it returns
148  *   eMBErrorCode::MB_EILLSTATE.
149  */
150 eMBErrorCode    eMBMasterClose( void );
151 
152 /*! \ingroup modbus
153  * \brief Enable the Modbus Master protocol stack.
154  *
155  * This function enables processing of Modbus Master frames. Enabling the protocol
156  * stack is only possible if it is in the disabled state.
157  *
158  * \return If the protocol stack is now in the state enabled it returns
159  *   eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
160  *   return eMBErrorCode::MB_EILLSTATE.
161  */
162 eMBErrorCode    eMBMasterEnable( void );
163 
164 /*! \ingroup modbus
165  * \brief Disable the Modbus Master protocol stack.
166  *
167  * This function disables processing of Modbus frames.
168  *
169  * \return If the protocol stack has been disabled it returns
170  *  eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
171  *  eMBErrorCode::MB_EILLSTATE.
172  */
173 eMBErrorCode    eMBMasterDisable( void );
174 
175 /*! \ingroup modbus
176  * \brief The main pooling loop of the Modbus Master protocol stack.
177  *
178  * This function must be called periodically. The timer interval required
179  * is given by the application dependent Modbus slave timeout. Internally the
180  * function calls xMBMasterPortEventGet() and waits for an event from the receiver or
181  * transmitter state machines.
182  *
183  * \return If the protocol stack is not in the enabled state the function
184  *   returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
185  *   eMBErrorCode::MB_ENOERR.
186  */
187 eMBErrorCode    eMBMasterPoll( void );
188 
189 /*! \ingroup modbus
190  * \brief Registers a callback handler for a given function code.
191  *
192  * This function registers a new callback handler for a given function code.
193  * The callback handler supplied is responsible for interpreting the Modbus PDU and
194  * the creation of an appropriate response. In case of an error it should return
195  * one of the possible Modbus exceptions which results in a Modbus exception frame
196  * sent by the protocol stack.
197  *
198  * \param ucFunctionCode The Modbus function code for which this handler should
199  *   be registers. Valid function codes are in the range 1 to 127.
200  * \param pxHandler The function handler which should be called in case
201  *   such a frame is received. If \c NULL a previously registered function handler
202  *   for this function code is removed.
203  *
204  * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
205  *   more resources are available it returns eMBErrorCode::MB_ENORES. In this
206  *   case the values in mbconfig.h should be adjusted. If the argument was not
207  *   valid it returns eMBErrorCode::MB_EINVAL.
208  */
209 eMBErrorCode    eMBMasterRegisterCB( UCHAR ucFunctionCode,
210                                pxMBFunctionHandler pxHandler );
211 
212 /* ----------------------- Callback -----------------------------------------*/
213 
214 /*! \defgroup modbus_master registers Modbus Registers
215  * \code #include "mb_m.h" \endcode
216  * The protocol stack does not internally allocate any memory for the
217  * registers. This makes the protocol stack very small and also usable on
218  * low end targets. In addition the values don't have to be in the memory
219  * and could for example be stored in a flash.<br>
220  * Whenever the protocol stack requires a value it calls one of the callback
221  * function with the register address and the number of registers to read
222  * as an argument. The application should then read the actual register values
223  * (for example the ADC voltage) and should store the result in the supplied
224  * buffer.<br>
225  * If the protocol stack wants to update a register value because a write
226  * register function was received a buffer with the new register values is
227  * passed to the callback function. The function should then use these values
228  * to update the application register values.
229  */
230 
231 /*! \ingroup modbus_registers
232  * \brief Callback function used if the value of a <em>Input Register</em>
233  *   is required by the protocol stack. The starting register address is given
234  *   by \c usAddress and the last register is given by <tt>usAddress +
235  *   usNRegs - 1</tt>.
236  *
237  * \param pucRegBuffer A buffer where the callback function should write
238  *   the current value of the modbus registers to.
239  * \param usAddress The starting address of the register. Input registers
240  *   are in the range 1 - 65535.
241  * \param usNRegs Number of registers the callback function must supply.
242  *
243  * \return The function must return one of the following error codes:
244  *   - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
245  *       Modbus response is sent.
246  *   - eMBErrorCode::MB_ENOREG If the application does not map an coils
247  *       within the requested address range. In this case a
248  *       <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
249  */
250 eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
251 		USHORT usNRegs );
252 
253 /*! \ingroup modbus_registers
254  * \brief Callback function used if a <em>Holding Register</em> value is
255  *   read or written by the protocol stack. The starting register address
256  *   is given by \c usAddress and the last register is given by
257  *   <tt>usAddress + usNRegs - 1</tt>.
258  *
259  * \param pucRegBuffer If the application registers values should be updated the
260  *   buffer points to the new registers values. If the protocol stack needs
261  *   to now the current values the callback function should write them into
262  *   this buffer.
263  * \param usAddress The starting address of the register.
264  * \param usNRegs Number of registers to read or write.
265  * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
266  *   values should be updated from the values in the buffer. For example
267  *   this would be the case when the Modbus master has issued an
268  *   <b>WRITE SINGLE REGISTER</b> command.
269  *   If the value eMBRegisterMode::MB_REG_READ the application should copy
270  *   the current values into the buffer \c pucRegBuffer.
271  *
272  * \return The function must return one of the following error codes:
273  *   - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
274  *       Modbus response is sent.
275  *   - eMBErrorCode::MB_ENOREG If the application does not map an coils
276  *       within the requested address range. In this case a
277  *       <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
278  */
279 eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
280 		USHORT usNRegs, eMBRegisterMode eMode );
281 
282 /*! \ingroup modbus_registers
283  * \brief Callback function used if a <em>Coil Register</em> value is
284  *   read or written by the protocol stack. If you are going to use
285  *   this function you might use the functions xMBUtilSetBits(  ) and
286  *   xMBUtilGetBits(  ) for working with bitfields.
287  *
288  * \param pucRegBuffer The bits are packed in bytes where the first coil
289  *   starting at address \c usAddress is stored in the LSB of the
290  *   first byte in the buffer <code>pucRegBuffer</code>.
291  *   If the buffer should be written by the callback function unused
292  *   coil values (I.e. if not a multiple of eight coils is used) should be set
293  *   to zero.
294  * \param usAddress The first coil number.
295  * \param usNCoils Number of coil values requested.
296  * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
297  *   be updated from the values supplied in the buffer \c pucRegBuffer.
298  *   If eMBRegisterMode::MB_REG_READ the application should store the current
299  *   values in the buffer \c pucRegBuffer.
300  *
301  * \return The function must return one of the following error codes:
302  *   - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
303  *       Modbus response is sent.
304  *   - eMBErrorCode::MB_ENOREG If the application does not map an coils
305  *       within the requested address range. In this case a
306  *       <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
307  */
308 eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
309 		USHORT usNCoils, eMBRegisterMode eMode );
310 
311 /*! \ingroup modbus_registers
312  * \brief Callback function used if a <em>Input Discrete Register</em> value is
313  *   read by the protocol stack.
314  *
315  * If you are going to use his function you might use the functions
316  * xMBUtilSetBits(  ) and xMBUtilGetBits(  ) for working with bitfields.
317  *
318  * \param pucRegBuffer The buffer should be updated with the current
319  *   coil values. The first discrete input starting at \c usAddress must be
320  *   stored at the LSB of the first byte in the buffer. If the requested number
321  *   is not a multiple of eight the remaining bits should be set to zero.
322  * \param usAddress The starting address of the first discrete input.
323  * \param usNDiscrete Number of discrete input values.
324  * \return The function must return one of the following error codes:
325  *   - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
326  *       Modbus response is sent.
327  *   - eMBErrorCode::MB_ENOREG If the application does not map an coils
328  *       within the requested address range. In this case a
329  *       <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
330  */
331 eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
332 		USHORT usNDiscrete );
333 
334 /*! \ingroup modbus
335  *\brief These Modbus functions are called for user when Modbus run in Master Mode.
336  */
337 eMBMasterReqErrCode
338 eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
339 eMBMasterReqErrCode
340 eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
341 eMBMasterReqErrCode
342 eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr,
343 		USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut );
344 eMBMasterReqErrCode
345 eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
346 eMBMasterReqErrCode
347 eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
348 		USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
349 		USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut );
350 eMBMasterReqErrCode
351 eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut );
352 eMBMasterReqErrCode
353 eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut );
354 eMBMasterReqErrCode
355 eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
356 		USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut );
357 eMBMasterReqErrCode
358 eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut );
359 
360 eMBException
361 eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
362 eMBException
363 eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
364 eMBException
365 eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
366 eMBException
367 eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
368 eMBException
369 eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
370 eMBException
371 eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
372 eMBException
373 eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
374 eMBException
375 eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
376 eMBException
377 eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
378 eMBException
379 eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
380 
381 /*�� \ingroup modbus
382  *\brief These functions are interface for Modbus Master
383  */
384 void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );
385 UCHAR ucMBMasterGetDestAddress( void );
386 void vMBMasterSetDestAddress( UCHAR Address );
387 BOOL xMBMasterGetCBRunInMasterMode( void );
388 void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
389 USHORT usMBMasterGetPDUSndLength( void );
390 void vMBMasterSetPDUSndLength( USHORT SendPDULength );
391 void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
392 BOOL xMBMasterRequestIsBroadcast( void );
393 eMBMasterErrorEventType eMBMasterGetErrorType( void );
394 void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
395 eMBMasterReqErrCode eMBMasterWaitRequestFinish( void );
396 
397 /* ----------------------- Callback -----------------------------------------*/
398 
399 #ifdef __cplusplus
400 PR_END_EXTERN_C
401 #endif
402 #endif
403