Agile Modbus 1.1.1
Lightweight modbus protocol stack.
agile_modbus.h
浏览该文件的文档.
1
14#ifndef __PKG_AGILE_MODBUS_H
15#define __PKG_AGILE_MODBUS_H
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include <stdint.h>
22
34#define AGILE_MODBUS_FC_READ_COILS 0x01
35#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02
36#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03
37#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04
38#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05
39#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
40#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07
41#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
42#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
43#define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11
44#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16
45#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
53#define AGILE_MODBUS_VERSION_STRING "AMB_1.1.0"
55#define AGILE_MODBUS_BROADCAST_ADDRESS 0
67#define AGILE_MODBUS_MAX_READ_BITS 2000
68#define AGILE_MODBUS_MAX_WRITE_BITS 1968
85#define AGILE_MODBUS_MAX_READ_REGISTERS 125
86#define AGILE_MODBUS_MAX_WRITE_REGISTERS 123
87#define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121
88#define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125
102#define AGILE_MODBUS_MAX_PDU_LENGTH 253
103
116#define AGILE_MODBUS_MAX_ADU_LENGTH 260
132enum {
146
150typedef enum {
154
165typedef enum {
169
173typedef struct agile_modbus_sft {
174 int slave;
175 int function;
176 int t_id;
178
184typedef struct agile_modbus_backend {
185 uint32_t backend_type;
186 uint32_t header_length;
188 uint32_t max_adu_length;
189 int (*set_slave)(agile_modbus_t *ctx, int slave);
190 int (*build_request_basis)(agile_modbus_t *ctx, int function, int addr,
191 int nb, uint8_t *req);
192 int (*build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp);
193 int (*prepare_response_tid)(const uint8_t *req, int *req_length);
194 int (*send_msg_pre)(uint8_t *req, int req_length);
195 int (*check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length);
196 int (*pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req,
197 const uint8_t *rsp, int rsp_length);
199
204 int slave;
205 uint8_t *send_buf;
207 uint8_t *read_buf;
210 agile_modbus_msg_type_t msg_type);
212 int msg_length, agile_modbus_msg_type_t msg_type);
215};
216
236 int nb;
237 uint8_t *buf;
239};
240
251
263void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
264int agile_modbus_set_slave(agile_modbus_t *ctx, int slave);
266 uint8_t (*cb)(agile_modbus_t *ctx, int function,
267 agile_modbus_msg_type_t msg_type));
269 int (*cb)(agile_modbus_t *ctx, uint8_t *msg,
270 int msg_length, agile_modbus_msg_type_t msg_type));
271int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type);
283int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb);
284int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
286int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
288int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
290int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
291int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status);
292int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length);
293int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value);
295int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src);
297int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src);
299int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
302 int write_addr, int write_nb,
303 const uint16_t *src,
304 int read_addr, int read_nb);
305int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
307int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest);
315int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
332int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict,
333 agile_modbus_slave_callback_t slave_cb, int *frame_length);
334void agile_modbus_slave_io_set(uint8_t *buf, int index, int status);
335uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index);
336void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data);
337uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index);
350/* Include RTU and TCP module */
351#include "agile_modbus_rtu.h"
352#include "agile_modbus_tcp.h"
353
354#ifdef __cplusplus
355}
356#endif
357
358#endif /* __PKG_AGILE_MODBUS_H */
Agile Modbus 软件包 RTU 头文件
Agile Modbus 软件包 TCP 头文件
void agile_modbus_set_compute_data_length_after_meta_cb(agile_modbus_t *ctx, int(*cb)(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type))
设置 modbus 对象的计算数据元之后要接收的数据长度回调函数
Definition: agile_modbus.c:297
void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
初始化 modbus 句柄
Definition: agile_modbus.c:257
int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type)
校验接收数据正确性
Definition: agile_modbus.c:312
int agile_modbus_set_slave(agile_modbus_t *ctx, int slave)
设置地址
Definition: agile_modbus.c:273
void agile_modbus_set_compute_meta_length_after_function_cb(agile_modbus_t *ctx, uint8_t(*cb)(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type))
设置 modbus 对象的计算功能码后要接收的数据元长度回调函数
Definition: agile_modbus.c:284
struct agile_modbus_backend agile_modbus_backend_t
Agile Modbus 后端接口结构体
agile_modbus_backend_type_t
Modbus 后端类型
Definition: agile_modbus.h:150
struct agile_modbus_sft agile_modbus_sft_t
包含 modbus 头部参数结构体
agile_modbus_msg_type_t
Modbus 收到消息类型
Definition: agile_modbus.h:165
@ AGILE_MODBUS_EXCEPTION_GATEWAY_PATH
Definition: agile_modbus.h:142
@ AGILE_MODBUS_EXCEPTION_UNKNOW
Definition: agile_modbus.h:144
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
Definition: agile_modbus.h:133
@ AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET
Definition: agile_modbus.h:143
@ AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE
Definition: agile_modbus.h:137
@ AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
Definition: agile_modbus.h:138
@ AGILE_MODBUS_EXCEPTION_NOT_DEFINED
Definition: agile_modbus.h:141
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
Definition: agile_modbus.h:135
@ AGILE_MODBUS_EXCEPTION_MEMORY_PARITY
Definition: agile_modbus.h:140
@ AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE
Definition: agile_modbus.h:136
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
Definition: agile_modbus.h:134
@ AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
Definition: agile_modbus.h:139
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU
Definition: agile_modbus.h:151
@ AGILE_MODBUS_BACKEND_TYPE_TCP
TCP
Definition: agile_modbus.h:152
@ AGILE_MODBUS_MSG_CONFIRMATION
服务器端的请求消息
Definition: agile_modbus.h:167
@ AGILE_MODBUS_MSG_INDICATION
主机端的请求消息
Definition: agile_modbus.h:166
int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value)
Definition: agile_modbus.c:721
int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb)
Definition: agile_modbus.c:900
int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:941
int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:883
int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:844
int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:704
int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:603
int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:564
int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src)
Definition: agile_modbus.c:751
int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:493
int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src)
Definition: agile_modbus.c:813
int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest)
Definition: agile_modbus.c:984
int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:663
int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:509
int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status)
Definition: agile_modbus.c:691
int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:734
int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask)
Definition: agile_modbus.c:861
int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:796
int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:647
int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:619
int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx)
Definition: agile_modbus.c:969
int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:548
int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length)
解析响应原始数据
int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length)
将原始数据打包成请求报文
int(* agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
从机回调函数
Definition: agile_modbus.h:250
int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict, agile_modbus_slave_callback_t slave_cb, int *frame_length)
从机数据处理
uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
读取从机 IO 状态
void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
从机 IO 设置
uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
读取从机寄存器数据
void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
从机寄存器设置
Agile Modbus 后端接口结构体
Definition: agile_modbus.h:184
int(* check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
检查接收数据完整性接口
Definition: agile_modbus.h:195
int(* set_slave)(agile_modbus_t *ctx, int slave)
设置地址接口
Definition: agile_modbus.h:189
uint32_t max_adu_length
后端 ADU 长度
Definition: agile_modbus.h:188
int(* pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
预检查确认接口
Definition: agile_modbus.h:196
int(* build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp)
构建基础响应报文接口
Definition: agile_modbus.h:192
uint32_t header_length
头部长度,不包含功能码
Definition: agile_modbus.h:186
uint32_t backend_type
后端类型
Definition: agile_modbus.h:185
int(* send_msg_pre)(uint8_t *req, int req_length)
预发送数据接口
Definition: agile_modbus.h:194
int(* prepare_response_tid)(const uint8_t *req, int *req_length)
准备响应接口
Definition: agile_modbus.h:193
int(* build_request_basis)(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
构建基础请求报文接口
Definition: agile_modbus.h:190
uint32_t checksum_length
校验数据长度
Definition: agile_modbus.h:187
包含 modbus 头部参数结构体
Definition: agile_modbus.h:173
int slave
从机地址
Definition: agile_modbus.h:174
int t_id
事务标识符
Definition: agile_modbus.h:176
Agile Modbus 从机信息结构体
Definition: agile_modbus.h:232
uint8_t * buf
不同功能码需要使用的数据域
Definition: agile_modbus.h:237
int send_index
发送缓冲区当前索引
Definition: agile_modbus.h:238
agile_modbus_sft_t * sft
sft 结构体指针
Definition: agile_modbus.h:233
int address
寄存器地址
Definition: agile_modbus.h:235
int * rsp_length
响应数据长度指针
Definition: agile_modbus.h:234
Agile Modbus 结构体
Definition: agile_modbus.h:203
int(* compute_data_length_after_meta)(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
自定义计算数据长度接口
Definition: agile_modbus.h:211
uint8_t(* compute_meta_length_after_function)(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
自定义计算数据元长度接口
Definition: agile_modbus.h:209
int send_bufsz
发送缓冲区大小
Definition: agile_modbus.h:206
uint8_t * send_buf
发送缓冲区
Definition: agile_modbus.h:205
uint8_t * read_buf
接收缓冲区
Definition: agile_modbus.h:207
const agile_modbus_backend_t * backend
后端接口
Definition: agile_modbus.h:213
int slave
从机地址
Definition: agile_modbus.h:204
int read_bufsz
接收缓冲区大小
Definition: agile_modbus.h:208
void * backend_data
后端数据,指向 RTU 或 TCP 结构体
Definition: agile_modbus.h:214