0
|
1 |
/*
|
|
2 |
* Copyright (c) 2001,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 MODBUS_RTU_PRIVATE_H
|
|
26 |
#define MODBUS_RTU_PRIVATE_H
|
|
27 |
|
|
28 |
#include "mb_types.h" /* get the data types */
|
|
29 |
#include "mb_util.h"
|
|
30 |
|
|
31 |
|
|
32 |
/* serial port default configuration... */
|
|
33 |
#define DEF_DATA_BITS 8
|
|
34 |
#define DEF_STOP_BITS_PAR 1 /* default stop bits if parity is used */
|
|
35 |
#define DEF_STOP_BITS_NOP 2 /* default stop bits if parity is not used */
|
|
36 |
#define DEF_BAUD_RATE 9600
|
|
37 |
|
|
38 |
|
|
39 |
/* Send retries of rtu frames... */
|
|
40 |
#define RTU_FRAME_SEND_RETRY 1
|
|
41 |
/* NOTES:
|
|
42 |
* - the above are the retries at the layer1 level,
|
|
43 |
* higher layers may decide to retry for themselves!
|
|
44 |
*/
|
|
45 |
|
|
46 |
|
|
47 |
/* Buffer sizes... */
|
|
48 |
/* We use double the maximum frame length for the read buffer,
|
|
49 |
* due to the algorithm used to work around aborted frames.
|
|
50 |
*/
|
|
51 |
#define RECV_BUFFER_SIZE_SMALL (MAX_RTU_FRAME_LENGTH + 10)
|
|
52 |
#define RECV_BUFFER_SIZE_LARGE (2 * MAX_RTU_FRAME_LENGTH)
|
|
53 |
|
|
54 |
|
|
55 |
/* Frame lengths... */
|
|
56 |
|
|
57 |
/* The number of bytes in each frame format, excluding CRC.
|
|
58 |
*
|
|
59 |
* BYTE_COUNT_3 denotes that third byte of frame contains the number of bytes;
|
|
60 |
* - total number of bytes in frame is
|
|
61 |
* BYTE_COUNT_3_HEADER + byte_count
|
|
62 |
* BYTE_COUNT_34 denotes that third+fourth bytes of frame contain the number of bytes;
|
|
63 |
* - total number of bytes in frame is
|
|
64 |
* BYTE_COUNT_34_HEADER + byte_count
|
|
65 |
* BYTE_COUNT_7 denotes that seventh byte of frame contain the number of bytes;
|
|
66 |
* - total number of bytes in frame is
|
|
67 |
* BYTE_COUNT_7_HEADER + byte_count
|
|
68 |
* BYTE_COUNT_11 denotes that eleventh byte of frame contain the number of bytes;
|
|
69 |
* - total number of bytes in frame is
|
|
70 |
* BYTE_COUNT_11_HEADER + byte_count
|
|
71 |
* BYTE_COUNT_U denotes unknown number of bytes;
|
|
72 |
*/
|
|
73 |
|
|
74 |
#define BYTE_COUNT_3_HEADER 3
|
|
75 |
#define BYTE_COUNT_34_HEADER 4
|
|
76 |
#define BYTE_COUNT_7_HEADER 7
|
|
77 |
#define BYTE_COUNT_11_HEADER 11
|
|
78 |
|
|
79 |
#define BYTE_COUNT_3 (-3)
|
|
80 |
#define BYTE_COUNT_34 (-34)
|
|
81 |
#define BYTE_COUNT_7 (-7)
|
|
82 |
#define BYTE_COUNT_11 (-11)
|
|
83 |
#define BYTE_COUNT_U (-128)
|
|
84 |
|
|
85 |
|
|
86 |
#define MAX_FUNCTION_CODE 0x18
|
|
87 |
|
|
88 |
#define MIN_FRAME_LENGTH 3
|
|
89 |
#define EXCEPTION_FRAME_LENGTH 3
|
|
90 |
|
|
91 |
static i8 query_frame_lengths[MAX_FUNCTION_CODE+1] = {
|
|
92 |
/* 0x00 */ 0, /* unused */
|
|
93 |
/* 0x01 */ 6, /* Read Coil Status */
|
|
94 |
/* 0x02 */ 6, /* Read Input Status */
|
|
95 |
/* 0x03 */ 6, /* Read Holding Registers */
|
|
96 |
/* 0x04 */ 6, /* Read Input Registers */
|
|
97 |
/* 0x05 */ 6, /* Force Single Coil */
|
|
98 |
/* 0x06 */ 6, /* Preset Single Register */
|
|
99 |
/* 0x07 */ 2, /* Read Exception Status */
|
|
100 |
/* 0x08 */ 4, /* Diagnostics */
|
|
101 |
/* 0x09 */ BYTE_COUNT_U, /* Program 484 */
|
|
102 |
/* 0x0A */ BYTE_COUNT_U, /* Poll 484 */
|
|
103 |
/* 0x0B */ 2, /* Fetch Comm. Event Counter */
|
|
104 |
/* 0x0C */ 2, /* Fetch Comm. Event Log */
|
|
105 |
/* 0x0D */ BYTE_COUNT_U, /* Program Controller */
|
|
106 |
/* 0x0E */ BYTE_COUNT_U, /* Poll Controller */
|
|
107 |
/* 0x0F */ BYTE_COUNT_7, /* Force Multiple Coils */
|
|
108 |
/* 0x10 */ BYTE_COUNT_7, /* Preset Multiple Registers */
|
|
109 |
/* 0x11 */ 2, /* Report Slave ID */
|
|
110 |
/* 0x12 */ BYTE_COUNT_U, /* Program 884/M84 */
|
|
111 |
/* 0x13 */ BYTE_COUNT_U, /* Reset. Comm. Link */
|
|
112 |
/* 0x14 */ BYTE_COUNT_3, /* Read General Reference */
|
|
113 |
/* 0x15 */ BYTE_COUNT_3, /* Write General Reference */
|
|
114 |
/* 0x16 */ 8, /* Mask Write 4X Register */
|
|
115 |
/* 0x17 */ BYTE_COUNT_11, /* Read/Write 4x Register */
|
|
116 |
/* 0x18 */ 4 /* Read FIFO Queue */
|
|
117 |
};
|
|
118 |
|
|
119 |
static i8 response_frame_lengths[MAX_FUNCTION_CODE+1] = {
|
|
120 |
/* 0x00 */ 0, /* unused */
|
|
121 |
/* 0x01 */ BYTE_COUNT_3, /* Read Coil Status */
|
|
122 |
/* 0x02 */ BYTE_COUNT_3, /* Read Input Status */
|
|
123 |
/* 0x03 */ BYTE_COUNT_3, /* Read Holding Registers */
|
|
124 |
/* 0x04 */ BYTE_COUNT_3, /* Read Input Registers */
|
|
125 |
/* 0x05 */ 6, /* Force Single Coil */
|
|
126 |
/* 0x06 */ 6, /* Preset Single Register */
|
|
127 |
/* 0x07 */ 3, /* Read Exception Status */
|
|
128 |
/* 0x08 */ 6,/*see (1)*/ /* Diagnostics */
|
|
129 |
/* 0x09 */ BYTE_COUNT_U, /* Program 484 */
|
|
130 |
/* 0x0A */ BYTE_COUNT_U, /* Poll 484 */
|
|
131 |
/* 0x0B */ 6, /* Fetch Comm. Event Counter */
|
|
132 |
/* 0x0C */ BYTE_COUNT_3, /* Fetch Comm. Event Log */
|
|
133 |
/* 0x0D */ BYTE_COUNT_U, /* Program Controller */
|
|
134 |
/* 0x0E */ BYTE_COUNT_U, /* Poll Controller */
|
|
135 |
/* 0x0F */ 6, /* Force Multiple Coils */
|
|
136 |
/* 0x10 */ 6, /* Preset Multiple Registers */
|
|
137 |
/* 0x11 */ BYTE_COUNT_3, /* Report Slave ID */
|
|
138 |
/* 0x12 */ BYTE_COUNT_U, /* Program 884/M84 */
|
|
139 |
/* 0x13 */ BYTE_COUNT_U, /* Reset. Comm. Link */
|
|
140 |
/* 0x14 */ BYTE_COUNT_3, /* Read General Reference */
|
|
141 |
/* 0x15 */ BYTE_COUNT_3, /* Write General Reference */
|
|
142 |
/* 0x16 */ 8, /* Mask Write 4X Register */
|
|
143 |
/* 0x17 */ BYTE_COUNT_3, /* Read/Write 4x Register */
|
|
144 |
/* 0x18 */ BYTE_COUNT_34 /* Read FIFO Queue */
|
|
145 |
};
|
|
146 |
|
|
147 |
/* NOTE (1):
|
|
148 |
* The diagnostic function (0x08) has sub-functions. In particular,
|
|
149 |
* sub-function 21 (0x15) has two sub-sub-functions. In the very
|
|
150 |
* particular case of *one* of these sub-sub-functions, the reply
|
|
151 |
* frame does *not* have a size of 4, but is variable in length
|
|
152 |
* and includes a byte counter.
|
|
153 |
* To take this into account in the table would require an extra two
|
|
154 |
* tables.
|
|
155 |
* The above length has been hardcoded into the frame_length() function
|
|
156 |
* (in file modbus_rtu.c)
|
|
157 |
*/
|
|
158 |
|
|
159 |
|
|
160 |
#define FALSE 0
|
|
161 |
#define TRUE 1
|
|
162 |
|
|
163 |
|
|
164 |
#endif /* MODBUS_RTU_PRIVATE_H */
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|