Agile Modbus  1.1.4
Lightweight modbus protocol stack.
agile_modbus_rtu.c
Go to the documentation of this file.
1 
14 #include "agile_modbus.h"
15 
16 #if AGILE_MODBUS_USING_RTU
17 
18 #include "agile_modbus_rtu.h"
19 
28 static const uint8_t _table_crc_hi[] =
29  {
30  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
31  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
32  0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
33  0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
34  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
35  0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
36  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
37  0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
38  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
39  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
40  0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
41  0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
42  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
43  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
44  0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
45  0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
46  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
47  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
48  0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
49  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
50  0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
51  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
52  0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
53  0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
54  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
55  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40};
56 
58 static const uint8_t _table_crc_lo[] =
59  {
60  0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
61  0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
62  0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
63  0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
64  0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
65  0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
66  0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
67  0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
68  0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
69  0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
70  0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
71  0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
72  0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
73  0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
74  0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
75  0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
76  0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
77  0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
78  0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
79  0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
80  0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
81  0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
82  0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
83  0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
84  0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
85  0x43, 0x83, 0x41, 0x81, 0x80, 0x40};
100 static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length)
101 {
102  uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
103  uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
104  unsigned int i; /* will index into CRC lookup */
105 
106  /* pass through message buffer */
107  while (buffer_length--) {
108  i = crc_hi ^ *buffer++; /* calculate the CRC */
109  crc_hi = crc_lo ^ _table_crc_hi[i];
110  crc_lo = _table_crc_lo[i];
111  }
112 
113  return (crc_hi << 8 | crc_lo);
114 }
115 
122 static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
123 {
124  ctx->slave = slave;
125  return 0;
126 }
127 
138  int addr, int nb,
139  uint8_t *req)
140 {
141  req[0] = ctx->slave;
142  req[1] = function;
143  req[2] = addr >> 8;
144  req[3] = addr & 0x00ff;
145  req[4] = nb >> 8;
146  req[5] = nb & 0x00ff;
147 
149 }
150 
158 {
159  rsp[0] = sft->slave;
160  rsp[1] = sft->function;
161 
163 }
164 
172 static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
173 {
174  (void)req;
175  (*req_length) -= AGILE_MODBUS_RTU_CHECKSUM_LENGTH;
176  /* No TID */
177  return 0;
178 }
179 
187 static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
188 {
189  uint16_t crc = agile_modbus_rtu_crc16(req, req_length);
190  req[req_length++] = crc >> 8;
191  req[req_length++] = crc & 0x00FF;
192 
193  return req_length;
194 }
195 
203 static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
204 {
205  uint16_t crc_calculated;
206  uint16_t crc_received;
207  (void)ctx;
208  crc_calculated = agile_modbus_rtu_crc16(msg, msg_length - 2);
209  crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
210 
211  /* Check CRC of msg */
212  if (crc_calculated == crc_received)
213  return msg_length;
214 
215  return -1;
216 }
217 
227 static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
228  const uint8_t *rsp, int rsp_length)
229 {
230  /* Check responding slave is the slave we requested (except for broacast
231  * request) */
232  (void)ctx;
233  (void)rsp_length;
234  if (req[0] != rsp[0] && req[0] != AGILE_MODBUS_BROADCAST_ADDRESS)
235  return -1;
236 
237  return 0;
238 }
239 
252  {
264 
282 int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
283 {
284  agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
286  ctx->_ctx.backend_data = ctx;
287 
288  return 0;
289 }
290 
299 #endif /* AGILE_MODBUS_USING_RTU */
Agile Modbus software package common header file.
Agile Modbus package RTU header file.
void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
initialize modbus handle
Definition: agile_modbus.c:257
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU.
Definition: agile_modbus.h:165
#define AGILE_MODBUS_BROADCAST_ADDRESS
Modbus broadcast address.
Definition: agile_modbus.h:69
#define AGILE_MODBUS_RTU_HEADER_LENGTH
#define AGILE_MODBUS_RTU_MAX_ADU_LENGTH
#define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH
#define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH
#define AGILE_MODBUS_RTU_CHECKSUM_LENGTH
int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
RTU initialization.
static const uint8_t _table_crc_hi[]
Table of CRC values for high-order byte.
static const agile_modbus_backend_t agile_modbus_rtu_backend
RTU backend interface.
static const uint8_t _table_crc_lo[]
Table of CRC values for low-order byte.
static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
RTU builds the basic response message interface (header message)
static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
RTU pre-send data interface.
static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
RTU builds the basic request message interface (header message)
static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
RTU sets the address interface.
static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length)
RTU CRC16 calculation.
static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
RTU check received data integrity interface (CRC16 comparison)
static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
RTU pre-check confirmation interface (request response address comparison)
static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
RTU ready response interface.
Agile Modbus backend interface structure.
Definition: agile_modbus.h:198
RTU structure.
agile_modbus_t _ctx
modbus handle
contains the modbus header parameter structure
Definition: agile_modbus.h:187
int slave
slave address
Definition: agile_modbus.h:188
int function
function code
Definition: agile_modbus.h:189
Agile Modbus structure.
Definition: agile_modbus.h:217
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
int slave
slave address
Definition: agile_modbus.h:218
void * backend_data
Backend data, pointing to RTU or TCP structure.
Definition: agile_modbus.h:228