libmodbus 3.1.4源码分析

libmodbus 3.1.4源码分析:


modbus 功能码:
/* Modbus function codes */
#define MODBUS_FC_READ_COILS                0x01
#define MODBUS_FC_READ_DISCRETE_INPUTS      0x02
#define MODBUS_FC_READ_HOLDING_REGISTERS    0x03
#define MODBUS_FC_READ_INPUT_REGISTERS      0x04
#define MODBUS_FC_WRITE_SINGLE_COIL         0x05
#define MODBUS_FC_WRITE_SINGLE_REGISTER     0x06
#define MODBUS_FC_READ_EXCEPTION_STATUS     0x07
#define MODBUS_FC_WRITE_MULTIPLE_COILS      0x0F
#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS  0x10
#define MODBUS_FC_REPORT_SLAVE_ID           0x11
#define MODBUS_FC_MASK_WRITE_REGISTER       0x16
#define MODBUS_FC_WRITE_AND_READ_REGISTERS  0x17


modbus-rtu.c对应modbus-private.h里面的函数:
const modbus_backend_t _modbus_rtu_backend = {
    _MODBUS_BACKEND_TYPE_RTU,
    _MODBUS_RTU_HEADER_LENGTH,
    _MODBUS_RTU_CHECKSUM_LENGTH,
    MODBUS_RTU_MAX_ADU_LENGTH,
    _modbus_set_slave,-->set_slave
    _modbus_rtu_build_request_basis,-->build_request_basis
    _modbus_rtu_build_response_basis,-->build_response_basis
    _modbus_rtu_prepare_response_tid,-->prepare_response_tid
    _modbus_rtu_send_msg_pre,-->send_msg_pre
    _modbus_rtu_send,-->send
    _modbus_rtu_receive,-->receive
    _modbus_rtu_recv,-->recv
    _modbus_rtu_check_integrity,-->check_integrity
    _modbus_rtu_pre_check_confirmation,-->pre_check_confirmation
    _modbus_rtu_connect,-->connect
    _modbus_rtu_close,-->close
    _modbus_rtu_flush,-->flush
    _modbus_rtu_select,-->select
    _modbus_rtu_free-->free
};
modbus-private.h对应上面的函数:
typedef struct _modbus_backend {
    unsigned int backend_type;
    unsigned int header_length;
    unsigned int checksum_length;
    unsigned int max_adu_length;
    int (*set_slave) (modbus_t *ctx, int slave);
    int (*build_request_basis) (modbus_t *ctx, int function, int addr,
                                int nb, uint8_t *req);
    int (*build_response_basis) (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);
    ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);
    int (*receive) (modbus_t *ctx, uint8_t *req);
    ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);
    int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
                            const int msg_length);
    int (*pre_check_confirmation) (modbus_t *ctx, const uint8_t *req,
                                   const uint8_t *rsp, int rsp_length);
    int (*connect) (modbus_t *ctx);
    void (*close) (modbus_t *ctx);
    int (*flush) (modbus_t *ctx);
    int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
    void (*free) (modbus_t *ctx);
} modbus_backend_t;
#define MODBUS_FC_READ_HOLDING_REGISTERS    0x03
0x03读寄存器:
int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)
传入MODBUS_FC_READ_HOLDING_REGISTERS    0x03确定发送的功能是读保持寄存器:
   status = read_registers(ctx, MODBUS_FC_READ_HOLDING_REGISTERS,
                            addr, nb, dest);
   rc = send_msg(ctx, req, req_length);
   msg_length = ctx->backend->send_msg_pre(msg, msg_length);-->  modbus-private.h里面 typedef struct _modbus_backend 结构体里面的 int (*send_msg_pre) (uint8_t *req, int req_length);对应
modbus-rtu.c --> const modbus_backend_t _modbus_rtu_backend 结构体里面_modbus_rtu_send_msg_pre,
static int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
{
uint16_t crc = crc16(req, req_length);
req[req_length++] = crc >> 8;
req[req_length++] = crc & 0x00FF;
return req_length;
}
#define MODBUS_FC_READ_COILS                0x01
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
传入MODBUS_FC_READ_COILS                0x01确定读线圈寄存器
rc = read_io_status(ctx, MODBUS_FC_READ_COILS, addr, nb, dest);
/* Reads IO status */
static int read_io_status(modbus_t *ctx, int function,int addr, int nb, uint8_t *dest)


rc = send_msg(ctx, req, req_length);
rc = ctx->backend->send(ctx, msg, msg_length);
typedef struct _modbus_backend {
ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);对就下面modbus-rtu.c中的发送函数
static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
































你可能感兴趣的:(嵌入式,QT)