ConOpSys V2970  P004.07
ANVILEX control operating system
MODBUS_PDU.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 //! @file MODBUS_PDU.h
3 //! @brief Communication protocol MODBUS over UART class header file.
4 //! @attention No special attention requered.
5 //! @copyright (C) 2015-2021 ANVILEX LLC
6 //! $HeadURL: https://192.168.3.4:8443/svn/P004_07/ConOpSys/Engine/MODBUS_PDU.h $
7 //! $Revision: 2826 $
8 //! $Date: 2021-07-28 13:46:37 +0500 (Mi, 28 Jul 2021) $
9 //! $Author: minch $
10 //------------------------------------------------------------------------------
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are met:
14 //
15 // 1. Redistributions of source code must retain the above copyright notice,
16 // this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of ANVILEX nor the names of its contributors may be
23 // used to endorse or promote products derived from this software without
24 // specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
30 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 // POSSIBILITY OF SUCH DAMAGE.
37 //
38 //------------------------------------------------------------------------------
39 
40 #pragma once
41 
42 //------------------------------------------------------------------------------
43 // Include system files
44 //------------------------------------------------------------------------------
45 
46 #include "Defines.h"
47 
48 //------------------------------------------------------------------------------
49 // Modbus responce code definition
50 //------------------------------------------------------------------------------
51 
52 #define MODBUS_RESPONCE_CODE_ILLEGAL_FUNCTION ( (U8)0x01 )
53 #define MODBUS_RESPONCE_CODE_ILLEGAL_DATA_ADDRESS ( (U8)0x02 )
54 #define MODBUS_RESPONCE_CODE_ILLEGAL_DATA_VALUE ( (U8)0x03 )
55 #define MODBUS_RESPONCE_CODE_SERVER_DEVICE_FAILURE ( (U8)0x04 )
56 #define MODBUS_RESPONCE_CODE_ACKNOWLEDGE ( (U8)0x05 )
57 #define MODBUS_RESPONCE_CODE_SERVER_DEVICE_BUSY ( (U8)0x06 )
58 #define MODBUS_RESPONCE_CODE_MEMORY_PARITY_ERROR ( (U8)0x07 )
59 #define MODBUS_RESPONCE_CODE_GATEWAY_PATH_UNAVAILABLE ( (U8)0x0A )
60 #define MODBUS_RESPONCE_CODE_GATAWAY_TARGET_DEVICE_FAILED_TO_RESPOND ( (U8)0x0B )
61 
62 //------------------------------------------------------------------------------
63 // Modbus function code definition
64 //------------------------------------------------------------------------------
65 
66 #define MODBUS_FC_0x01_READ_COILS ( (U8)0x01 )
67 #define MODBUS_FC_0x02_READ_DISCRETE_INPUTS ( (U8)0x02 )
68 #define MODBUS_FC_0x03_READ_HOLDING_REGISTERS ( (U8)0x03 )
69 #define MODBUS_FC_0x04_READ_INPUT_REGISTER ( (U8)0x04 )
70 #define MODBUS_FC_0x05_WRITE_SINGLE_COIL ( (U8)0x05 )
71 #define MODBUS_FC_0x06_WRITE_SINGLE_REGISTER ( (U8)0x06 )
72 #define MODBUS_FC_0x07_READ_EXCEPTION_STATUS ( (U8)0x07 )
73 #define MODBUS_FC_0x08_DIAGNOSTIC ( (U8)0x08 )
74 #define MODBUS_FC_0x0B_GET_COMMUNICATION_EVENT_COUNTER ( (U8)0x0B )
75 #define MODBUS_FC_0x0C_GET_COMMUNICATION_EVENT_LOG ( (U8)0x0C )
76 #define MODBUS_FC_0x0F_WRITE_MULTIPLE_COILS ( (U8)0x0F )
77 #define MODBUS_FC_0x10_WRITE_MULTIPLE_REGISTERS ( (U8)0x10 )
78 #define MODBUS_FC_0x11_REPORT_SERVER_ID ( (U8)0x11 )
79 #define MODBUS_FC_0x14_READ_FILE_RECORD ( (U8)0x14 )
80 #define MODBUS_FC_0x15_WRITE_FILE_RECORD ( (U8)0x15 )
81 #define MODBUS_FC_0x16_MASK_WRITE_REGISTER ( (U8)0x16 )
82 #define MODBUS_FC_0x17_READ_WRITE_MULTIPLE_REGISTERS ( (U8)0x17 )
83 #define MODBUS_FC_0x18_READ_FIFO_QUEUE ( (U8)0x18 )
84 #define MODBUS_FC_0x2B_ENCAPSULATED_INTERFACE_TRANSPORT ( (U8)0x2B )
85 
86 #define MODBUS_MEI_0x0D_CANOPEN_GENERAL_REFERENCE ( (U8)0x0D )
87 #define MODBUS_MEI_0x0E_READ_DEVICE_IDENTEFICATION ( (U8)0x0E )
88 
89 //------------------------------------------------------------------------------
90 
91 //! @brief MODBUS PDU buffer side definition
92 #define MODBUS_PDU_BUFFER_SIZE (0x100)
93 
94 //------------------------------------------------------------------------------
95 // Class fefinitions
96 //------------------------------------------------------------------------------
97 
98 //! @brief MODBUS generic PDU base class
100 {
101 
102  //----------------------------------------------------------------------------
103  // Public defines, methods and variables
104  //----------------------------------------------------------------------------
105 
106  public:
107 
108  //--------------------------------------------------------------------------
109  // Public methods
110  //--------------------------------------------------------------------------
111 
112  //!-------------------------------------------------------------------------
113  //! @brief Class constructor method
114  //! @note None
115  //! \par Override
116  //! Not allowed
117  //! @attention Don't call this method directly.
118  //--------------------------------------------------------------------------
119 
121 
122  //!-------------------------------------------------------------------------
123  //! @brief Class destructor method
124  //! @note None
125  //! \par Override
126  //! Not allowed
127  //! @attention Don't call this method directly.
128  //--------------------------------------------------------------------------
129 
130  virtual ~TMODBUS_PDU_Base();
131 
132  //!-------------------------------------------------------------------------
133  //! @brief Returns PDU buffer empty status
134  //! @note This function is not implemented
135  //! @return BOOL - PDU buffer status
136  //! @retval false - PDU buffer is not empty
137  //! @retval true - PDU buffer is empty
138  //! \par Override
139  //! Not allowed
140  //! @attention None
141  //--------------------------------------------------------------------------
142 
143  BOOL Is_Empty();
144 
145  //!-------------------------------------------------------------------------
146  //! @brief Returns PDU buffer size
147  //! @note None
148  //! @return U32 - PDU buffer size
149  //! \par Override
150  //! Not allowed
151  //! @attention None
152  //--------------------------------------------------------------------------
153 
154  U32 Get_Size();
155 
156  //!-------------------------------------------------------------------------
157  //! @brief Returns actual PDU buffer index
158  //! @note None
159  //! @return U32 - Actual PDU buffer index
160  //! \par Override
161  //! Not allowed
162  //! @attention None
163  //--------------------------------------------------------------------------
164 
165  U32 Get_Index();
166 
167  //!-------------------------------------------------------------------------
168  //! @brief Set PDU buffer index
169  //! @note None
170  //! @param [in] u32_New_Index - New PDU buffer index
171  //! \par Override
172  //! Not allowed
173  //! @attention None
174  //--------------------------------------------------------------------------
175 
176  VOID Set_Index( U32 u32_New_Index );
177 
178  //!-------------------------------------------------------------------------
179  //! @brief Returns pointer to PDU buffer
180  //! @note None
181  //! @return U8* - Pointer to PDU buffer
182  //! \par Override
183  //! Not allowed
184  //! @attention None
185  //--------------------------------------------------------------------------
186 
188 
189  //!-------------------------------------------------------------------------
190  //! @brief PDU initialisation method
191  //! @note None
192  //! @return None
193  //! \par Override
194  //! Not allowed
195  //! @attention None
196  //--------------------------------------------------------------------------
197 
198  VOID Init();
199 
200  //!-------------------------------------------------------------------------
201  //! @brief PDU initialisation with specifird slave address
202  //! @note None
203  //! @param [in] u8_Slave_Address - New slave address
204  //! @return None
205  //! \par Override
206  //! Not allowed
207  //! @attention None
208  //--------------------------------------------------------------------------
209 
210  VOID Init( U8 u8_Slave_Address );
211 
212  //!-------------------------------------------------------------------------
213  //! @brief PDU finalization method
214  //! @note None
215  //! @return None
216  //! \par Override
217  //! Not allowed
218  //! @attention None
219  //--------------------------------------------------------------------------
220 
221  virtual BOOL Done() = 0;
222 
223  //!-------------------------------------------------------------------------
224  //! @brief Set slave address
225  //! @note None
226  //! @param [in] u8_Slave_Address - New slave address
227  //! \par Override
228  //! Not allowed
229  //! @attention None
230  //--------------------------------------------------------------------------
231 
232  VOID Set_Slave_Address( U8 u8_Slave_Address );
233 
234  //!-------------------------------------------------------------------------
235  //! @brief Returns actual slave address
236  //! @note None
237  //! @return U8 - Actual slave address
238  //! \par Override
239  //! Not allowed
240  //! @attention None
241  //--------------------------------------------------------------------------
242 
244 
245  //!-------------------------------------------------------------------------
246  //! @brief Set function code method
247  //! @note None
248  //! @param [in] u8_Function_Code - Function code
249  //! \par Override
250  //! Not allowed
251  //! @attention None
252  //--------------------------------------------------------------------------
253 
254  VOID Set_Function_Code( U8 u8_Function_Code );
255 
256  //!-------------------------------------------------------------------------
257  //! @brief Get function code method
258  //! @note None
259  //! @return U8 - Function code
260  //! \par Override
261  //! Not allowed
262  //! @attention None
263  //--------------------------------------------------------------------------
264 
266 
267  //!-------------------------------------------------------------------------
268  //! @brief Read U8 value at actual address method
269  //! @note None
270  //! @return U8 - Read value
271  //! \par Override
272  //! Not allowed
273  //! @attention None
274  //--------------------------------------------------------------------------
275 
276  U8 Read_U8_Value();
277 
278  //!-------------------------------------------------------------------------
279  //! @brief Read U8 value at specifird address method
280  //! @note None
281  //! @param [in] u32_Address - Address for reading
282  //! @return U8 - Read value
283  //! \par Override
284  //! Not allowed
285  //! @attention None
286  //--------------------------------------------------------------------------
287 
288  U8 Read_U8_Value( U32 u32_Address );
289 
290  //!-------------------------------------------------------------------------
291  //! @brief Append U8 value at the buffer end method
292  //! @note None
293  //! @param [in] u8_Value - Append velue
294  //! \par Override
295  //! Not allowed
296  //! @attention None
297  //--------------------------------------------------------------------------
298 
299  VOID Append_U8_Value( U8 u8_Value );
300 
301  //!-------------------------------------------------------------------------
302  //! @brief Read U16 value at specifird address method
303  //! @note None
304  //! @return U16 - Read value
305  //! \par Override
306  //! Not allowed
307  //! @attention None
308  //--------------------------------------------------------------------------
309 
311 
312  //!-------------------------------------------------------------------------
313  //! @brief Read U16 value at specifird address method
314  //! @note None
315  //! @param [in] u32_Address - Address for reading
316  //! @return U16 - Read value
317  //! \par Override
318  //! Not allowed
319  //! @attention None
320  //--------------------------------------------------------------------------
321 
322  U16 Read_U16_Value( U32 u32_Address );
323 
324  //!-------------------------------------------------------------------------
325  //! @brief Append U16 value at the buffer end method
326  //! @note None
327  //! @param [in] u16_Value - Append velue
328  //! \par Override
329  //! Not allowed
330  //! @attention None
331  //--------------------------------------------------------------------------
332 
333  VOID Append_U16_Value( U16 u16_Value );
334 
335  //!-------------------------------------------------------------------------
336  //! @brief Read U32 value at specifird address method
337  //! @note None
338  //! @return U32 - Read value
339  //! \par Override
340  //! Not allowed
341  //! @attention None
342  //--------------------------------------------------------------------------
343 
345 
346  //!-------------------------------------------------------------------------
347  //! @brief Read U32 value at specifird address method
348  //! @note None
349  //! @param [in] u32_Address - Address for reading
350  //! @return U32 - Read value
351  //! \par Override
352  //! Not allowed
353  //! @attention None
354  //--------------------------------------------------------------------------
355 
356  U32 Read_U32_Value( U32 u32_Address );
357 
358  //!-------------------------------------------------------------------------
359  //! @brief Append U32 value at the buffer end method
360  //! @note None
361  //! @param [in] u32_Value - Append velue
362  //! \par Override
363  //! Not allowed
364  //! @attention None
365  //--------------------------------------------------------------------------
366 
367  VOID Append_U32_Value( U32 u32_Value );
368 
369  //!-------------------------------------------------------------------------
370  //! @brief Read F32 value at specifird address method
371  //! @note None
372  //! @return F32 - Read value
373  //! \par Override
374  //! Not allowed
375  //! @attention None
376  //--------------------------------------------------------------------------
377 
379 
380  //!-------------------------------------------------------------------------
381  //! @brief Read F32 value at specifird address method
382  //! @note None
383  //! @param [in] u32_Address - Address for reading
384  //! @return F32 - Read value
385  //! \par Override
386  //! Not allowed
387  //! @attention None
388  //--------------------------------------------------------------------------
389 
390  F32 Read_F32_Value( U32 u32_Address );
391 
392  //!-------------------------------------------------------------------------
393  //! @brief Append F32 value at the buffer end method
394  //! @note None
395  //! @param [in] f32_Value - Append velue
396  //! \par Override
397  //! Not allowed
398  //! @attention None
399  //--------------------------------------------------------------------------
400 
401  VOID Append_F32_Value( F32 f32_Value );
402 
403  //!-------------------------------------------------------------------------
404  //! @brief Get cyclic redundancy check method
405  //! @note None
406  //! @return U16 - Read value
407  //! \par Override
408  //! Not allowed
409  //! @attention None
410  //--------------------------------------------------------------------------
411 
412  U16 Get_CRC();
413 
414  //!-------------------------------------------------------------------------
415  //! @brief Return PDU overrun status method
416  //! @note None
417  //! @return BOOL - PDU overrun status
418  //! @retval false - PDU is not overrun
419  //! @retval true - PDU is overrun
420  //! \par Override
421  //! Not allowed
422  //! @attention None
423  //--------------------------------------------------------------------------
424 
425  BOOL Is_Overrun();
426 
427  //!-------------------------------------------------------------------------
428  //! @brief Return PDU size valid status method
429  //! @note None
430  //! @return BOOL - PDU size valid status
431  //! @retval false - PDU size invalid
432  //! @retval true - PDU size valid
433  //! \par Override
434  //! Not allowed
435  //! @attention None
436  //--------------------------------------------------------------------------
437 
439 
440  //--------------------------------------------------------------------------
441  // Public variables
442  //--------------------------------------------------------------------------
443 
444  //! @brief PDU received CRC
446 
447  //----------------------------------------------------------------------------
448  // Protected definies, methods and variables
449  //----------------------------------------------------------------------------
450 
451  protected:
452 
453  //--------------------------------------------------------------------------
454  // protected variables
455  //--------------------------------------------------------------------------
456 
457  //! @brief PDU buffer data field
459 
460  //! @brief PDU buffer size in byte
462 
463  //! @brief PDU buffer index
465 
466  //! @brief PDU buffer CRC
467  U16 u16_PDU_CRC = (U32)0x0000;
468 
469  //----------------------------------------------------------------------------
470  // Private defines, methods and variables
471  //----------------------------------------------------------------------------
472 
473  private:
474 
475 };
476 
477 //------------------------------------------------------------------------------
478 // End of file
479 //------------------------------------------------------------------------------
ConOpSys data type definitions header file.
int BOOL
Boolean datatype definition.
Definition: Defines.h:124
unsigned char U8
Binary 8-Bit unsigned integer datatype defenition.
Definition: Defines.h:183
void VOID
Datatypesess datatype definition.
Definition: Defines.h:105
unsigned long U32
Binary 32-Bit unsigned integer datatype defenition.
Definition: Defines.h:203
float F32
IEEE-754 32-Bit single presession floating point numbers datatype defenition.
Definition: Defines.h:324
unsigned short U16
Binary 16-Bit unsigned integer datatype defenition.
Definition: Defines.h:193
#define MODBUS_PDU_BUFFER_SIZE
MODBUS PDU buffer side definition.
Definition: MODBUS_PDU.h:92
MODBUS generic PDU base class.
Definition: MODBUS_PDU.h:100
TMODBUS_PDU_Base()
Class constructor method.
Definition: MODBUS_PDU.cpp:68
VOID Set_Index(U32 u32_New_Index)
Set PDU buffer index.
Definition: MODBUS_PDU.cpp:120
BOOL Is_Empty()
Returns PDU buffer empty status.
Definition: MODBUS_PDU.cpp:84
U32 Get_Index()
Returns actual PDU buffer index.
Definition: MODBUS_PDU.cpp:108
U8 * Get_Buffer_Pointer()
Returns pointer to PDU buffer.
Definition: MODBUS_PDU.cpp:228
BOOL Is_Size_Valid()
Return PDU size valid status method.
Definition: MODBUS_PDU.cpp:769
VOID Append_U8_Value(U8 u8_Value)
Append U8 value at the buffer end method.
Definition: MODBUS_PDU.cpp:276
U8 Read_U8_Value()
Read U8 value at actual address method.
Definition: MODBUS_PDU.cpp:240
virtual BOOL Done()=0
PDU finalization method.
U32 Get_Size()
Returns PDU buffer size.
Definition: MODBUS_PDU.cpp:96
U16 u16_PDU_CRC
PDU buffer CRC.
Definition: MODBUS_PDU.h:467
U32 u32_PDU_Index
PDU buffer index.
Definition: MODBUS_PDU.h:464
VOID Append_U16_Value(U16 u16_Value)
Append U16 value at the buffer end method.
Definition: MODBUS_PDU.cpp:356
U16 Get_CRC()
Get cyclic redundancy check method.
Definition: MODBUS_PDU.cpp:745
U16 Read_U16_Value()
Read U16 value at specifird address method.
Definition: MODBUS_PDU.cpp:309
U32 Read_U32_Value()
Read U32 value at specifird address method.
Definition: MODBUS_PDU.cpp:413
U32 u32_PDU_Size
PDU buffer size in byte.
Definition: MODBUS_PDU.h:461
VOID Set_Slave_Address(U8 u8_Slave_Address)
Set slave address.
Definition: MODBUS_PDU.cpp:179
VOID Append_F32_Value(F32 f32_Value)
Append F32 value at the buffer end method.
Definition: MODBUS_PDU.cpp:640
U8 Get_Slave_Address()
Returns actual slave address.
Definition: MODBUS_PDU.cpp:192
U16 u16_Received_CRC
PDU received CRC.
Definition: MODBUS_PDU.h:445
VOID Append_U32_Value(U32 u32_Value)
Append U32 value at the buffer end method.
Definition: MODBUS_PDU.cpp:474
VOID Init()
PDU initialisation method.
Definition: MODBUS_PDU.cpp:134
F32 Read_F32_Value()
Read F32 value at specifird address method.
Definition: MODBUS_PDU.cpp:579
U8 Get_Function_Code()
Get function code method.
Definition: MODBUS_PDU.cpp:216
U8 u8_Buffer[MODBUS_PDU_BUFFER_SIZE]
PDU buffer data field.
Definition: MODBUS_PDU.h:458
virtual ~TMODBUS_PDU_Base()
Class destructor method.
Definition: MODBUS_PDU.cpp:76
BOOL Is_Overrun()
Return PDU overrun status method.
Definition: MODBUS_PDU.cpp:757
VOID Set_Function_Code(U8 u8_Function_Code)
Set function code method.
Definition: MODBUS_PDU.cpp:204