fix .gitignore

This commit is contained in:
Lyon 2023-07-09 23:50:40 +08:00
parent 17f5a09e25
commit e5144767a4
8 changed files with 917 additions and 703 deletions

12
.gitignore vendored
View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -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);
/**
* @}
*/

View File

@ -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;

View File

@ -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);
/**
* @}
*/

View File

@ -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;

View File

@ -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);
/**
* @}
*/

View File

@ -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