diff --git a/.gitignore b/.gitignore index 1c22d672c..037593531 100644 --- a/.gitignore +++ b/.gitignore @@ -16,18 +16,18 @@ bsp/pico-dev/MDK/mdk/RTE .vscode/ coredump/ -#remove unnecessary file -test/out/ -test/python/*.py.a -test/python/*.py.o -test/python/__asset_lib_to_file_py_a.c tools/pikaByteCodeGen/build/ tools/pikaByteCodeGen/pikascript/ tools/pikaByteCodeGen/libpikabinder/ tools/pikaCompiler/libpikabinder/ tools/pikaCompiler/rust-msc-latest-linux -port/linux/test + +#remove unnecessary file +port/linux/test/out/ +port/linux/test/python/*.py.a +port/linux/test/python/*.py.o +port/linux/test/python/__asset_lib_to_file_py_a.c port/linux/Release/ port/linux/benchmark_result.json port/linux/gmon.out diff --git a/package/modbus/agile_modbus.c b/package/modbus/agile_modbus.c index 81ff40424..42e0cea25 100644 --- a/package/modbus/agile_modbus.c +++ b/package/modbus/agile_modbus.c @@ -6,10 +6,12 @@ * @verbatim 使用: - 用户需要实现硬件接口的 `发送数据` 、 `等待数据接收结束` 、 `清空接收缓存` 函数 + 用户需要实现硬件接口的 `发送数据` 、 `等待数据接收结束` 、 `清空接收缓存` + 函数 - 主机: - 1. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` 初始化 `RTU/TCP` 环境 + 1. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` 初始化 `RTU/TCP` + 环境 2. `agile_modbus_set_slave` 设置从机地址 3. `清空接收缓存` 4. `agile_modbus_serialize_xxx` 打包请求数据 @@ -20,7 +22,8 @@ - 从机: 1. 实现 `agile_modbus_slave_callback_t` 类型回调函数 - 2. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` 初始化 `RTU/TCP` 环境 + 2. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` 初始化 `RTU/TCP` + 环境 3. `agile_modbus_set_slave` 设置从机地址 4. `等待数据接收结束` 5. `agile_modbus_slave_handle` 处理请求数据 @@ -77,7 +80,8 @@ --------------- | 00 00 00 01 | --------------- - 数据元: 与功能码相关的数据,如 03 功能码数据元中包含寄存器起始地址和寄存器长度 + 数据元: 与功能码相关的数据,如 03 + 功能码数据元中包含寄存器起始地址和寄存器长度 @endverbatim * @param ctx modbus 句柄 @@ -85,8 +89,10 @@ * @param msg_type 消息类型 * @return 数据元长度 */ -static uint8_t agile_modbus_compute_meta_length_after_function(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type) -{ +static uint8_t agile_modbus_compute_meta_length_after_function( + agile_modbus_t* ctx, + int function, + agile_modbus_msg_type_t msg_type) { int length; if (msg_type == AGILE_MODBUS_MSG_INDICATION) { @@ -103,35 +109,37 @@ static uint8_t agile_modbus_compute_meta_length_after_function(agile_modbus_t *c /* MODBUS_FC_READ_EXCEPTION_STATUS, MODBUS_FC_REPORT_SLAVE_ID */ length = 0; if (ctx->compute_meta_length_after_function) - length = ctx->compute_meta_length_after_function(ctx, function, msg_type); + length = ctx->compute_meta_length_after_function(ctx, function, + msg_type); } } else { /* MSG_CONFIRMATION */ switch (function) { - case AGILE_MODBUS_FC_READ_COILS: - case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: - case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: - case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: - case AGILE_MODBUS_FC_REPORT_SLAVE_ID: - case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: - length = 1; - break; + case AGILE_MODBUS_FC_READ_COILS: + case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: + case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: + case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: + case AGILE_MODBUS_FC_REPORT_SLAVE_ID: + case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: + length = 1; + break; - case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: - case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: - length = 4; - break; + case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: + case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: + length = 4; + break; - case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: - length = 6; - break; + case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: + length = 6; + break; - default: - length = 1; - if (ctx->compute_meta_length_after_function) - length = ctx->compute_meta_length_after_function(ctx, function, msg_type); + default: + length = 1; + if (ctx->compute_meta_length_after_function) + length = ctx->compute_meta_length_after_function( + ctx, function, msg_type); } } @@ -174,26 +182,30 @@ static uint8_t agile_modbus_compute_meta_length_after_function(agile_modbus_t *c * @param msg_type 消息类型 * @return 数据长度 */ -static int agile_modbus_compute_data_length_after_meta(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type) -{ +static int agile_modbus_compute_data_length_after_meta( + agile_modbus_t* ctx, + uint8_t* msg, + int msg_length, + agile_modbus_msg_type_t msg_type) { int function = msg[ctx->backend->header_length]; int length; if (msg_type == AGILE_MODBUS_MSG_INDICATION) { switch (function) { - case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: - length = msg[ctx->backend->header_length + 5]; - break; + case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: + length = msg[ctx->backend->header_length + 5]; + break; - case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: - length = msg[ctx->backend->header_length + 9]; - break; + case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: + length = msg[ctx->backend->header_length + 9]; + break; - default: - length = 0; - if (ctx->compute_data_length_after_meta) - length = ctx->compute_data_length_after_meta(ctx, msg, msg_length, msg_type); + default: + length = 0; + if (ctx->compute_data_length_after_meta) + length = ctx->compute_data_length_after_meta( + ctx, msg, msg_length, msg_type); } } else { /* MSG_CONFIRMATION */ @@ -204,7 +216,8 @@ static int agile_modbus_compute_data_length_after_meta(agile_modbus_t *ctx, uint } else { length = 0; if (ctx->compute_data_length_after_meta) - length = ctx->compute_data_length_after_meta(ctx, msg, msg_length, msg_type); + length = ctx->compute_data_length_after_meta( + ctx, msg, msg_length, msg_type); } } @@ -221,17 +234,21 @@ static int agile_modbus_compute_data_length_after_meta(agile_modbus_t *ctx, uint * @param msg_type 消息类型 * @return >0:正确,modbus 数据帧长度; 其他:异常 */ -static int agile_modbus_receive_msg_judge(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type) -{ +static int agile_modbus_receive_msg_judge(agile_modbus_t* ctx, + uint8_t* msg, + int msg_length, + agile_modbus_msg_type_t msg_type) { int remain_len = msg_length; remain_len -= (ctx->backend->header_length + 1); if (remain_len < 0) return -1; - remain_len -= agile_modbus_compute_meta_length_after_function(ctx, msg[ctx->backend->header_length], msg_type); + remain_len -= agile_modbus_compute_meta_length_after_function( + ctx, msg[ctx->backend->header_length], msg_type); if (remain_len < 0) return -1; - remain_len -= agile_modbus_compute_data_length_after_meta(ctx, msg, msg_length, msg_type); + remain_len -= agile_modbus_compute_data_length_after_meta( + ctx, msg, msg_length, msg_type); if (remain_len < 0) return -1; @@ -254,8 +271,11 @@ static int agile_modbus_receive_msg_judge(agile_modbus_t *ctx, uint8_t *msg, int * @param read_buf 接收缓冲区 * @param read_bufsz 接收缓冲区大小 */ -void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz) -{ +void agile_modbus_common_init(agile_modbus_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz) { memset(ctx, 0, sizeof(agile_modbus_t)); ctx->slave = -1; ctx->send_buf = send_buf; @@ -270,8 +290,7 @@ void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_b * @param slave 地址 * @return 0:成功 */ -int agile_modbus_set_slave(agile_modbus_t *ctx, int slave) -{ +int agile_modbus_set_slave(agile_modbus_t* ctx, int slave) { return ctx->backend->set_slave(ctx, slave); } @@ -281,10 +300,11 @@ int agile_modbus_set_slave(agile_modbus_t *ctx, int slave) * @param cb 计算功能码后要接收的数据元长度回调函数 * @see agile_modbus_compute_meta_length_after_function */ -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)) -{ +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)) { ctx->compute_meta_length_after_function = cb; } @@ -294,27 +314,32 @@ void agile_modbus_set_compute_meta_length_after_function_cb(agile_modbus_t *ctx, * @param cb 计算数据元之后要接收的数据长度回调函数 * @see agile_modbus_compute_data_length_after_meta */ -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)) -{ +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)) { ctx->compute_data_length_after_meta = cb; } /** * @brief 校验接收数据正确性 - * @note 该 API 返回的是 modbus 数据帧长度,比如 8 个字节的 modbus 数据帧 + 2 个字节的脏数据,返回 8 + * @note 该 API 返回的是 modbus 数据帧长度,比如 8 个字节的 modbus 数据帧 + 2 + * 个字节的脏数据,返回 8 * @param ctx modbus 句柄 * @param msg_length 接收数据长度 * @param msg_type 消息类型 * @return >0:正确,modbus 数据帧长度; 其他:异常 */ -int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type) -{ +int agile_modbus_receive_judge(agile_modbus_t* ctx, + int msg_length, + agile_modbus_msg_type_t msg_type) { if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, msg_type); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + msg_type); return rc; } @@ -333,47 +358,49 @@ int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus /** * @brief 计算预期响应数据长度 - * @note 如果是特殊的功能码,返回 AGILE_MODBUS_MSG_LENGTH_UNDEFINED ,但这不代表异常。 - * agile_modbus_check_confirmation 调用该 API 处理时认为 AGILE_MODBUS_MSG_LENGTH_UNDEFINED 返回值也是有效的。 + * @note 如果是特殊的功能码,返回 AGILE_MODBUS_MSG_LENGTH_UNDEFINED + * ,但这不代表异常。 agile_modbus_check_confirmation 调用该 API 处理时认为 + * AGILE_MODBUS_MSG_LENGTH_UNDEFINED 返回值也是有效的。 * @param ctx modbus 句柄 * @param req 请求数据指针 * @return 预期响应数据长度 */ -static int agile_modbus_compute_response_length_from_request(agile_modbus_t *ctx, uint8_t *req) -{ +static int agile_modbus_compute_response_length_from_request( + agile_modbus_t* ctx, + uint8_t* req) { int length; const int offset = ctx->backend->header_length; switch (req[offset]) { - case AGILE_MODBUS_FC_READ_COILS: - case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: { - /* Header + nb values (code from write_bits) */ - int nb = (req[offset + 3] << 8) | req[offset + 4]; - length = 2 + (nb / 8) + ((nb % 8) ? 1 : 0); - } break; + case AGILE_MODBUS_FC_READ_COILS: + case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: { + /* Header + nb values (code from write_bits) */ + int nb = (req[offset + 3] << 8) | req[offset + 4]; + length = 2 + (nb / 8) + ((nb % 8) ? 1 : 0); + } break; - case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: - case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: - case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: - /* Header + 2 * nb values */ - length = 2 + 2 * (req[offset + 3] << 8 | req[offset + 4]); - break; + case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: + case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: + case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: + /* Header + 2 * nb values */ + length = 2 + 2 * (req[offset + 3] << 8 | req[offset + 4]); + break; - case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: - case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: - length = 5; - break; + case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: + case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: + length = 5; + break; - case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: - length = 7; - break; + case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: + length = 7; + break; - default: - /* The response is device specific (the header provides the - length) */ - return AGILE_MODBUS_MSG_LENGTH_UNDEFINED; + default: + /* The response is device specific (the header provides the + length) */ + return AGILE_MODBUS_MSG_LENGTH_UNDEFINED; } return offset + length + ctx->backend->checksum_length; @@ -388,9 +415,10 @@ static int agile_modbus_compute_response_length_from_request(agile_modbus_t *ctx * @return >=0:对应功能码响应对象的长度(如 03 功能码,值代表寄存器个数); * 其他:异常 (-1:报文错误;其他:可根据 `-128 - $返回值` 得到异常码) */ -static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req, - uint8_t *rsp, int rsp_length) -{ +static int agile_modbus_check_confirmation(agile_modbus_t* ctx, + uint8_t* req, + uint8_t* rsp, + int rsp_length) { int rc; int rsp_length_computed; const int offset = ctx->backend->header_length; @@ -402,18 +430,22 @@ static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req, return -1; } - rsp_length_computed = agile_modbus_compute_response_length_from_request(ctx, req); + rsp_length_computed = + agile_modbus_compute_response_length_from_request(ctx, req); /* Exception code */ if (function >= 0x80) { - if (rsp_length == (offset + 2 + (int)ctx->backend->checksum_length) && req[offset] == (rsp[offset] - 0x80)) + if (rsp_length == (offset + 2 + (int)ctx->backend->checksum_length) && + req[offset] == (rsp[offset] - 0x80)) return (-128 - rsp[offset + 1]); else return -1; } /* Check length */ - if ((rsp_length == rsp_length_computed || rsp_length_computed == AGILE_MODBUS_MSG_LENGTH_UNDEFINED) && function < 0x80) { + if ((rsp_length == rsp_length_computed || + rsp_length_computed == AGILE_MODBUS_MSG_LENGTH_UNDEFINED) && + function < 0x80) { int req_nb_value; int rsp_nb_value; @@ -423,39 +455,40 @@ static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req, /* Check the number of values is corresponding to the request */ switch (function) { - case AGILE_MODBUS_FC_READ_COILS: - case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: - /* Read functions, 8 values in a byte (nb - * of values in the request and byte count in - * the response. */ - req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; - req_nb_value = (req_nb_value / 8) + ((req_nb_value % 8) ? 1 : 0); - rsp_nb_value = rsp[offset + 1]; - break; + case AGILE_MODBUS_FC_READ_COILS: + case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: + /* Read functions, 8 values in a byte (nb + * of values in the request and byte count in + * the response. */ + req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; + req_nb_value = + (req_nb_value / 8) + ((req_nb_value % 8) ? 1 : 0); + rsp_nb_value = rsp[offset + 1]; + break; - case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: - case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: - case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: - /* Read functions 1 value = 2 bytes */ - req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; - rsp_nb_value = (rsp[offset + 1] / 2); - break; + case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: + case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: + case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: + /* Read functions 1 value = 2 bytes */ + req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; + rsp_nb_value = (rsp[offset + 1] / 2); + break; - case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: - case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: - /* N Write functions */ - req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; - rsp_nb_value = (rsp[offset + 3] << 8) | rsp[offset + 4]; - break; + case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: + case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: + /* N Write functions */ + req_nb_value = (req[offset + 3] << 8) + req[offset + 4]; + rsp_nb_value = (rsp[offset + 3] << 8) | rsp[offset + 4]; + break; - case AGILE_MODBUS_FC_REPORT_SLAVE_ID: - /* Report slave ID (bytes received) */ - req_nb_value = rsp_nb_value = rsp[offset + 1]; - break; + case AGILE_MODBUS_FC_REPORT_SLAVE_ID: + /* Report slave ID (bytes received) */ + req_nb_value = rsp_nb_value = rsp[offset + 1]; + break; - default: - /* 1 Write functions & others */ - req_nb_value = rsp_nb_value = 1; + default: + /* 1 Write functions & others */ + req_nb_value = rsp_nb_value = 1; } if (req_nb_value == rsp_nb_value) @@ -472,7 +505,8 @@ static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req, * @} */ -/** @defgroup Master_Common_Operation_Functions Master Common Operation Functions +/** @defgroup Master_Common_Operation_Functions Master Common Operation + Functions * @brief 常用 modbus 主机操作函数 @verbatim API 形式如下: @@ -490,9 +524,9 @@ static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req, * @{ */ -int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_read_bits(agile_modbus_t* ctx, int addr, int nb) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -500,21 +534,25 @@ int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_COILS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_READ_COILS, addr, nb, ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_read_bits(agile_modbus_t* ctx, + int msg_length, + uint8_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -530,7 +568,8 @@ int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint offset = ctx->backend->header_length + 2; offset_end = offset + rc; - nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + ctx->send_buf[ctx->backend->header_length + 4]; + nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + + ctx->send_buf[ctx->backend->header_length + 4]; for (i = offset; i < offset_end; i++) { /* Shift reg hi_byte to temp */ @@ -545,9 +584,11 @@ int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint return nb; } -int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_read_input_bits(agile_modbus_t* ctx, + int addr, + int nb) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -555,21 +596,25 @@ int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_DISCRETE_INPUTS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_READ_DISCRETE_INPUTS, addr, nb, ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_read_input_bits(agile_modbus_t* ctx, + int msg_length, + uint8_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -585,7 +630,8 @@ int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length offset = ctx->backend->header_length + 2; offset_end = offset + rc; - nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + ctx->send_buf[ctx->backend->header_length + 4]; + nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + + ctx->send_buf[ctx->backend->header_length + 4]; for (i = offset; i < offset_end; i++) { /* Shift reg hi_byte to temp */ @@ -600,9 +646,11 @@ int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length return nb; } -int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_read_registers(agile_modbus_t* ctx, + int addr, + int nb) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -610,21 +658,25 @@ int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_HOLDING_REGISTERS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_READ_HOLDING_REGISTERS, addr, nb, ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_read_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -638,15 +690,18 @@ int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, offset = ctx->backend->header_length; for (i = 0; i < rc; i++) { /* shift reg hi_byte to temp OR with lo_byte */ - dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)]; + dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | + ctx->read_buf[offset + 3 + (i << 1)]; } return rc; } -int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_read_input_registers(agile_modbus_t* ctx, + int addr, + int nb) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -654,21 +709,25 @@ int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, i return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_INPUT_REGISTERS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_READ_INPUT_REGISTERS, addr, nb, ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_read_input_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -682,34 +741,40 @@ int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_l offset = ctx->backend->header_length; for (i = 0; i < rc; i++) { /* shift reg hi_byte to temp OR with lo_byte */ - dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)]; + dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | + ctx->read_buf[offset + 3 + (i << 1)]; } return rc; } -int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_write_bit(agile_modbus_t* ctx, + int addr, + int status) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_SINGLE_COIL, addr, status ? 0xFF00 : 0, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_WRITE_SINGLE_COIL, addr, status ? 0xFF00 : 0, + ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_write_bit(agile_modbus_t* ctx, int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -718,28 +783,34 @@ int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length) return rc; } -int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_write_register(agile_modbus_t* ctx, + int addr, + const uint16_t value) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER, addr, (int)value, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER, addr, (int)value, + ctx->send_buf); req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); return req_length; } -int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_write_register(agile_modbus_t* ctx, + int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -748,9 +819,12 @@ int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length) return rc; } -int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_write_bits(agile_modbus_t* ctx, + int addr, + int nb, + const uint8_t* src) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -763,7 +837,8 @@ int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, con int bit_check = 0; int pos = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS, addr, nb, ctx->send_buf); byte_count = (nb / 8) + ((nb % 8) ? 1 : 0); min_req_length += (1 + byte_count); @@ -793,15 +868,16 @@ int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, con return req_length; } -int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_write_bits(agile_modbus_t* ctx, int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -810,9 +886,12 @@ int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length) return rc; } -int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_write_registers(agile_modbus_t* ctx, + int addr, + int nb, + const uint16_t* src) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -823,7 +902,8 @@ int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb int req_length; int byte_count; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS, addr, nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS, addr, nb, ctx->send_buf); byte_count = nb * 2; min_req_length += (1 + byte_count); @@ -841,15 +921,17 @@ int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb return req_length; } -int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_write_registers(agile_modbus_t* ctx, + int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -858,14 +940,18 @@ int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length return rc; } -int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length + 2; +int agile_modbus_serialize_mask_write_register(agile_modbus_t* ctx, + int addr, + uint16_t and_mask, + uint16_t or_mask) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length + 2; if (ctx->send_bufsz < min_req_length) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_MASK_WRITE_REGISTER, addr, 0, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_MASK_WRITE_REGISTER, addr, 0, ctx->send_buf); /* HACKISH, count is not used */ req_length -= 2; @@ -880,15 +966,17 @@ int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, ui return req_length; } -int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_mask_write_register(agile_modbus_t* ctx, + int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -897,12 +985,14 @@ int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_le return rc; } -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) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +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) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; @@ -916,7 +1006,9 @@ int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx, int i; int byte_count; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS, read_addr, read_nb, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS, read_addr, read_nb, + ctx->send_buf); byte_count = write_nb * 2; min_req_length += (5 + byte_count); @@ -938,15 +1030,18 @@ int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx, return req_length; } -int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -960,20 +1055,22 @@ int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int m offset = ctx->backend->header_length; for (i = 0; i < rc; i++) { /* shift reg hi_byte to temp OR with lo_byte */ - dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)]; + dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | + ctx->read_buf[offset + 3 + (i << 1)]; } return rc; } -int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_serialize_report_slave_id(agile_modbus_t* ctx) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; int req_length = 0; - req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_REPORT_SLAVE_ID, 0, 0, ctx->send_buf); + req_length = ctx->backend->build_request_basis( + ctx, AGILE_MODBUS_FC_REPORT_SLAVE_ID, 0, 0, ctx->send_buf); /* HACKISH, addr and count are not used */ req_length -= 4; req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length); @@ -981,9 +1078,12 @@ int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx) return req_length; } -int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_report_slave_id(agile_modbus_t* ctx, + int msg_length, + int max_dest, + uint8_t* dest) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) @@ -991,7 +1091,8 @@ int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length if (max_dest <= 0) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -1028,8 +1129,9 @@ int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length * @param raw_req_length 原始报文长度 * @return >0:请求数据长度; 其他:异常 */ -int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length) -{ +int agile_modbus_serialize_raw_request(agile_modbus_t* ctx, + const uint8_t* raw_req, + int raw_req_length) { if (raw_req_length < 2) { /* The raw request must contain function and slave at least and must not be longer than the maximum pdu length plus the slave @@ -1038,7 +1140,8 @@ int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_r return -1; } - int min_req_length = ctx->backend->header_length + 1 + ctx->backend->checksum_length + raw_req_length - 2; + int min_req_length = ctx->backend->header_length + 1 + + ctx->backend->checksum_length + raw_req_length - 2; if (ctx->send_bufsz < min_req_length) return -1; @@ -1070,15 +1173,16 @@ int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_r * @return >=0:对应功能码响应对象的长度(如 03 功能码,值代表寄存器个数); * 其他:异常 (-1:报文错误;其他:可根据 `-128 - $返回值` 得到异常码) */ -int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length) -{ - int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_deserialize_raw_response(agile_modbus_t* ctx, int msg_length) { + int min_req_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_req_length) return -1; if ((msg_length <= 0) || (msg_length > ctx->read_bufsz)) return -1; - int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION); + int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, + AGILE_MODBUS_MSG_CONFIRMATION); if (rc < 0) return -1; @@ -1110,8 +1214,9 @@ int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length) * @param exception_code 异常码 * @return 响应数据长度 */ -static int agile_modbus_serialize_response_exception(agile_modbus_t *ctx, agile_modbus_sft_t *sft, int exception_code) -{ +static int agile_modbus_serialize_response_exception(agile_modbus_t* ctx, + agile_modbus_sft_t* sft, + int exception_code) { int rsp_length; /* Build exception response */ @@ -1136,8 +1241,7 @@ static int agile_modbus_serialize_response_exception(agile_modbus_t *ctx, agile_ * @param index IO 索引(第几个 IO) * @param status IO 状态 */ -void agile_modbus_slave_io_set(uint8_t *buf, int index, int status) -{ +void agile_modbus_slave_io_set(uint8_t* buf, int index, int status) { int offset = index / 8; int shift = index % 8; @@ -1153,8 +1257,7 @@ void agile_modbus_slave_io_set(uint8_t *buf, int index, int status) * @param index IO 索引(第几个 IO) * @return IO 状态(1/0) */ -uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index) -{ +uint8_t agile_modbus_slave_io_get(uint8_t* buf, int index) { int offset = index / 8; int shift = index % 8; @@ -1169,8 +1272,7 @@ uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index) * @param index 寄存器索引(第几个寄存器) * @param data 寄存器数据 */ -void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data) -{ +void agile_modbus_slave_register_set(uint8_t* buf, int index, uint16_t data) { buf[index * 2] = data >> 8; buf[index * 2 + 1] = data & 0xFF; } @@ -1181,8 +1283,7 @@ void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data) * @param index 寄存器索引(第几个寄存器) * @return 寄存器数据 */ -uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index) -{ +uint16_t agile_modbus_slave_register_get(uint8_t* buf, int index) { uint16_t data = (buf[index * 2] << 8) + buf[index * 2 + 1]; return data; @@ -1200,14 +1301,19 @@ uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index) * @param frame_length 存放 modbus 数据帧长度 * @return >=0:要响应的数据长度; 其他:异常 */ -int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict, - agile_modbus_slave_callback_t slave_cb, const void *slave_data, int *frame_length) -{ - int min_rsp_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length; +int agile_modbus_slave_handle(agile_modbus_t* ctx, + int msg_length, + uint8_t slave_strict, + agile_modbus_slave_callback_t slave_cb, + const void* slave_data, + int* frame_length) { + int min_rsp_length = + ctx->backend->header_length + 5 + ctx->backend->checksum_length; if (ctx->send_bufsz < min_rsp_length) return -1; - int req_length = agile_modbus_receive_judge(ctx, msg_length, AGILE_MODBUS_MSG_INDICATION); + int req_length = agile_modbus_receive_judge(ctx, msg_length, + AGILE_MODBUS_MSG_INDICATION); if (req_length < 0) return -1; if (frame_length) @@ -1220,8 +1326,8 @@ int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave int rsp_length = 0; int exception_code = 0; agile_modbus_sft_t sft; - uint8_t *req = ctx->read_buf; - uint8_t *rsp = ctx->send_buf; + uint8_t* req = ctx->read_buf; + uint8_t* rsp = ctx->send_buf; memset(rsp, 0, ctx->send_bufsz); offset = ctx->backend->header_length; @@ -1244,245 +1350,261 @@ int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave } switch (function) { - case AGILE_MODBUS_FC_READ_COILS: - case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: { - int nb = (req[offset + 3] << 8) + req[offset + 4]; - if (nb < 1 || AGILE_MODBUS_MAX_READ_BITS < nb) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } + case AGILE_MODBUS_FC_READ_COILS: + case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: { + int nb = (req[offset + 3] << 8) + req[offset + 4]; + if (nb < 1 || AGILE_MODBUS_MAX_READ_BITS < nb) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } - int end_address = (int)address + nb - 1; - if (end_address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } + int end_address = (int)address + nb - 1; + if (end_address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - slave_info.nb = (nb / 8) + ((nb % 8) ? 1 : 0); - rsp[rsp_length++] = slave_info.nb; - slave_info.send_index = rsp_length; - rsp_length += slave_info.nb; - slave_info.nb = nb; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - } break; - - case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: - case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: { - int nb = (req[offset + 3] << 8) + req[offset + 4]; - if (nb < 1 || AGILE_MODBUS_MAX_READ_REGISTERS < nb) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } - - int end_address = (int)address + nb - 1; - if (end_address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - slave_info.nb = nb << 1; - rsp[rsp_length++] = slave_info.nb; - slave_info.send_index = rsp_length; - rsp_length += slave_info.nb; - slave_info.nb = nb; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - } break; - - case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: { - //! warning: comparison is always false due to limited range of data type [-Wtype-limits] - #if 0 - if (address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - #endif - - int data = (req[offset + 3] << 8) + req[offset + 4]; - if (data == 0xFF00 || data == 0x0) - data = data ? 1 : 0; - else { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } - - slave_info.buf = (uint8_t *)&data; - rsp_length = req_length; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - memcpy(rsp, req, req_length); - } break; - - case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: { - //! warning: comparison is always false due to limited range of data type [-Wtype-limits] - #if 0 - if (address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - #endif - - int data = (req[offset + 3] << 8) + req[offset + 4]; - - slave_info.buf = (uint8_t *)&data; - rsp_length = req_length; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - memcpy(rsp, req, req_length); - } break; - - case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: { - int nb = (req[offset + 3] << 8) + req[offset + 4]; - int nb_bits = req[offset + 5]; - if (nb < 1 || AGILE_MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } - - int end_address = (int)address + nb - 1; - if (end_address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - slave_info.nb = nb; - slave_info.buf = &req[offset + 6]; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + 4)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - /* 4 to copy the bit address (2) and the quantity of bits */ - memcpy(rsp + rsp_length, req + rsp_length, 4); - rsp_length += 4; - } break; - - case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: { - int nb = (req[offset + 3] << 8) + req[offset + 4]; - int nb_bytes = req[offset + 5]; - if (nb < 1 || AGILE_MODBUS_MAX_WRITE_REGISTERS < nb || nb_bytes != nb * 2) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } - - int end_address = (int)address + nb - 1; - if (end_address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - slave_info.nb = nb; - slave_info.buf = &req[offset + 6]; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + 4)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - /* 4 to copy the address (2) and the no. of registers */ - memcpy(rsp + rsp_length, req + rsp_length, 4); - rsp_length += 4; - - } break; - - case AGILE_MODBUS_FC_REPORT_SLAVE_ID: { - int str_len; - int byte_count_pos; - - slave_cb = NULL; - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - /* Skip byte count for now */ - byte_count_pos = rsp_length++; - rsp[rsp_length++] = ctx->slave; - /* Run indicator status to ON */ - rsp[rsp_length++] = 0xFF; - - str_len = strlen(AGILE_MODBUS_VERSION_STRING); - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + str_len)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - memcpy(rsp + rsp_length, AGILE_MODBUS_VERSION_STRING, str_len); - rsp_length += str_len; - rsp[byte_count_pos] = rsp_length - byte_count_pos - 1; - } break; - - case AGILE_MODBUS_FC_READ_EXCEPTION_STATUS: - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION; - break; - - case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: { - //! warning: comparison is always false due to limited range of data type [-Wtype-limits] - #if 0 - if (address > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - #endif - - slave_info.buf = &req[offset + 3]; - rsp_length = req_length; - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - memcpy(rsp, req, req_length); - } break; - - case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: { - int nb = (req[offset + 3] << 8) + req[offset + 4]; - uint16_t address_write = (req[offset + 5] << 8) + req[offset + 6]; - int nb_write = (req[offset + 7] << 8) + req[offset + 8]; - int nb_write_bytes = req[offset + 9]; - if (nb_write < 1 || AGILE_MODBUS_MAX_WR_WRITE_REGISTERS < nb_write || - nb < 1 || AGILE_MODBUS_MAX_WR_READ_REGISTERS < nb || - nb_write_bytes != nb_write * 2) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; - break; - } - - int end_address = (int)address + nb - 1; - int end_address_write = (int)address_write + nb_write - 1; - if (end_address > 0xFFFF || end_address_write > 0xFFFF) { - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - break; - } - - rsp_length = ctx->backend->build_response_basis(&sft, rsp); - rsp[rsp_length++] = nb << 1; - slave_info.buf = &req[offset + 3]; - slave_info.send_index = rsp_length; - rsp_length += (nb << 1); - if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) { - exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; - break; - } - } break; - - default: { - if (slave_cb == NULL) - exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION; - else { rsp_length = ctx->backend->build_response_basis(&sft, rsp); + slave_info.nb = (nb / 8) + ((nb % 8) ? 1 : 0); + rsp[rsp_length++] = slave_info.nb; slave_info.send_index = rsp_length; - slave_info.buf = &req[offset + 1]; - slave_info.nb = req_length - offset - 1; + rsp_length += slave_info.nb; + slave_info.nb = nb; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + } break; + + case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: + case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: { + int nb = (req[offset + 3] << 8) + req[offset + 4]; + if (nb < 1 || AGILE_MODBUS_MAX_READ_REGISTERS < nb) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } + + int end_address = (int)address + nb - 1; + if (end_address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } + + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + slave_info.nb = nb << 1; + rsp[rsp_length++] = slave_info.nb; + slave_info.send_index = rsp_length; + rsp_length += slave_info.nb; + slave_info.nb = nb; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + } break; + + case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: { +//! warning: comparison is always false due to limited range of data type +//! [-Wtype-limits] +#if 0 + if (address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; } - } break; +#endif + + int data = (req[offset + 3] << 8) + req[offset + 4]; + if (data == 0xFF00 || data == 0x0) + data = data ? 1 : 0; + else { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } + + slave_info.buf = (uint8_t*)&data; + rsp_length = req_length; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + memcpy(rsp, req, req_length); + } break; + + case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: { +//! warning: comparison is always false due to limited range of data type +//! [-Wtype-limits] +#if 0 + if (address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } +#endif + + int data = (req[offset + 3] << 8) + req[offset + 4]; + + slave_info.buf = (uint8_t*)&data; + rsp_length = req_length; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + memcpy(rsp, req, req_length); + } break; + + case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: { + int nb = (req[offset + 3] << 8) + req[offset + 4]; + int nb_bits = req[offset + 5]; + if (nb < 1 || AGILE_MODBUS_MAX_WRITE_BITS < nb || + nb_bits * 8 < nb) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } + + int end_address = (int)address + nb - 1; + if (end_address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } + + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + slave_info.nb = nb; + slave_info.buf = &req[offset + 6]; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length + 4)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + /* 4 to copy the bit address (2) and the quantity of bits */ + memcpy(rsp + rsp_length, req + rsp_length, 4); + rsp_length += 4; + } break; + + case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: { + int nb = (req[offset + 3] << 8) + req[offset + 4]; + int nb_bytes = req[offset + 5]; + if (nb < 1 || AGILE_MODBUS_MAX_WRITE_REGISTERS < nb || + nb_bytes != nb * 2) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } + + int end_address = (int)address + nb - 1; + if (end_address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } + + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + slave_info.nb = nb; + slave_info.buf = &req[offset + 6]; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length + 4)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + /* 4 to copy the address (2) and the no. of registers */ + memcpy(rsp + rsp_length, req + rsp_length, 4); + rsp_length += 4; + + } break; + + case AGILE_MODBUS_FC_REPORT_SLAVE_ID: { + int str_len; + int byte_count_pos; + + slave_cb = NULL; + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + /* Skip byte count for now */ + byte_count_pos = rsp_length++; + rsp[rsp_length++] = ctx->slave; + /* Run indicator status to ON */ + rsp[rsp_length++] = 0xFF; + + str_len = strlen(AGILE_MODBUS_VERSION_STRING); + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length + str_len)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + memcpy(rsp + rsp_length, AGILE_MODBUS_VERSION_STRING, str_len); + rsp_length += str_len; + rsp[byte_count_pos] = rsp_length - byte_count_pos - 1; + } break; + + case AGILE_MODBUS_FC_READ_EXCEPTION_STATUS: + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION; + break; + + case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: { +//! warning: comparison is always false due to limited range of data type +//! [-Wtype-limits] +#if 0 + if (address > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } +#endif + + slave_info.buf = &req[offset + 3]; + rsp_length = req_length; + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + memcpy(rsp, req, req_length); + } break; + + case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: { + int nb = (req[offset + 3] << 8) + req[offset + 4]; + uint16_t address_write = (req[offset + 5] << 8) + req[offset + 6]; + int nb_write = (req[offset + 7] << 8) + req[offset + 8]; + int nb_write_bytes = req[offset + 9]; + if (nb_write < 1 || + AGILE_MODBUS_MAX_WR_WRITE_REGISTERS < nb_write || nb < 1 || + AGILE_MODBUS_MAX_WR_READ_REGISTERS < nb || + nb_write_bytes != nb_write * 2) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE; + break; + } + + int end_address = (int)address + nb - 1; + int end_address_write = (int)address_write + nb_write - 1; + if (end_address > 0xFFFF || end_address_write > 0xFFFF) { + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS; + break; + } + + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + rsp[rsp_length++] = nb << 1; + slave_info.buf = &req[offset + 3]; + slave_info.send_index = rsp_length; + rsp_length += (nb << 1); + if (ctx->send_bufsz < + (int)(rsp_length + ctx->backend->checksum_length)) { + exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE; + break; + } + } break; + + default: { + if (slave_cb == NULL) + exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION; + else { + rsp_length = ctx->backend->build_response_basis(&sft, rsp); + slave_info.send_index = rsp_length; + slave_info.buf = &req[offset + 1]; + slave_info.nb = req_length - offset - 1; + } + } break; } if (exception_code) - rsp_length = agile_modbus_serialize_response_exception(ctx, &sft, exception_code); + rsp_length = agile_modbus_serialize_response_exception(ctx, &sft, + exception_code); else { if (slave_cb) { int ret = slave_cb(ctx, &slave_info, slave_data); @@ -1491,13 +1613,15 @@ int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave if (ret == -AGILE_MODBUS_EXCEPTION_UNKNOW) rsp_length = 0; else - rsp_length = agile_modbus_serialize_response_exception(ctx, &sft, -ret); + rsp_length = agile_modbus_serialize_response_exception( + ctx, &sft, -ret); } } } if (rsp_length) { - if ((ctx->backend->backend_type == AGILE_MODBUS_BACKEND_TYPE_RTU) && (slave == AGILE_MODBUS_BROADCAST_ADDRESS)) + if ((ctx->backend->backend_type == AGILE_MODBUS_BACKEND_TYPE_RTU) && + (slave == AGILE_MODBUS_BROADCAST_ADDRESS)) return 0; rsp_length = ctx->backend->send_msg_pre(rsp, rsp_length); diff --git a/package/modbus/agile_modbus.h b/package/modbus/agile_modbus.h index 4be7429b5..c097f5238 100644 --- a/package/modbus/agile_modbus.h +++ b/package/modbus/agile_modbus.h @@ -31,17 +31,17 @@ extern "C" { /** @defgroup Modbus_Function_Codes Modbus Function Codes * @{ */ -#define AGILE_MODBUS_FC_READ_COILS 0x01 -#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02 -#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03 -#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04 -#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05 -#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06 -#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07 -#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F +#define AGILE_MODBUS_FC_READ_COILS 0x01 +#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02 +#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03 +#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04 +#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05 +#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06 +#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07 +#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F #define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10 -#define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11 -#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16 +#define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11 +#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16 #define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17 /** * @} @@ -64,7 +64,7 @@ extern "C" { @endverbatim * @{ */ -#define AGILE_MODBUS_MAX_READ_BITS 2000 +#define AGILE_MODBUS_MAX_READ_BITS 2000 #define AGILE_MODBUS_MAX_WRITE_BITS 1968 /** * @} @@ -82,10 +82,10 @@ extern "C" { @endverbatim * @{ */ -#define AGILE_MODBUS_MAX_READ_REGISTERS 125 -#define AGILE_MODBUS_MAX_WRITE_REGISTERS 123 +#define AGILE_MODBUS_MAX_READ_REGISTERS 125 +#define AGILE_MODBUS_MAX_WRITE_REGISTERS 123 #define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121 -#define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125 +#define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125 /** * @} */ @@ -182,19 +182,28 @@ typedef struct agile_modbus agile_modbus_t; /**< Agile Modbus 结构体 */ * @brief Agile Modbus 后端接口结构体 */ typedef struct agile_modbus_backend { - uint32_t backend_type; /**< 后端类型 */ - uint32_t header_length; /**< 头部长度,不包含功能码 */ - uint32_t checksum_length; /**< 校验数据长度 */ - uint32_t max_adu_length; /**< 后端 ADU 长度 */ - int (*set_slave)(agile_modbus_t *ctx, int slave); /**< 设置地址接口 */ - int (*build_request_basis)(agile_modbus_t *ctx, int function, int addr, - int nb, uint8_t *req); /**< 构建基础请求报文接口 */ - int (*build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp); /**< 构建基础响应报文接口 */ - int (*prepare_response_tid)(const uint8_t *req, int *req_length); /**< 准备响应接口 */ - int (*send_msg_pre)(uint8_t *req, int req_length); /**< 预发送数据接口 */ - int (*check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length); /**< 检查接收数据完整性接口 */ - int (*pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req, - const uint8_t *rsp, int rsp_length); /**< 预检查确认接口 */ + uint32_t backend_type; /**< 后端类型 */ + uint32_t header_length; /**< 头部长度,不包含功能码 */ + uint32_t checksum_length; /**< 校验数据长度 */ + uint32_t max_adu_length; /**< 后端 ADU 长度 */ + int (*set_slave)(agile_modbus_t* ctx, int slave); /**< 设置地址接口 */ + int (*build_request_basis)(agile_modbus_t* ctx, + int function, + int addr, + int nb, + uint8_t* req); /**< 构建基础请求报文接口 */ + int (*build_response_basis)(agile_modbus_sft_t* sft, + uint8_t* rsp); /**< 构建基础响应报文接口 */ + int (*prepare_response_tid)(const uint8_t* req, + int* req_length); /**< 准备响应接口 */ + int (*send_msg_pre)(uint8_t* req, int req_length); /**< 预发送数据接口 */ + int (*check_integrity)(agile_modbus_t* ctx, + uint8_t* msg, + const int msg_length); /**< 检查接收数据完整性接口 */ + int (*pre_check_confirmation)(agile_modbus_t* ctx, + const uint8_t* req, + const uint8_t* rsp, + int rsp_length); /**< 预检查确认接口 */ } agile_modbus_backend_t; /** @@ -202,16 +211,21 @@ typedef struct agile_modbus_backend { */ struct agile_modbus { int slave; /**< 从机地址 */ - uint8_t *send_buf; /**< 发送缓冲区 */ + uint8_t* send_buf; /**< 发送缓冲区 */ int send_bufsz; /**< 发送缓冲区大小 */ - uint8_t *read_buf; /**< 接收缓冲区 */ + uint8_t* read_buf; /**< 接收缓冲区 */ int read_bufsz; /**< 接收缓冲区大小 */ - uint8_t (*compute_meta_length_after_function)(agile_modbus_t *ctx, int function, - agile_modbus_msg_type_t msg_type); /**< 自定义计算数据元长度接口 */ - int (*compute_data_length_after_meta)(agile_modbus_t *ctx, uint8_t *msg, - int msg_length, agile_modbus_msg_type_t msg_type); /**< 自定义计算数据长度接口 */ - const agile_modbus_backend_t *backend; /**< 后端接口 */ - void *backend_data; /**< 后端数据,指向 RTU 或 TCP 结构体 */ + uint8_t (*compute_meta_length_after_function)( + agile_modbus_t* ctx, + int function, + agile_modbus_msg_type_t msg_type); /**< 自定义计算数据元长度接口 */ + int (*compute_data_length_after_meta)( + agile_modbus_t* ctx, + uint8_t* msg, + int msg_length, + agile_modbus_msg_type_t msg_type); /**< 自定义计算数据长度接口 */ + const agile_modbus_backend_t* backend; /**< 后端接口 */ + void* backend_data; /**< 后端数据,指向 RTU 或 TCP 结构体 */ }; /** @@ -230,11 +244,11 @@ struct agile_modbus { * @brief Agile Modbus 从机信息结构体 */ struct agile_modbus_slave_info { - agile_modbus_sft_t *sft; /**< sft 结构体指针 */ - int *rsp_length; /**< 响应数据长度指针 */ + agile_modbus_sft_t* sft; /**< sft 结构体指针 */ + int* rsp_length; /**< 响应数据长度指针 */ int address; /**< 寄存器地址 */ int nb; /**< 数目 */ - uint8_t *buf; /**< 不同功能码需要使用的数据域 */ + uint8_t* buf; /**< 不同功能码需要使用的数据域 */ int send_index; /**< 发送缓冲区当前索引 */ }; @@ -245,10 +259,13 @@ struct agile_modbus_slave_info { * @param data 私有数据 * @return =0:正常; * <0:异常 - * (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): 未知异常,从机不会打包响应数据) - * (其他负数异常码: 从机会打包异常响应数据) + * (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): + * 未知异常,从机不会打包响应数据) (其他负数异常码: 从机会打包异常响应数据) */ -typedef int (*agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data); +typedef int (*agile_modbus_slave_callback_t)( + agile_modbus_t* ctx, + struct agile_modbus_slave_info* slave_info, + const void* data); /** * @} @@ -261,15 +278,26 @@ typedef int (*agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_m /** @addtogroup COMMON_Exported_Functions * @{ */ -void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz); -int agile_modbus_set_slave(agile_modbus_t *ctx, int slave); -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)); -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)); -int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type); +void agile_modbus_common_init(agile_modbus_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz); +int agile_modbus_set_slave(agile_modbus_t* ctx, int slave); +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)); +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)); +int agile_modbus_receive_judge(agile_modbus_t* ctx, + int msg_length, + agile_modbus_msg_type_t msg_type); /** * @} */ @@ -281,31 +309,66 @@ int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus /** @addtogroup Master_Common_Operation_Functions * @{ */ -int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb); -int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest); -int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb); -int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest); -int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb); -int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest); -int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb); -int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest); -int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status); -int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length); -int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value); -int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length); -int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src); -int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length); -int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src); -int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length); -int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask); -int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length); -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); -int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest); -int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx); -int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest); +int agile_modbus_serialize_read_bits(agile_modbus_t* ctx, int addr, int nb); +int agile_modbus_deserialize_read_bits(agile_modbus_t* ctx, + int msg_length, + uint8_t* dest); +int agile_modbus_serialize_read_input_bits(agile_modbus_t* ctx, + int addr, + int nb); +int agile_modbus_deserialize_read_input_bits(agile_modbus_t* ctx, + int msg_length, + uint8_t* dest); +int agile_modbus_serialize_read_registers(agile_modbus_t* ctx, + int addr, + int nb); +int agile_modbus_deserialize_read_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest); +int agile_modbus_serialize_read_input_registers(agile_modbus_t* ctx, + int addr, + int nb); +int agile_modbus_deserialize_read_input_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest); +int agile_modbus_serialize_write_bit(agile_modbus_t* ctx, int addr, int status); +int agile_modbus_deserialize_write_bit(agile_modbus_t* ctx, int msg_length); +int agile_modbus_serialize_write_register(agile_modbus_t* ctx, + int addr, + const uint16_t value); +int agile_modbus_deserialize_write_register(agile_modbus_t* ctx, + int msg_length); +int agile_modbus_serialize_write_bits(agile_modbus_t* ctx, + int addr, + int nb, + const uint8_t* src); +int agile_modbus_deserialize_write_bits(agile_modbus_t* ctx, int msg_length); +int agile_modbus_serialize_write_registers(agile_modbus_t* ctx, + int addr, + int nb, + const uint16_t* src); +int agile_modbus_deserialize_write_registers(agile_modbus_t* ctx, + int msg_length); +int agile_modbus_serialize_mask_write_register(agile_modbus_t* ctx, + int addr, + uint16_t and_mask, + uint16_t or_mask); +int agile_modbus_deserialize_mask_write_register(agile_modbus_t* ctx, + int msg_length); +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); +int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t* ctx, + int msg_length, + uint16_t* dest); +int agile_modbus_serialize_report_slave_id(agile_modbus_t* ctx); +int agile_modbus_deserialize_report_slave_id(agile_modbus_t* ctx, + int msg_length, + int max_dest, + uint8_t* dest); /** * @} */ @@ -313,8 +376,10 @@ int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length /** @addtogroup Master_Raw_Operation_Functions * @{ */ -int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length); -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_deserialize_raw_response(agile_modbus_t* ctx, int msg_length); /** * @} */ @@ -330,12 +395,16 @@ int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length); /** @addtogroup Slave_Operation_Functions * @{ */ -int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict, - agile_modbus_slave_callback_t slave_cb, const void *slave_data, int *frame_length); -void agile_modbus_slave_io_set(uint8_t *buf, int index, int status); -uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index); -void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data); -uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index); +int agile_modbus_slave_handle(agile_modbus_t* ctx, + int msg_length, + uint8_t slave_strict, + agile_modbus_slave_callback_t slave_cb, + const void* slave_data, + int* frame_length); +void agile_modbus_slave_io_set(uint8_t* buf, int index, int status); +uint8_t agile_modbus_slave_io_get(uint8_t* buf, int index); +void agile_modbus_slave_register_set(uint8_t* buf, int index, uint16_t data); +uint16_t agile_modbus_slave_register_get(uint8_t* buf, int index); /** * @} */ diff --git a/package/modbus/agile_modbus_rtu.c b/package/modbus/agile_modbus_rtu.c index 95b24cf0a..e2e777962 100644 --- a/package/modbus/agile_modbus_rtu.c +++ b/package/modbus/agile_modbus_rtu.c @@ -22,64 +22,54 @@ * @{ */ /** Table of CRC values for high-order byte */ -static const uint8_t _table_crc_hi[] = - { - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40}; +static const uint8_t _table_crc_hi[] = { + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40}; /** Table of CRC values for low-order byte */ -static const uint8_t _table_crc_lo[] = - { - 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, - 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, - 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, - 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, - 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, - 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, - 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, - 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, - 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, - 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, - 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, - 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, - 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, - 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, - 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, - 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, - 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, - 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, - 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, - 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, - 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, - 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, - 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, - 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, - 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, - 0x43, 0x83, 0x41, 0x81, 0x80, 0x40}; +static const uint8_t _table_crc_lo[] = { + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, + 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, + 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, + 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, + 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, + 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, + 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, + 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, + 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, + 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, + 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, + 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, + 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, + 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, + 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, + 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, + 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, + 0x41, 0x81, 0x80, 0x40}; /** * @} */ @@ -94,8 +84,8 @@ static const uint8_t _table_crc_lo[] = * @param buffer_length 数据长度 * @return CRC16 值 */ -static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length) -{ +static uint16_t agile_modbus_rtu_crc16(uint8_t* buffer, + uint16_t buffer_length) { uint8_t crc_hi = 0xFF; /* high CRC byte initialized */ uint8_t crc_lo = 0xFF; /* low CRC byte initialized */ unsigned int i; /* will index into CRC lookup */ @@ -116,8 +106,7 @@ static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length) * @param slave 从机地址 * @return 0:成功 */ -static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave) -{ +static int agile_modbus_rtu_set_slave(agile_modbus_t* ctx, int slave) { ctx->slave = slave; return 0; } @@ -131,10 +120,11 @@ static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave) * @param req 数据存放指针 * @return 数据长度 */ -static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function, - int addr, int nb, - uint8_t *req) -{ +static int agile_modbus_rtu_build_request_basis(agile_modbus_t* ctx, + int function, + int addr, + int nb, + uint8_t* req) { req[0] = ctx->slave; req[1] = function; req[2] = addr >> 8; @@ -151,8 +141,8 @@ static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int functio * @param rsp 数据存放指针 * @return 数据长度 */ -static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp) -{ +static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t* sft, + uint8_t* rsp) { rsp[0] = sft->slave; rsp[1] = sft->function; @@ -161,13 +151,14 @@ static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_ /** * @brief RTU 准备响应接口 - * @note 该 API 会将 req_length 自动减去 AGILE_MODBUS_RTU_CHECKSUM_LENGTH 长度 + * @note 该 API 会将 req_length 自动减去 AGILE_MODBUS_RTU_CHECKSUM_LENGTH + * 长度 * @param req 请求数据指针 * @param req_length 请求数据长度 * @return 0 (RTU 没有事务标识符) */ -static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length) -{ +static int agile_modbus_rtu_prepare_response_tid(const uint8_t* req, + int* req_length) { (*req_length) -= AGILE_MODBUS_RTU_CHECKSUM_LENGTH; /* No TID */ return 0; @@ -180,8 +171,7 @@ static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_le * @param req_length 已有数据长度 * @return 数据长度 */ -static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length) -{ +static int agile_modbus_rtu_send_msg_pre(uint8_t* req, int req_length) { uint16_t crc = agile_modbus_rtu_crc16(req, req_length); req[req_length++] = crc >> 8; req[req_length++] = crc & 0x00FF; @@ -196,8 +186,9 @@ static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length) * @param msg_length 有效数据长度 * @return >0:有效数据长度; 其他:异常 */ -static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length) -{ +static int agile_modbus_rtu_check_integrity(agile_modbus_t* ctx, + uint8_t* msg, + const int msg_length) { uint16_t crc_calculated; uint16_t crc_received; @@ -220,9 +211,10 @@ static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, c * @param rsp_length 响应数据长度 * @return 0:成功; 其他:异常 */ -static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, - const uint8_t *rsp, int rsp_length) -{ +static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t* ctx, + const uint8_t* req, + const uint8_t* rsp, + int rsp_length) { /* Check responding slave is the slave we requested (except for broacast * request) */ if (req[0] != rsp[0] && req[0] != AGILE_MODBUS_BROADCAST_ADDRESS) @@ -242,19 +234,18 @@ static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const ui /** * @brief RTU 后端接口 */ -static const agile_modbus_backend_t agile_modbus_rtu_backend = - { - AGILE_MODBUS_BACKEND_TYPE_RTU, - AGILE_MODBUS_RTU_HEADER_LENGTH, - AGILE_MODBUS_RTU_CHECKSUM_LENGTH, - AGILE_MODBUS_RTU_MAX_ADU_LENGTH, - agile_modbus_rtu_set_slave, - agile_modbus_rtu_build_request_basis, - agile_modbus_rtu_build_response_basis, - agile_modbus_rtu_prepare_response_tid, - agile_modbus_rtu_send_msg_pre, - agile_modbus_rtu_check_integrity, - agile_modbus_rtu_pre_check_confirmation}; +static const agile_modbus_backend_t agile_modbus_rtu_backend = { + AGILE_MODBUS_BACKEND_TYPE_RTU, + AGILE_MODBUS_RTU_HEADER_LENGTH, + AGILE_MODBUS_RTU_CHECKSUM_LENGTH, + AGILE_MODBUS_RTU_MAX_ADU_LENGTH, + agile_modbus_rtu_set_slave, + agile_modbus_rtu_build_request_basis, + agile_modbus_rtu_build_response_basis, + agile_modbus_rtu_prepare_response_tid, + agile_modbus_rtu_send_msg_pre, + agile_modbus_rtu_check_integrity, + agile_modbus_rtu_pre_check_confirmation}; /** * @} @@ -273,9 +264,13 @@ static const agile_modbus_backend_t agile_modbus_rtu_backend = * @param read_bufsz 接收缓冲区大小 * @return 0:成功 */ -int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz) -{ - agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz); +int agile_modbus_rtu_init(agile_modbus_rtu_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz) { + agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, + read_bufsz); ctx->_ctx.backend = &agile_modbus_rtu_backend; ctx->_ctx.backend_data = ctx; diff --git a/package/modbus/agile_modbus_rtu.h b/package/modbus/agile_modbus_rtu.h index 062aa84a6..a50017c02 100644 --- a/package/modbus/agile_modbus_rtu.h +++ b/package/modbus/agile_modbus_rtu.h @@ -27,7 +27,7 @@ extern "C" { /** @defgroup RTU_Exported_Constants RTU Exported Constants * @{ */ -#define AGILE_MODBUS_RTU_HEADER_LENGTH 1 +#define AGILE_MODBUS_RTU_HEADER_LENGTH 1 #define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6 #define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2 @@ -63,7 +63,11 @@ typedef struct agile_modbus_rtu { /** @addtogroup RTU_Exported_Functions * @{ */ -int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz); +int agile_modbus_rtu_init(agile_modbus_rtu_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz); /** * @} */ diff --git a/package/modbus/agile_modbus_tcp.c b/package/modbus/agile_modbus_tcp.c index 478045994..04e7b473d 100644 --- a/package/modbus/agile_modbus_tcp.c +++ b/package/modbus/agile_modbus_tcp.c @@ -28,8 +28,7 @@ * @param slave 从机地址 * @return 0:成功 */ -static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave) -{ +static int agile_modbus_tcp_set_slave(agile_modbus_t* ctx, int slave) { ctx->slave = slave; return 0; } @@ -43,11 +42,12 @@ static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave) * @param req 数据存放指针 * @return 数据长度 */ -static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function, - int addr, int nb, - uint8_t *req) -{ - agile_modbus_tcp_t *ctx_tcp = ctx->backend_data; +static int agile_modbus_tcp_build_request_basis(agile_modbus_t* ctx, + int function, + int addr, + int nb, + uint8_t* req) { + agile_modbus_tcp_t* ctx_tcp = ctx->backend_data; /* Increase transaction ID */ if (ctx_tcp->t_id < UINT16_MAX) @@ -80,8 +80,8 @@ static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int functio * @param rsp 数据存放指针 * @return 数据长度 */ -static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp) -{ +static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t* sft, + uint8_t* rsp) { /* Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b (page 23/46): The transaction identifier is used to associate the future @@ -108,8 +108,8 @@ static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_ * @param req_length 请求数据长度 * @return 事务标识符 */ -static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length) -{ +static int agile_modbus_tcp_prepare_response_tid(const uint8_t* req, + int* req_length) { return (req[0] << 8) + req[1]; } @@ -119,8 +119,7 @@ static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_le * @param req_length 已有数据长度 * @return 数据长度 */ -static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length) -{ +static int agile_modbus_tcp_send_msg_pre(uint8_t* req, int req_length) { /* Substract the header length to the message length */ int mbap_length = req_length - 6; @@ -137,8 +136,9 @@ static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length) * @param msg_length 有效数据长度 * @return 有效数据长度 */ -static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length) -{ +static int agile_modbus_tcp_check_integrity(agile_modbus_t* ctx, + uint8_t* msg, + const int msg_length) { return msg_length; } @@ -150,9 +150,10 @@ static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, c * @param rsp_length 响应数据长度 * @return 0:成功; 其他:异常 */ -static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, - const uint8_t *rsp, int rsp_length) -{ +static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t* ctx, + const uint8_t* req, + const uint8_t* rsp, + int rsp_length) { /* Check transaction ID */ if (req[0] != rsp[0] || req[1] != rsp[1]) return -1; @@ -175,19 +176,18 @@ static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const ui /** * @brief TCP 后端接口 */ -static const agile_modbus_backend_t agile_modbus_tcp_backend = - { - AGILE_MODBUS_BACKEND_TYPE_TCP, - AGILE_MODBUS_TCP_HEADER_LENGTH, - AGILE_MODBUS_TCP_CHECKSUM_LENGTH, - AGILE_MODBUS_TCP_MAX_ADU_LENGTH, - agile_modbus_tcp_set_slave, - agile_modbus_tcp_build_request_basis, - agile_modbus_tcp_build_response_basis, - agile_modbus_tcp_prepare_response_tid, - agile_modbus_tcp_send_msg_pre, - agile_modbus_tcp_check_integrity, - agile_modbus_tcp_pre_check_confirmation}; +static const agile_modbus_backend_t agile_modbus_tcp_backend = { + AGILE_MODBUS_BACKEND_TYPE_TCP, + AGILE_MODBUS_TCP_HEADER_LENGTH, + AGILE_MODBUS_TCP_CHECKSUM_LENGTH, + AGILE_MODBUS_TCP_MAX_ADU_LENGTH, + agile_modbus_tcp_set_slave, + agile_modbus_tcp_build_request_basis, + agile_modbus_tcp_build_response_basis, + agile_modbus_tcp_prepare_response_tid, + agile_modbus_tcp_send_msg_pre, + agile_modbus_tcp_check_integrity, + agile_modbus_tcp_pre_check_confirmation}; /** * @} @@ -206,9 +206,13 @@ static const agile_modbus_backend_t agile_modbus_tcp_backend = * @param read_bufsz 接收缓冲区大小 * @return 0:成功 */ -int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz) -{ - agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz); +int agile_modbus_tcp_init(agile_modbus_tcp_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz) { + agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, + read_bufsz); ctx->_ctx.backend = &agile_modbus_tcp_backend; ctx->_ctx.backend_data = ctx; diff --git a/package/modbus/agile_modbus_tcp.h b/package/modbus/agile_modbus_tcp.h index 50475e073..722ade26e 100644 --- a/package/modbus/agile_modbus_tcp.h +++ b/package/modbus/agile_modbus_tcp.h @@ -27,7 +27,7 @@ extern "C" { /** @defgroup TCP_Exported_Constants TCP Exported Constants * @{ */ -#define AGILE_MODBUS_TCP_HEADER_LENGTH 7 +#define AGILE_MODBUS_TCP_HEADER_LENGTH 7 #define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12 #define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8 @@ -54,10 +54,10 @@ extern "C" { */ typedef struct agile_modbus_tcp { agile_modbus_t _ctx; /**< modbus 句柄 */ - uint16_t t_id; /**< Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b - (page 23/46): - The transaction identifier is used to associate the future response - with the request. This identifier is unique on each TCP connection. */ + uint16_t t_id; /**< Extract from MODBUS Messaging on TCP/IP Implementation + Guide V1.0b (page 23/46): The transaction identifier is used + to associate the future response with the request. This + identifier is unique on each TCP connection. */ } agile_modbus_tcp_t; /** @@ -67,7 +67,11 @@ typedef struct agile_modbus_tcp { /** @addtogroup TCP_Exported_Functions * @{ */ -int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz); +int agile_modbus_tcp_init(agile_modbus_tcp_t* ctx, + uint8_t* send_buf, + int send_bufsz, + uint8_t* read_buf, + int read_bufsz); /** * @} */ diff --git a/port/linux/test/python/modbus/rtu_request.py b/port/linux/test/python/modbus/rtu_request.py new file mode 100644 index 000000000..18c127ed9 --- /dev/null +++ b/port/linux/test/python/modbus/rtu_request.py @@ -0,0 +1,14 @@ +import modbus + +# Create a ModBusRTU object, specify the send buffer and receive buffer size as 128 bytes +mb = modbus.ModBusRTU(128, 128) + +# Set slave address to 1 +op_code = 'x06' +slave_addr = 0x13 +reg_addr = 0x00 +reg_value = 0x01 +mb.serializeRequest(op_code, reg_addr, reg_value, slave_addr) +res = b'\x13\x06\x00\x00\x00\x01\x4b\x78' +ret = mb.deserializeResponse('x06', res) +assert ret == 1