|
1 /* |
|
2 * Copyright (c) 2002,2016 Mario de Sousa (msousa@fe.up.pt) |
|
3 * |
|
4 * This file is part of the Modbus library for Beremiz and matiec. |
|
5 * |
|
6 * This Modbus library is free software: you can redistribute it and/or modify |
|
7 * it under the terms of the GNU Lesser General Public License as published by |
|
8 * the Free Software Foundation, either version 3 of the License, or |
|
9 * (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, but |
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
|
14 * General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU Lesser General Public License |
|
17 * along with this Modbus library. If not, see <http://www.gnu.org/licenses/>. |
|
18 * |
|
19 * This code is made available on the understanding that it will not be |
|
20 * used in safety-critical situations without a full and competent review. |
|
21 */ |
|
22 |
|
23 |
|
24 |
|
25 #ifndef MB_UTIL_H |
|
26 #define MB_UTIL_H |
|
27 |
|
28 /* This file has constants related to the modbus protocol */ |
|
29 /* |
|
30 * Some of these constants are specific to the layer two protocols |
|
31 * (i.e. master and slave), while others are specific of the |
|
32 * layer one protocols (i.e. rtu, ascii, tcp). |
|
33 * |
|
34 * a) Unfortunately, due to the nature of the modbus protocol, that does not |
|
35 * include a frame size field in the layer 1 frame (see note 1), and the |
|
36 * fact that we are implementing it at the user level, the implementation |
|
37 * of some layer 1 protocols need to know the content of the layer 2 protocol |
|
38 * in order to determine the size of the frame. |
|
39 * |
|
40 * b) The layer two message formats are in fact the same, just reversing the role |
|
41 * being played (master or slave). |
|
42 * |
|
43 * Bothe a) and b) mean we need the same modbus protocol constants in several files. |
|
44 * It ends up making more sense to put them all together in a single file, which |
|
45 * makes updating easier, even though we are trying to strictly seperate the layer 1 |
|
46 * and layer 2 protocols. |
|
47 * |
|
48 * |
|
49 * |
|
50 * Notes: |
|
51 * (1) There is no layer 1 field with the frame size, nevertheless this |
|
52 * size can be determined indirectly due to timing restrictions on the rtu |
|
53 * protocol. Unfortunately, due to the fact that we are implementing |
|
54 * it at the user level, we are not guaranteed to detect these timings |
|
55 * correctly, and therefore have to rely on layer 2 protocol info to |
|
56 * determine the frame size. |
|
57 * For the ascii protocol, the frame size is determined indirectly by |
|
58 * a frame header and tail, so we do not use layer 2 protocol info. |
|
59 */ |
|
60 |
|
61 |
|
62 /* Layer 2 Frame Structure... */ |
|
63 /* Valid for both master and slave protocols */ |
|
64 #define L2_FRAME_HEADER_LENGTH 6 |
|
65 #define L2_FRAME_BYTECOUNT_LENGTH 1 |
|
66 #define L2_FRAME_DATABYTES_LENGTH 255 |
|
67 #define MAX_L2_FRAME_LENGTH (L2_FRAME_HEADER_LENGTH + L2_FRAME_BYTECOUNT_LENGTH + \ |
|
68 L2_FRAME_DATABYTES_LENGTH) |
|
69 |
|
70 #define L2_FRAME_SLAVEID_OFS 0 |
|
71 #define L2_FRAME_FUNCTION_OFS 1 |
|
72 |
|
73 /* Layer 1 - Ascii Frame sizes... */ |
|
74 #define L2_TO_ASC_CODING 2 /* number of ascii bytes used to code a Layer 2 frame byte */ |
|
75 #define ASC_FRAME_HEADER_LENGTH 1 |
|
76 #define ASC_FRAME_HEADER ':' |
|
77 #define ASC_FRAME_TAIL_LENGTH 2 |
|
78 #define ASC_FRAME_TAIL_0 '\13' /* 'CR' */ |
|
79 #define ASC_FRAME_TAIL_1 '\10' /* 'LF' */ |
|
80 #define ASC_FRAME_LRC_LENGTH 2 |
|
81 |
|
82 /* Layer 1 - RTU Frame sizes... */ |
|
83 #define RTU_FRAME_CRC_LENGTH 2 |
|
84 |
|
85 /* Layer 1 - TCP Frame sizes... */ |
|
86 #define TCP_HEADER_LENGTH 6 |
|
87 |
|
88 /* Global Frame sizes */ |
|
89 #define MAX_RTU_FRAME_LENGTH MAX_L2_FRAME_LENGTH + RTU_FRAME_CRC_LENGTH |
|
90 #define MAX_ASC_FRAME_LENGTH ((MAX_L2_FRAME_LENGTH * L2_TO_ASC_CODING) + \ |
|
91 ASC_FRAME_HEADER_LENGTH + ASC_FRAME_TAIL_LENGTH + \ |
|
92 ASC_FRAME_LRC_LENGTH) |
|
93 |
|
94 |
|
95 /* Modbus Exception codes */ |
|
96 #define ERR_ILLEGAL_FUNCTION 0x01 |
|
97 #define ERR_ILLEGAL_DATA_ADDRESS 0x02 |
|
98 #define ERR_ILLEGAL_DATA_VALUE 0x03 |
|
99 #define ERR_SLAVE_DEVICE_FAILURE 0x04 |
|
100 #define ERR_ACKNOWLEDGE 0x05 |
|
101 #define ERR_SLAVE_DEVICE_BUSY 0x06 |
|
102 #define ERR_NEGATIVE_ACKNOWLEDGE 0x07 |
|
103 #define ERR_MEMORY_PARITY_ERROR 0x08 |
|
104 #define ERR_GATEWAY_PATH_UNAVAILABLE 0x0A |
|
105 #define ERR_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND 0x0B |
|
106 |
|
107 |
|
108 |
|
109 #endif /* MB_UTIL_H */ |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |