Agile Modbus  1.1.4
Lightweight modbus protocol stack.
agile_modbus.c File Reference

Agile Modbus software package common source file. More...

#include "agile_modbus.h"
#include <string.h>
Include dependency graph for agile_modbus.c:

Go to the source code of this file.

Macros

#define AGILE_MODBUS_MSG_LENGTH_UNDEFINED   -1
 The corresponding function code data length is undefined. More...
 

Functions

static uint8_t agile_modbus_compute_meta_length_after_function (agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
 The length of the data element to be received after calculating the function code. More...
 
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)
 The length of data to be received after calculating the data element. More...
 
static int agile_modbus_receive_msg_judge (agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
 Check the correctness of received data. More...
 
void agile_modbus_common_init (agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
 initialize modbus handle More...
 
int agile_modbus_set_slave (agile_modbus_t *ctx, int slave)
 set address More...
 
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))
 sets the data element length callback function to be received after calculating the function code of the modbus object More...
 
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))
 sets the data length callback function to be received after calculating the data element of the modbus object More...
 
int agile_modbus_receive_judge (agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type)
 Verify the correctness of received data. More...
 
static int agile_modbus_check_confirmation (agile_modbus_t *ctx, uint8_t *req, uint8_t *rsp, int rsp_length)
 Check and confirm the slave response data. More...
 
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_compute_response_length_from_request (agile_modbus_t *ctx, uint8_t *req)
 Calculate the expected response data length. More...
 
int agile_modbus_serialize_raw_request (agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length)
 Pack the original data into a request message. More...
 
int agile_modbus_deserialize_raw_response (agile_modbus_t *ctx, int msg_length)
 parses the original response data More...
 
static int agile_modbus_serialize_response_exception (agile_modbus_t *ctx, agile_modbus_sft_t *sft, int exception_code)
 packaged exception response data More...
 
void agile_modbus_slave_io_set (uint8_t *buf, int index, int status)
 slave IO settings More...
 
uint8_t agile_modbus_slave_io_get (uint8_t *buf, int index)
 Read slave IO status. More...
 
void agile_modbus_slave_register_set (uint8_t *buf, int index, uint16_t data)
 slave register settings More...
 
uint16_t agile_modbus_slave_register_get (uint8_t *buf, int index)
 Read slave register data. More...
 
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)
 slave data processing More...
 

Detailed Description

Agile Modbus software package common source file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2022-07-28
   use:
   Users need to implement the `send data`, `wait for data reception to end` and `clear the receive buffer` functions of the hardware interface.

   - Host:
       1. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` initializes `RTU/TCP` environment
       2. `agile_modbus_set_slave` sets the slave address
       3. `Clear the receive cache`
       4. `agile_modbus_serialize_xxx` package request data
       5. `Send data`
       6. `Waiting for data reception to end`
       7. `agile_modbus_deserialize_xxx` Parse response data
       8. Data processed by users

   - Slave machine:
       1. Implement the `agile_modbus_slave_callback_t` type callback function
       2. `agile_modbus_rtu_init` / `agile_modbus_tcp_init` initializes `RTU/TCP` environment
       3. `agile_modbus_set_slave` sets the slave address
       4. `Waiting for data reception to end`
       5. `agile_modbus_slave_handle` processes request data
       6. `Clear the receive buffer` (optional)
       7. `Send data`
Attention

© Copyright (c) 2021 Ma Longwei. All rights reserved.

Definition in file agile_modbus.c.