Agile Modbus: Agile Modbus

Agile Modbus 1.1.3
Lightweight modbus protocol stack.
Agile Modbus

1. Introduction

Agile Modbus is a lightweight modbus protocol stack that meets the needs of users in any scenario.

  • Online documentation: API Manual
  • The examples folder provides examples on PC
  • See examples on MCU mcu_demos
  • Bootloader based on RT-Thread on AT32F437 that supports Modbus firmware upgrade: AT32F437_Boot
  • Bootloader based on RT-Thread on HPM6750 that supports Modbus firmware upgrade: HPM6750_Boot

1.1. Features

  1. Supports rtu and tcp protocols, is developed using pure C, does not involve any hardware interface, and can be used directly on any form of hardware.
  2. Since it is developed using pure C and does not involve hardware, it can run the tcp protocol on the serial port and the rtu protocol on the network.
  3. Support custom protocols compliant with modbus format.
  4. Supports multiple masters and multiple slaves at the same time.
  5. It is easy to use. You only need to initialize the rtu or tcp handle and call the corresponding API to package and unpack.

1.2. Directory structure

Name | Description
doc | documentation
examples | examples
figures | materials
inc | header file
src | source code
util | Provides simple and practical components

1.3. License

Agile Modbus complies with the Apache-2.0 license, see the LICENSE file for details.

2. Use Agile Modbus

Please view the help document doc/doxygen/Agile_Modbus.chm

2.1. Transplantation

  • Users need to implement the send data, wait for data reception to end and clear the receive buffer functions of the hardware interface

    Regarding waiting for data reception to end, the following ideas are provided:

    1. General method

      Every 20 /50 ms (this time can be set according to the baud rate and hardware, here is just a reference value) reads data from the hardware interface and stores it in the buffer and updates the offset until it cannot be read or the buffer is full. , exit reading.

      This applies to both bare metal and operating systems, which can accomplish blocking via select or semaphore.

    2. Serial port DMA + IDLE interrupt mode

      Configure the DMA + IDLE interrupt, enable the flag in the interrupt, and determine whether the flag is set in the application program.

      However, this solution is prone to problems. If the data bytes are slightly staggered, it will not be a frame. The first option is recommended.

  • 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

  • Special function code

    You need to call the agile_modbus_set_compute_meta_length_after_function_cb and agile_modbus_set_compute_data_length_after_meta_cb APIs to set the callbacks for special function codes to be processed in master-slave mode.

    • agile_modbus_set_compute_meta_length_after_function_cb

      msg_type == AGILE_MODBUS_MSG_INDICATION: Returns the data element length of the host request message (uint8_t type). If it is not a special function code, 0 must be returned.

      msg_type == MSG_CONFIRMATION: Returns the data element length (uint8_t type) of the slave response message. If it is not a special function code, 1 must be returned.

    • agile_modbus_set_compute_data_length_after_meta_cb

      msg_type == AGILE_MODBUS_MSG_INDICATION: Returns the data length after the data element of the host request message. If it is not a special function code, 0 must be returned.

      msg_type == MSG_CONFIRMATION: Returns the data length after the data element of the slave response message. If it is not a special function code, it must return 0.

  • agile_modbus_rtu_init / agile_modbus_tcp_init

    When initializing the RTU/TCP environment, the user needs to pass in the send buffer and receive buffer. It is recommended that the size of both buffers is AGILE_MODBUS_MAX_ADU_LENGTH (260) bytes. Special function code is determined by the user according to the agreement.

    But for small memory MCUs, these two buffers can also be set small, and all APIs will judge the buffer size:

    Send buffer setting: If expected request data length or expected response data length is greater than set send buffer size, an exception is returned.

    Receive buffer setting: If the message length requested by the host is greater than the set receive buffer size, an exception will be returned. This is reasonable. When a small memory MCU is used as a slave, certain function codes must be restricted.

2.2. Host

See 2.1. Transplantation.

2.3. Slave machine

2.3.1. Interface description

  • Introduction to agile_modbus_slave_handle

    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)
    agile_modbus_slave_callback_t
    int(* agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
    Slave callback function.
    Definition: agile_modbus.h:265
    agile_modbus_slave_handle
    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
    Definition: agile_modbus.c:1203
    agile_modbus
    Agile Modbus structure.
    Definition: agile_modbus.h:217

    msg_length: The length of data received after waiting for the end of data reception.

    slave_strict: slave address strictness check (0: Do not judge whether the address is consistent, it will be processed by user callback; 1: The address must be consistent, otherwise the callback will not be called and the response data will not be packaged).

    slave_cb: agile_modbus_slave_callback_t type callback function, implemented and passed in by the user. If it is NULL, all function codes can respond and are successful, but the register data is still 0.

    slave_data: slave callback function private data.

    frame_length: Get the length of the parsed modbus data frame. The meaning of this parameter is:

    1. There is dirty data at the end: it can still be parsed successfully and tells the user the real modbus frame length, which the user can process.
    2. Data sticky packet: The data consists of a complete frame of modbus data + a partial modbus data frame. After the user obtains the real modbus frame length, he can remove the processed modbus data frame and read the hardware interface data and the current one again. Part of the modbus data frame forms a new frame
    3. This parameter is often used when modbus broadcast transmits big data (such as custom function code broadcast to upgrade firmware). Ordinary slave responses are one question and one answer, and only the complete data frame is processed. It is recommended to Execute clear receive cache

  • Introduction to agile_modbus_slave_callback_t

    typedef int (*agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data);
    agile_modbus_slave_info
    Agile Modbus slave information structure.
    Definition: agile_modbus.h:246

    agile_modbus_slave_info:

    sft: Contains slave address and function code attributes, which can be used in callbacks

    rsp_length: response data length pointer, its value needs to be updated when processing special function code in the callback, otherwise not allowed to change

    address: register address (not used by all function codes)

    nb: number (not used by all function codes)

    buf: data field required by different function codes (not used by all function codes)

    send_index: the current index of the send buffer (not used by all function codes)

  • agile_modbus_slave_info used by different function codes

    • AGILE_MODBUS_FC_READ_COILS、AGILE_MODBUS_FC_READ_DISCRETE_INPUTS

      The address, nb, and send_index attributes need to be used, and the agile_modbus_slave_io_set API needs to be called to store the IO data in the data area starting from ctx->send_buf + send_index.

    • AGILE_MODBUS_FC_READ_HOLDING_REGISTERS、AGILE_MODBUS_FC_READ_INPUT_REGISTERS

      The address, nb, and send_index attributes need to be used, and the agile_modbus_slave_register_set API needs to be called to store the register data in the data area starting from ctx->send_buf + send_index.

    • AGILE_MODBUS_FC_WRITE_SINGLE_COIL、AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER

      You need to use the address and buf attributes, force buf to the int * type, and get the value and store it in a register.

    • AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS

      The address, nb, buf attributes need to be used, and the agile_modbus_slave_io_get API needs to be called to obtain the IO data to be written.

    • AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS

      The address, nb, and buf attributes need to be used, and the agile_modbus_slave_register_get API needs to be called to obtain the register data to be written.

    • AGILE_MODBUS_FC_MASK_WRITE_REGISTER

      You need to use the address and buf attributes, pass (buf[0] << 8) + buf[1] to get the and value, pass (buf[2] << 8) + buf[3 ] Gets the or value. Get the register value data, perform the data = (data & and) | (or & (~and)) operation to update the data value, and write it to the register.

    • AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS

      You need to use the address, buf, send_index attributes, pass (buf[0] << 8) + buf[1] to get the number of registers to be read, pass (buf[2] << 8) + buf[3] Get the register address to be written, and use (buf[4] << 8) + buf[5] to get the number of registers to be written. You need to call the agile_modbus_slave_register_get API to obtain the register data to be written, and call the agile_modbus_slave_register_set API to store the register data in the data area starting from ctx->send_buf + send_index.

    • Custom function code

      You need to use the send_index, nb, and buf attributes, and the user processes the data in the callback.

      send_index: current index of send buffer

      nb: PUD - 1, which is the modbus data field length

      buf: starting position of modbus data field

      Note: After the user fills data into the send buffer in the callback, the rsp_length value of agile_modbus_slave_info needs to be updated.

2.3.2. Simple slave access interface

Agile Modbus provides an implementation of agile_modbus_slave_callback_t, allowing users to access it simply and conveniently.

See examples/slave for examples of usage.

How to use:

#include "agile_modbus.h"
#include "agile_modbus_slave_util.h"
const agile_modbus_slave_util_t slave_util = {
/* User implementation */
};
agile_modbus_slave_handle(ctx, read_len, 0, agile_modbus_slave_util_callback, &slave_util, NULL);
agile_modbus.h
Agile Modbus software package common header file.
agile_modbus_slave_util.h
The simple slave access header file provided by the Agile Modbus software package.
agile_modbus_slave_util_callback
int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
Definition: agile_modbus_slave_util.c:387
agile_modbus_slave_util
slave function structure
Definition: agile_modbus_slave_util.h:48
  • Introduction to agile_modbus_slave_util_callback
    • An implementation of agile_modbus_slave_callback_t provided by Agile Modbus, which requires agile_modbus_slave_util_t type variable pointer as private data.
    • The private data is NULL, all function codes can respond and are successful, but the register data is still 0.

  • Introduction to agile_modbus_slave_util_t

    typedef struct agile_modbus_slave_util {
    const agile_modbus_slave_util_map_t *tab_bits;
    int nb_bits;
    const agile_modbus_slave_util_map_t *tab_input_bits;
    int nb_input_bits;
    const agile_modbus_slave_util_map_t *tab_registers;
    int nb_registers;
    const agile_modbus_slave_util_map_t *tab_input_registers;
    int nb_input_registers;
    int (*addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info);
    int (*special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info);
    int (*done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret);
    } agile_modbus_slave_util_t;
    agile_modbus_slave_util_t
    struct agile_modbus_slave_util agile_modbus_slave_util_t
    slave function structure
    agile_modbus_slave_util_map
    slave register mapping structure
    Definition: agile_modbus_slave_util.h:38
    agile_modbus_slave_util::addr_check
    int(* addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
    Address checking interface.
    Definition: agile_modbus_slave_util.h:57
    agile_modbus_slave_util::nb_input_registers
    int nb_input_registers
    Input register definition array number.
    Definition: agile_modbus_slave_util.h:56
    agile_modbus_slave_util::nb_bits
    int nb_bits
    The number of coil register definition arrays.
    Definition: agile_modbus_slave_util.h:50
    agile_modbus_slave_util::tab_input_registers
    const agile_modbus_slave_util_map_t * tab_input_registers
    Input register definition array.
    Definition: agile_modbus_slave_util.h:55
    agile_modbus_slave_util::special_function
    int(* special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
    Special function code processing interface.
    Definition: agile_modbus_slave_util.h:58
    agile_modbus_slave_util::nb_input_bits
    int nb_input_bits
    The number of discrete input register definition arrays.
    Definition: agile_modbus_slave_util.h:52
    agile_modbus_slave_util::tab_input_bits
    const agile_modbus_slave_util_map_t * tab_input_bits
    Discrete input register definition array.
    Definition: agile_modbus_slave_util.h:51
    agile_modbus_slave_util::done
    int(* done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)
    Processing end interface.
    Definition: agile_modbus_slave_util.h:59
    agile_modbus_slave_util::nb_registers
    int nb_registers
    Number of holding register definition arrays.
    Definition: agile_modbus_slave_util.h:54
    agile_modbus_slave_util::tab_bits
    const agile_modbus_slave_util_map_t * tab_bits
    Coil register definition array.
    Definition: agile_modbus_slave_util.h:49
    agile_modbus_slave_util::tab_registers
    const agile_modbus_slave_util_map_t * tab_registers
    Holding register definition array.
    Definition: agile_modbus_slave_util.h:53

    • Register related

      Users need to implement the definitions of bits, input_bits, registers and input_registers. If a register is defined as NULL, the function code corresponding to the register can respond and is successful, but the register data is all 0.

    • Interface calling process

  • Introduction to agile_modbus_slave_util_map

    typedef struct agile_modbus_slave_util_map {
    int start_addr;
    int end_addr;
    int (*get)(void *buf, int bufsz);
    int (*set)(int index, int len, void *buf, int bufsz);
    } agile_modbus_slave_util_map_t;
    agile_modbus_slave_util_map_t
    struct agile_modbus_slave_util_map agile_modbus_slave_util_map_t
    slave register mapping structure
    agile_modbus_slave_util_map::get
    int(* get)(void *buf, int bufsz)
    Get register data interface.
    Definition: agile_modbus_slave_util.h:41
    agile_modbus_slave_util_map::start_addr
    int start_addr
    Start address.
    Definition: agile_modbus_slave_util.h:39
    agile_modbus_slave_util_map::end_addr
    int end_addr
    end address
    Definition: agile_modbus_slave_util.h:40
    agile_modbus_slave_util_map::set
    int(* set)(int index, int len, void *buf, int bufsz)
    Set register data interface.
    Definition: agile_modbus_slave_util.h:42
    • Precautions:
      • The number of registers determined by the start address and end address is limited. Changing the size of the map_buf array inside the function can make it larger.
        • bit register < 250
        • register register < 125
      • The interface function is NULL, and the function code corresponding to the register can respond and is successful.

    • get interface

      Copy all data in the address field to buf.

    • set interface

      • index: offset within the address field
      • len: length

      Modify data based on index and len.

2.4. Example

  • Examples on PC are provided in the examples folder, which can be compiled and run under WSL or Linux.
    • Examples of RTU/TCP master and slave

    • Examples of special function codes

      RTU point-to-point transmission of files: Demonstrates the use of special function codes

      RTU broadcast transmission file: Demonstrates the use of frame_length in agile_modbus_slave_handle

  • mcu_demos provides examples on MCU.
  • AT32F437_Boot A Bootloader based on RT-Thread implemented on AT32F437 that supports Modbus firmware upgrade.
  • HPM6750_Boot A Bootloader based on RT-Thread implemented on HPM6750 that supports Modbus firmware upgrade.

2.5. Doxygen document generation

  • Use Doxywizard to open Doxyfile and run it. The generated file will be under doxygen/output.
  • Graphviz path needs to be changed.
  • HTML is generated without using chm format. If it is enabled, you need to change the hhc.exe path.

3. Support

If Agile Modbus solves your problem, you might as well scan the QR code above and invite me for a cup of coffee ~

4. Contact information & thanks

  • Maintenance: Ma Longwei
  • Home page: https://github.com/loogg/agile_modbus
  • Email: 25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com
Generated by doxygen 1.9.2


Agile Modbus: Update record

Update record

Agile Modbus 1.1.0 released

New function

2021-12-02: Ma Longwei

  • Add Doxygen comments and generate documentation

2021-12-28: Ma Longwei

  • Add RTU and TCP host examples
  • Add RTU and TCP slave examples
  • Add sample document

2022-01-08: Ma Longwei

  • Add RTU point-to-point transmission file example
  • Add RTU broadcast transmission file example

Revise

2022-01-06: Ma Longwei

  • Modify the slave example, RTU and TCP use the same slave callback
  • TCP slave supports up to 5 client access
  • The TCP slave will automatically disconnect if it does not receive the correct message within 10s.

2022-01-08: Ma Longwei

  • Remove the length limit in receiving data judgment
  • Remove agile_modbus_serialize_raw_request's length limit on raw data

Agile Modbus 1.1.1 released

Revise

2022-06-22: Ma Longwei

  • README.md adds a bootloader link that supports Modbus firmware upgrade based on RT-Thread on AT32F437
  • Add HPM6750_Boot link
  • Change LICENSE to Apache-2.0

Agile Modbus 1.1.2 released

New function

2022-07-28: Ma Longwei

  • Provide simple slave access agile_modbus_slave_util_callback interface

Revise

2022-07-28: Ma Longwei

  • agile_modbus_slave_handle adds slave callback private data parameter
  • agile_modbus_slave_callback_t adds private data parameter
  • The slave example in examples uses the agile_modbus_slave_util_callback interface to implement register reading and writing

Agile Modbus 1.1.3 released

Revise

2022-11-22: Ma Longwei

  • Writing a single register in agile_modbus_slave_handle will point the slave_info.buf pointer to the local variable address. This address will be used by other variables after turning on compiler optimization. Modify it to point to the global variable address within the function.


Agile Modbus: Example description

Example description

1. Introduction

This example provides functional demonstration of RTU / TCP master and slave.

Using gcc under WSL or Linux, you can directly make all to compile all examples and run the test program on your computer.

Directory Structure:

Name | Description
common | public source code
figures | materials
rtu_master | RTU master example
tcp_master | TCP master example
slave | RTU + TCP slave example
rtu_p2p | RTU peer-to-peer transfer file
rtu_broadcast | RTU broadcast transmission file (sticky packet processing example)

2. Use

The tools you need to prepare are as follows:

  • Virtual serial port software
  • Modbus Poll
  • Modbus Slave

Type make clean 、 make all on the command line.

2.1. Host

  • RTU (rtu_master)

    • Use virtual serial port software to virtualize a pair of serial ports

    • Open Modbus Slave and set as shown below

    • Modbus Slave connection, set as shown below

    • Enter the rtu_master directory, ./RtuMaster /dev/ttySX to run the RTU host example, ttySX is the other of a pair of virtual serial ports

  • TCP (tcp_master)
    • Open Modbus Slave, SetUp settings are consistent with RTU

    • Modbus Slave connection, set as shown below

    • Enter the tcp_master directory, ./TcpMaster 127.0.0.1 502 and run the TCP host example

2.2. Slave machine

  • This example (slave) provides both RTU and TCP slave function demonstrations, controlling the same memory. TCP can connect to a maximum of 5 clients. Each client has a no-data timeout of 10s and will be automatically disconnected after 10s.
  • The example supports all function codes (except function code 0x07).
  • bit, input_bit, register, input_register registers are defined separately for each file.
  • Use agile_modbus_slave_util_callback.
  • Register address field:
Register | Address range
Coil register | 0x041A ~ 0x0423 (1050 ~ 1059)
Discrete input register | 0x041A ~ 0x0423 (1050 ~ 1059)
Holding register | 0xFFF6 ~ 0xFFFF (65526 ~ 65535)
Input register | 0xFFF6 ~ 0xFFFF (65526 ~ 65535)

Note: Reading and writing other address registers can be successful, but the values ​​are all 0.

use:

  • Use virtual serial port software to virtualize a pair of serial ports

  • Enter the slave directory, ./ModbusSlave /dev/ttyS2 1025 to run the example

    /dev/ttySX: One of the virtual serial ports

    1025: Listening port number. If you do not have root permissions, the port number must be greater than 1024

  • Open Modbus Poll and set and connect RTU as shown below

  • Open 5 Modbus Poll, set the same as RTU, and connect as shown below

  • Effect demonstration

  • Timeout disconnect demonstration

    Close the poll interface of Modbus Poll and look at the console print to see the close message.

2.3. RTU transmission file

Use 0x50 as the special function code for transferring files.

File data is transferred in packets, with a maximum size of 1024 bytes per packet.

Data field protocol definition:

  • Host request
command | number of bytes | data
2 Bytes | 2 Bytes | N Bytes

Order:

Command | Description | Data
0x0001 | Start sending | File size (4 Bytes) + file name (string)
0x0002 | Transmission data | Flag (1 Byte) + file data

Logo:

Status | Description
0x00 | The last packet of data
0x01 | Not the last packet of data
  • Slave response
Command | Status
2 Bytes | 1 Byte

state:

Status | Description
0x00 | Failure
0x01 | Success

  • Use virtual serial port software to virtualize 3 serial ports to form a serial port group

    Here I use the MX virtual serial port

2.3.1. Point-to-point transmission

  • Enter the rtu_p2p directory and open Linux Shell. The demonstration effect is as follows

    Notice:

    • The transferred files must be general files. Executable files, directories, etc. are not supported.
    • The file path must be the path in Linux environment
    • After the slave receives the data, the file name is modified (slave address_original file name) and written in the current directory.

2.3.2. Broadcast transmission

This example mainly demonstrates the use of frame_length in agile_modbus_slave_handle.

In broadcast_master, use broadcast address 0 and send data packets every 5ms. At the same time, 100 bytes of dirty data are sent after each packet of data.

int send_len = agile_modbus_serialize_raw_request(ctx, raw_req, raw_req_len);
serial_send(_fd, ctx->send_buf, send_len);
//dirty data
serial_send(_fd, _dirty_buf, sizeof(_dirty_buf));
agile_modbus_serialize_raw_request
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.
Definition: agile_modbus.c:1031

Under such a fast data flow, broadcast_slave must use the frame_length parameter in agile_modbus_slave_handle to handle sticky packets.

  • Enter the rtu_broadcast directory and open Linux Shell. The demonstration effect is as follows

    Notice:

    • The transferred files must be general files. Executable files, directories, etc. are not supported.
    • The file path must be the path in Linux environment
    • After the slave receives the data, the file name is modified (slave address_original file name) and written in the current directory.


Agile Modbus: Modules

Modules
Here is a list of all modules:
[detail level 123]
▼ Common |
Modbus Module Definition |
▼ Common Exported Constants |
Modbus Function Codes |
Modbus Constants |
Common Exported Types |
Common Private Constants |
Common Private Functions |
Common Exported Functions |
▼ Modbus Master |
Master Private Functions |
Master Common Operation Functions | Commonly used modbus host operation functions
Master Raw Operation Functions |
▼ Modbus Slave |
Slave Exported Types |
Slave Private Functions |
Slave Operation Functions |
▼ RTU |
RTU Exported Constants |
RTU Exported Types |
RTU Private Constants |
RTU Private Functions |
RTU Exported Functions |
▼ TCP |
TCP Exported Constants |
TCP Exported Types |
TCP Private Functions |
TCP Private Constants |
TCP Exported Functions |
▼ UTIL |
▼ Slave Util |
Slave Util Private Functions |
Slave Util Exported Functions |
Slave Util Exported Types |


Agile Modbus: Common

Modules
Common
Collaboration diagram for Common:

Modules

Modbus Module Definition
Common Exported Constants
Common Exported Types
Common Private Constants
Common Private Functions
Common Exported Functions
Modbus Master
Modbus Slave

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Modbus Module Definition

Modbus Module Definition
Common
Collaboration diagram for Modbus Module Definition:


Agile Modbus: Common Exported Constants

Modules
Common Exported Constants
Common
Collaboration diagram for Common Exported Constants:

Modules

Modbus Function Codes
Modbus Constants

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Modbus Function Codes

Macros
Modbus Function Codes
Common » Common Exported Constants
Collaboration diagram for Modbus Function Codes:

Macros

#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_WRITE_AND_READ_REGISTERS 0x17

Detailed Description

Macro Definition Documentation

◆ AGILE_MODBUS_FC_READ_COILS

#define AGILE_MODBUS_FC_READ_COILS 0x01

Definition at line 48 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_READ_DISCRETE_INPUTS

#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02

Definition at line 49 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_READ_HOLDING_REGISTERS

#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03

Definition at line 50 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_READ_INPUT_REGISTERS

#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04

Definition at line 51 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_WRITE_SINGLE_COIL

#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05

Definition at line 52 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER

#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06

Definition at line 53 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_READ_EXCEPTION_STATUS

#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07

Definition at line 54 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS

#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F

Definition at line 55 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS

#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10

Definition at line 56 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_REPORT_SLAVE_ID

#define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11

Definition at line 57 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_MASK_WRITE_REGISTER

#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16

Definition at line 58 of file agile_modbus.h.

◆ AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS

#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17

Definition at line 59 of file agile_modbus.h.


Agile Modbus: Modbus Constants

Macros
Modbus Constants
Common » Common Exported Constants
Collaboration diagram for Modbus Constants:

Macros

#define | AGILE_MODBUS_VERSION_STRING "AMB_1.1.3"
Agile Modbus version number. More...
#define | AGILE_MODBUS_BROADCAST_ADDRESS 0
Modbus broadcast address. More...
#define | AGILE_MODBUS_MAX_PDU_LENGTH 253
#define | AGILE_MODBUS_MAX_ADU_LENGTH 260

Quantity limit of Coils

   Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
   Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
   (chapter 6 section 11 page 29)
   Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
#define | AGILE_MODBUS_MAX_READ_BITS 2000
#define | AGILE_MODBUS_MAX_WRITE_BITS 1968

Quantity limit of Registers

   Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
   Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
   (chapter 6 section 12 page 31)
   Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
   (chapter 6 section 17 page 38)
   Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
#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

Detailed Description

Macro Definition Documentation

◆ AGILE_MODBUS_VERSION_STRING

#define AGILE_MODBUS_VERSION_STRING "AMB_1.1.3"

Agile Modbus version number.

Definition at line 67 of file agile_modbus.h.

◆ AGILE_MODBUS_BROADCAST_ADDRESS

#define AGILE_MODBUS_BROADCAST_ADDRESS 0

Modbus broadcast address.

Definition at line 69 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_READ_BITS

#define AGILE_MODBUS_MAX_READ_BITS 2000

Definition at line 81 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_WRITE_BITS

#define AGILE_MODBUS_MAX_WRITE_BITS 1968

Definition at line 82 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_READ_REGISTERS

#define AGILE_MODBUS_MAX_READ_REGISTERS 125

Definition at line 99 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_WRITE_REGISTERS

#define AGILE_MODBUS_MAX_WRITE_REGISTERS 123

Definition at line 100 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_WR_WRITE_REGISTERS

#define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121

Definition at line 101 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_WR_READ_REGISTERS

#define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125

Definition at line 102 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_PDU_LENGTH

#define AGILE_MODBUS_MAX_PDU_LENGTH 253
   The size of the MODBUS PDU is limited by the size constraint inherited from
   the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
   bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
   address (1 byte) - CRC (2 bytes) = 253 bytes.

Definition at line 116 of file agile_modbus.h.

◆ AGILE_MODBUS_MAX_ADU_LENGTH

#define AGILE_MODBUS_MAX_ADU_LENGTH 260
   Consequently:
   - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256
   bytes.
   - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
   so the maximum of both backend in 260 bytes. This size can used to allocate
   an array of bytes to store responses and it will be compatible with the two
   backends.

Definition at line 130 of file agile_modbus.h.


Agile Modbus: Common Exported Types

Data Structures | Typedefs | Enumerations
Common Exported Types
Common
Collaboration diagram for Common Exported Types:

Data Structures

struct | agile_modbus_sft
contains the modbus header parameter structure More...
struct | agile_modbus_backend
Agile Modbus backend interface structure. More...
struct | agile_modbus
Agile Modbus structure. More...

Typedefs

typedef struct agile_modbus_sft | agile_modbus_sft_t
contains the modbus header parameter structure More...
typedef struct agile_modbus | agile_modbus_t
Agile Modbus structure. More...
typedef struct agile_modbus_backend | agile_modbus_backend_t
Agile Modbus backend interface structure. More...

Enumerations

enum {
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01 , AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS , AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE , AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE ,
AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE , AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY , AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE , AGILE_MODBUS_EXCEPTION_MEMORY_PARITY ,
AGILE_MODBUS_EXCEPTION_NOT_DEFINED , AGILE_MODBUS_EXCEPTION_GATEWAY_PATH , AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET , AGILE_MODBUS_EXCEPTION_UNKNOW = 0xff
}
Modbus exception code. More...
enum | agile_modbus_backend_type_t { AGILE_MODBUS_BACKEND_TYPE_RTU = 0 , AGILE_MODBUS_BACKEND_TYPE_TCP }
@ brief Modbus backend type More...
enum | agile_modbus_msg_type_t { AGILE_MODBUS_MSG_INDICATION , AGILE_MODBUS_MSG_CONFIRMATION }
Modbus received message type. More...

Detailed Description

Typedef Documentation

◆ agile_modbus_sft_t

typedef struct agile_modbus_sft agile_modbus_sft_t

contains the modbus header parameter structure

◆ agile_modbus_t

typedef struct agile_modbus agile_modbus_t

Agile Modbus structure.

Definition at line 193 of file agile_modbus.h.

◆ agile_modbus_backend_t

typedef struct agile_modbus_backend agile_modbus_backend_t

Agile Modbus backend interface structure.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Modbus exception code.

Enumerator
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE
AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE
AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
AGILE_MODBUS_EXCEPTION_MEMORY_PARITY
AGILE_MODBUS_EXCEPTION_NOT_DEFINED
AGILE_MODBUS_EXCEPTION_GATEWAY_PATH
AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET
AGILE_MODBUS_EXCEPTION_UNKNOW

Definition at line 146 of file agile_modbus.h.

◆ agile_modbus_backend_type_t

enum agile_modbus_backend_type_t

@ brief Modbus backend type

Enumerator
AGILE_MODBUS_BACKEND_TYPE_RTU | RTU.
AGILE_MODBUS_BACKEND_TYPE_TCP | TCP.

Definition at line 164 of file agile_modbus.h.

◆ agile_modbus_msg_type_t

enum agile_modbus_msg_type_t

Modbus received message type.

   ---------- Request     Indication ----------
   | Client | ---------------------->| Server |
   ---------- Confirmation  Response ----------
Enumerator
AGILE_MODBUS_MSG_INDICATION | Host-side request message.
AGILE_MODBUS_MSG_CONFIRMATION | Server-side request message.

Definition at line 179 of file agile_modbus.h.


Agile Modbus: agile_modbus_sft Struct Reference

Data Fields
agile_modbus_sft Struct Reference
Common » Common Exported Types

contains the modbus header parameter structure More...

#include <agile_modbus.h>

Data Fields

int | slave
slave address More...
int | function
function code More...
int | t_id
Transaction identifier. More...

Detailed Description

contains the modbus header parameter structure

Definition at line 187 of file agile_modbus.h.

Field Documentation

◆ slave

int agile_modbus_sft::slave

slave address

Definition at line 188 of file agile_modbus.h.

◆ function

int agile_modbus_sft::function

function code

Definition at line 189 of file agile_modbus.h.

◆ t_id

int agile_modbus_sft::t_id

Transaction identifier.

Definition at line 190 of file agile_modbus.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h


Agile Modbus: agile_modbus_backend Struct Reference

Data Fields
agile_modbus_backend Struct Reference
Common » Common Exported Types

Agile Modbus backend interface structure. More...

#include <agile_modbus.h>

Data Fields

uint32_t | backend_type
Backend type. More...
uint32_t | header_length
Header length, excluding function code. More...
uint32_t | checksum_length
Check data length. More...
uint32_t | max_adu_length
Backend ADU length. More...
int(* | set_slave )(agile_modbus_t *ctx, int slave)
Set address interface. More...
int(* | build_request_basis )(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
Build a basic request message interface. More...
int(* | build_response_basis )(agile_modbus_sft_t *sft, uint8_t *rsp)
Build a basic response message interface. More...
int(* | prepare_response_tid )(const uint8_t *req, int *req_length)
Prepare response interface. More...
int(* | send_msg_pre )(uint8_t *req, int req_length)
Pre-send data interface. More...
int(* | check_integrity )(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
Check the receive data integrity interface. More...
int(* | pre_check_confirmation )(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
Pre-check confirmation interface. More...

Detailed Description

Agile Modbus backend interface structure.

Definition at line 198 of file agile_modbus.h.

Field Documentation

◆ backend_type

uint32_t agile_modbus_backend::backend_type

Backend type.

Definition at line 199 of file agile_modbus.h.

◆ header_length

uint32_t agile_modbus_backend::header_length

Header length, excluding function code.

Definition at line 200 of file agile_modbus.h.

◆ checksum_length

uint32_t agile_modbus_backend::checksum_length

Check data length.

Definition at line 201 of file agile_modbus.h.

◆ max_adu_length

uint32_t agile_modbus_backend::max_adu_length

Backend ADU length.

Definition at line 202 of file agile_modbus.h.

◆ set_slave

int(* agile_modbus_backend::set_slave) (agile_modbus_t *ctx, int slave)

Set address interface.

Definition at line 203 of file agile_modbus.h.

◆ build_request_basis

int(* agile_modbus_backend::build_request_basis) (agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)

Build a basic request message interface.

Definition at line 204 of file agile_modbus.h.

◆ build_response_basis

int(* agile_modbus_backend::build_response_basis) (agile_modbus_sft_t *sft, uint8_t *rsp)

Build a basic response message interface.

Definition at line 206 of file agile_modbus.h.

◆ prepare_response_tid

int(* agile_modbus_backend::prepare_response_tid) (const uint8_t *req, int *req_length)

Prepare response interface.

Definition at line 207 of file agile_modbus.h.

◆ send_msg_pre

int(* agile_modbus_backend::send_msg_pre) (uint8_t *req, int req_length)

Pre-send data interface.

Definition at line 208 of file agile_modbus.h.

◆ check_integrity

int(* agile_modbus_backend::check_integrity) (agile_modbus_t *ctx, uint8_t *msg, const int msg_length)

Check the receive data integrity interface.

Definition at line 209 of file agile_modbus.h.

◆ pre_check_confirmation

int(* agile_modbus_backend::pre_check_confirmation) (agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)

Pre-check confirmation interface.

Definition at line 210 of file agile_modbus.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h


Agile Modbus: agile_modbus Struct Reference

Data Fields
agile_modbus Struct Reference
Common » Common Exported Types

Agile Modbus structure. More...

#include <agile_modbus.h>

Collaboration diagram for agile_modbus:
[legend]

Data Fields

int | slave
slave address More...
uint8_t * | send_buf
Send buffer. More...
int | send_bufsz
Send buffer size. More...
uint8_t * | read_buf
Receive buffer. More...
int | read_bufsz
Receive buffer size. More...
uint8_t(* | compute_meta_length_after_function )(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
Customized calculation data element length interface. More...
int(* | compute_data_length_after_meta )(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
Customized calculation data length interface. More...
const agile_modbus_backend_t * | backend
Backend interface. More...
void * | backend_data
Backend data, pointing to RTU or TCP structure. More...

Detailed Description

Agile Modbus structure.

Definition at line 217 of file agile_modbus.h.

Field Documentation

◆ slave

int agile_modbus::slave

slave address

Definition at line 218 of file agile_modbus.h.

◆ send_buf

uint8_t* agile_modbus::send_buf

Send buffer.

Definition at line 219 of file agile_modbus.h.

◆ send_bufsz

int agile_modbus::send_bufsz

Send buffer size.

Definition at line 220 of file agile_modbus.h.

◆ read_buf

uint8_t* agile_modbus::read_buf

Receive buffer.

Definition at line 221 of file agile_modbus.h.

◆ read_bufsz

int agile_modbus::read_bufsz

Receive buffer size.

Definition at line 222 of file agile_modbus.h.

◆ compute_meta_length_after_function

uint8_t(* agile_modbus::compute_meta_length_after_function) (agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)

Customized calculation data element length interface.

Definition at line 223 of file agile_modbus.h.

◆ compute_data_length_after_meta

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)

Customized calculation data length interface.

Definition at line 225 of file agile_modbus.h.

◆ backend

const agile_modbus_backend_t* agile_modbus::backend

Backend interface.

Definition at line 227 of file agile_modbus.h.

◆ backend_data

void* agile_modbus::backend_data

Backend data, pointing to RTU or TCP structure.

Definition at line 228 of file agile_modbus.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h


Agile Modbus: Common Private Constants

Macros
Common Private Constants
Common
Collaboration diagram for Common Private Constants:

Macros

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

Detailed Description

Macro Definition Documentation

◆ AGILE_MODBUS_MSG_LENGTH_UNDEFINED

#define AGILE_MODBUS_MSG_LENGTH_UNDEFINED -1

The corresponding function code data length is undefined.

Definition at line 49 of file agile_modbus.c.


Agile Modbus: Common Private Functions

Functions
Common Private Functions
Common
Collaboration diagram for Common Private Functions:

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

Detailed Description

Function Documentation

◆ agile_modbus_compute_meta_length_after_function()

static uint8_t agile_modbus_compute_meta_length_after_function | ( | agile_modbus_t * | ctx,
 |  | int | function,
 |  | agile_modbus_msg_type_t | msg_type
 | ) |  |
static

The length of the data element to be received after calculating the function code.

   ---------- Request     Indication ----------
   | Client | ---------------------->| Server |
   ---------- Confirmation  Response ----------

   Take the 03 function code request message as an example

   ---------- ------ --------------- ---------
   | header | | 03 | | 00 00 00 01 | | CRC16 |
   ---------- ------ --------------- ---------

   ----------
   | header |
   ----------
       RTU: device address
       TCP: | transaction identifier  protocol identifier  length  unit identifier |

   ---------------
   | 00 00 00 01 |
   ---------------
       Data element: Data related to the function code, such as 03. The function code data element contains the register starting address and register length.
Parameters
ctx | modbus handle
function | function code
msg_type | message type
Returns
data element length

Definition at line 88 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_compute_data_length_after_meta()

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

The length of data to be received after calculating the data element.

   ---------- Request     Indication ----------
   | Client | ---------------------->| Server |
   ---------- Confirmation  Response ----------

   Example of responding to a message with function code 03

   ---------- ------ ------ --------- ---------
   | header | | 03 | | 02 | | 00 00 | | CRC16 |
   ---------- ------ ------ --------- ---------

   ----------
   | header |
   ----------
       RTU: device address
       TCP: | transaction identifier  protocol identifier  length  unit identifier |

   ------
   | 02 |
   ------
       Data element: two bytes of data

   ---------
   | 00 00 |
   ---------
       data
Parameters
ctx | modbus handle
msg | message pointer
msg_length | message length
msg_type | message type
Returns
data length

Definition at line 177 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_receive_msg_judge()

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

Check the correctness of received data.

Parameters
ctx | modbus handle
msg | message pointer
msg_length | message length
msg_type | message type
Returns
>0: correct, modbus data frame length; others: exception

Definition at line 224 of file agile_modbus.c.

Here is the call graph for this function:
Here is the caller graph for this function:


Agile Modbus: Common Exported Functions

Functions
Common Exported Functions
Common
Collaboration diagram for Common Exported Functions:

Functions

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

Detailed Description

Function Documentation

◆ agile_modbus_common_init()

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

Parameters
ctx | modbus handle
send_buf | send buffer
send_bufsz | send buffer size
read_buf | receive buffer
read_bufsz | receive buffer size

Definition at line 257 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_set_slave()

int agile_modbus_set_slave | ( | agile_modbus_t * | ctx,
 |  | int | slave
 | ) |  |

set address

Parameters
ctx | modbus handle
slave | address
Returns
0: success

Definition at line 273 of file agile_modbus.c.

◆ agile_modbus_set_compute_meta_length_after_function_cb()

void agile_modbus_set_compute_meta_length_after_function_cb | ( | agile_modbus_t * | ctx,
 |  | uint8_t(*)(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type) | cb
 | ) |  |

sets the data element length callback function to be received after calculating the function code of the modbus object

Parameters
ctx | modbus handle
cb | callback function of the data element length to be received after calculating the function code
See also
agile_modbus_compute_meta_length_after_function

Definition at line 284 of file agile_modbus.c.

◆ agile_modbus_set_compute_data_length_after_meta_cb()

void agile_modbus_set_compute_data_length_after_meta_cb | ( | agile_modbus_t * | ctx,
 |  | int(*)(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type) | cb
 | ) |  |

sets the data length callback function to be received after calculating the data element of the modbus object

Parameters
ctx | modbus handle
cb | The data length callback function to be received after calculating the data element
See also
agile_modbus_compute_data_length_after_meta

Definition at line 297 of file agile_modbus.c.

◆ agile_modbus_receive_judge()

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.

Note
This API returns the modbus data frame length, for example, 8 bytes of modbus data frame + 2 bytes of dirty data, returns 8
Parameters
ctx | modbus handle
msg_length | received data length
msg_type | message type
Returns
>0: correct, modbus data frame length; others: exception

Definition at line 312 of file agile_modbus.c.

Here is the call graph for this function:
Here is the caller graph for this function:


Agile Modbus: Modbus Master

Modules
Modbus Master
Common
Collaboration diagram for Modbus Master:

Modules

Master Private Functions
Master Common Operation Functions
Commonly used modbus host operation functions.
Master Raw Operation Functions

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Master Private Functions

Functions
Master Private Functions
Common » Modbus Master
Collaboration diagram for Master Private Functions:

Functions

static int | agile_modbus_compute_response_length_from_request (agile_modbus_t *ctx, uint8_t *req)
Calculate the expected response data length. 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...

Detailed Description

Function Documentation

◆ agile_modbus_compute_response_length_from_request()

static int agile_modbus_compute_response_length_from_request | ( | agile_modbus_t * | ctx,
 |  | uint8_t * | req
 | ) |  |
static

Calculate the expected response data length.

Note
If it is a special function code, AGILE_MODBUS_MSG_LENGTH_UNDEFINED is returned, but this does not mean an exception. When agile_modbus_check_confirmation calls this API processing, it is considered that the return value of AGILE_MODBUS_MSG_LENGTH_UNDEFINED is also valid.
Parameters
ctx | modbus handle
req | request data pointer
Returns
expected response data length

Definition at line 342 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_check_confirmation()

static int agile_modbus_check_confirmation | ( | agile_modbus_t * | ctx,
 |  | uint8_t * | req,
 |  | uint8_t * | rsp,
 |  | int | rsp_length
 | ) |  |
static

Check and confirm the slave response data.

Parameters
ctx | modbus handle
req | request data pointer
rsp | response data pointer
rsp_length | response data length
Returns
>=0: The length of the corresponding function code response object (such as 03 function code, the value represents the number of registers); Others: exception (-1: message error; others: exception code can be obtained according to -128 -$return value)

Definition at line 391 of file agile_modbus.c.

Here is the call graph for this function:
Here is the caller graph for this function:


Agile Modbus: Master Common Operation Functions

Functions
Master Common Operation Functions
Common » Modbus Master

Commonly used modbus host operation functions. More...

Collaboration diagram for Master Common Operation Functions:

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)

Detailed Description

Commonly used modbus host operation functions.

   The API form is as follows:
   - agile_modbus_serialize_xxx    package request data
   return value:
       >0: Request data length
       Others: Abnormal

   - agile_modbus_deserialize_xxx  parses response data
   return value:
       >=0: The length of the corresponding function code response object (such as 03 function code, the value represents the number of registers)
       Others: exception (-1: message error; others: exception code can be obtained according to `-128 -$return value`)

Function Documentation

◆ agile_modbus_serialize_read_bits()

int agile_modbus_serialize_read_bits | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb
 | ) |  |

Definition at line 493 of file agile_modbus.c.

◆ agile_modbus_deserialize_read_bits()

int agile_modbus_deserialize_read_bits | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | uint8_t * | dest
 | ) |  |

Definition at line 509 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_read_input_bits()

int agile_modbus_serialize_read_input_bits | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb
 | ) |  |

Definition at line 548 of file agile_modbus.c.

◆ agile_modbus_deserialize_read_input_bits()

int agile_modbus_deserialize_read_input_bits | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | uint8_t * | dest
 | ) |  |

Definition at line 564 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_read_registers()

int agile_modbus_serialize_read_registers | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb
 | ) |  |

Definition at line 603 of file agile_modbus.c.

◆ agile_modbus_deserialize_read_registers()

int agile_modbus_deserialize_read_registers | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | uint16_t * | dest
 | ) |  |

Definition at line 619 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_read_input_registers()

int agile_modbus_serialize_read_input_registers | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb
 | ) |  |

Definition at line 647 of file agile_modbus.c.

◆ agile_modbus_deserialize_read_input_registers()

int agile_modbus_deserialize_read_input_registers | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | uint16_t * | dest
 | ) |  |

Definition at line 663 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_write_bit()

int agile_modbus_serialize_write_bit | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | status
 | ) |  |

Definition at line 691 of file agile_modbus.c.

◆ agile_modbus_deserialize_write_bit()

int agile_modbus_deserialize_write_bit | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

Definition at line 704 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_write_register()

int agile_modbus_serialize_write_register | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | const uint16_t | value
 | ) |  |

Definition at line 721 of file agile_modbus.c.

◆ agile_modbus_deserialize_write_register()

int agile_modbus_deserialize_write_register | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

Definition at line 734 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_write_bits()

int agile_modbus_serialize_write_bits | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb,
 |  | const uint8_t * | src
 | ) |  |

Definition at line 751 of file agile_modbus.c.

◆ agile_modbus_deserialize_write_bits()

int agile_modbus_deserialize_write_bits | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

Definition at line 796 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_write_registers()

int agile_modbus_serialize_write_registers | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | int | nb,
 |  | const uint16_t * | src
 | ) |  |

Definition at line 813 of file agile_modbus.c.

◆ agile_modbus_deserialize_write_registers()

int agile_modbus_deserialize_write_registers | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

Definition at line 844 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_mask_write_register()

int agile_modbus_serialize_mask_write_register | ( | agile_modbus_t * | ctx,
 |  | int | addr,
 |  | uint16_t | and_mask,
 |  | uint16_t | or_mask
 | ) |  |

Definition at line 861 of file agile_modbus.c.

◆ agile_modbus_deserialize_mask_write_register()

int agile_modbus_deserialize_mask_write_register | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

Definition at line 883 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_write_and_read_registers()

int agile_modbus_serialize_write_and_read_registers | ( | agile_modbus_t * | ctx,
 |  | int | write_addr,
 |  | int | write_nb,
 |  | const uint16_t * | src,
 |  | int | read_addr,
 |  | int | read_nb
 | ) |  |

Definition at line 900 of file agile_modbus.c.

◆ agile_modbus_deserialize_write_and_read_registers()

int agile_modbus_deserialize_write_and_read_registers | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | uint16_t * | dest
 | ) |  |

Definition at line 941 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_serialize_report_slave_id()

int agile_modbus_serialize_report_slave_id ( agile_modbus_t * ctx )

Definition at line 969 of file agile_modbus.c.

◆ agile_modbus_deserialize_report_slave_id()

int agile_modbus_deserialize_report_slave_id | ( | agile_modbus_t * | ctx,
 |  | int | msg_length,
 |  | int | max_dest,
 |  | uint8_t * | dest
 | ) |  |

Definition at line 984 of file agile_modbus.c.

Here is the call graph for this function:


Agile Modbus: Master Raw Operation Functions

Functions
Master Raw Operation Functions
Common » Modbus Master
Collaboration diagram for Master Raw Operation Functions:

Functions

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

Detailed Description

Function Documentation

◆ agile_modbus_serialize_raw_request()

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.

Parameters
ctx | modbus handle
raw_req | original message (PDU + Slave address)
raw_req_length | original message length
Returns
>0: Request data length; Others: Exception

Definition at line 1031 of file agile_modbus.c.

◆ agile_modbus_deserialize_raw_response()

int agile_modbus_deserialize_raw_response | ( | agile_modbus_t * | ctx,
 |  | int | msg_length
 | ) |  |

parses the original response data

Parameters
ctx | modbus handle
msg_length | received data length
Returns
>=0: The length of the corresponding function code response object (such as 03 function code, the value represents the number of registers); Others: exception (-1: message error; others: exception code can be obtained according to -128 -$return value)

Definition at line 1073 of file agile_modbus.c.

Here is the call graph for this function:


Agile Modbus: Modbus Slave

Modules
Modbus Slave
Common
Collaboration diagram for Modbus Slave:

Modules

Slave Exported Types
Slave Private Functions
Slave Operation Functions

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Slave Exported Types

Data Structures | Typedefs
Slave Exported Types
Common » Modbus Slave
Collaboration diagram for Slave Exported Types:

Data Structures

struct agile_modbus_slave_info
Agile Modbus slave information structure. More...

Typedefs

typedef int(* agile_modbus_slave_callback_t) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function. More...

Detailed Description

Typedef Documentation

◆ agile_modbus_slave_callback_t

typedef int(* agile_modbus_slave_callback_t) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)

Slave callback function.

Parameters
ctx | modbus handle
slave_info | slave information body
data | private data
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 265 of file agile_modbus.h.


Agile Modbus: agile_modbus_slave_info Struct Reference

Data Fields
agile_modbus_slave_info Struct Reference
Common » Modbus Slave » Slave Exported Types

Agile Modbus slave information structure. More...

#include <agile_modbus.h>

Collaboration diagram for agile_modbus_slave_info:
[legend]

Data Fields

agile_modbus_sft_t * sft
sft structure pointer More...
int * | rsp_length
Response data length pointer. More...
int | address
Register address. More...
int | nb
number More...
uint8_t * | buf
Data fields required for different function codes. More...
int | send_index
Current index of sending buffer. More...

Detailed Description

Agile Modbus slave information structure.

Definition at line 246 of file agile_modbus.h.

Field Documentation

◆ sft

agile_modbus_sft_t* agile_modbus_slave_info::sft

sft structure pointer

Definition at line 247 of file agile_modbus.h.

◆ rsp_length

int* agile_modbus_slave_info::rsp_length

Response data length pointer.

Definition at line 248 of file agile_modbus.h.

◆ address

int agile_modbus_slave_info::address

Register address.

Definition at line 249 of file agile_modbus.h.

◆ nb

int agile_modbus_slave_info::nb

number

Definition at line 250 of file agile_modbus.h.

◆ buf

uint8_t* agile_modbus_slave_info::buf

Data fields required for different function codes.

Definition at line 251 of file agile_modbus.h.

◆ send_index

int agile_modbus_slave_info::send_index

Current index of sending buffer.

Definition at line 252 of file agile_modbus.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h


Agile Modbus: Slave Private Functions

Functions
Slave Private Functions
Common » Modbus Slave
Collaboration diagram for Slave Private Functions:

Functions

static int agile_modbus_serialize_response_exception (agile_modbus_t *ctx, agile_modbus_sft_t *sft, int exception_code)
packaged exception response data More...

Detailed Description

Function Documentation

◆ agile_modbus_serialize_response_exception()

static int agile_modbus_serialize_response_exception | ( | agile_modbus_t * | ctx,
 |  | agile_modbus_sft_t * | sft,
 |  | int | exception_code
 | ) |  |
static

packaged exception response data

Parameters
ctx | modbus handle
sft | modbus information header
exception_code | exception code
Returns
response data length

Definition at line 1113 of file agile_modbus.c.

Here is the caller graph for this function:


Agile Modbus: Slave Operation Functions

Functions
Slave Operation Functions
Common » Modbus Slave
Collaboration diagram for Slave Operation Functions:

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)
slave data processing 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...

Detailed Description

Function Documentation

◆ agile_modbus_slave_handle()

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

Parameters
ctx | modbus handle
msg_length | received data length
slave_strict slave address strict check flag
  • 0: Do not compare slave addresses
  • 1: Compare slave address
slave_cb | slave callback function
slave_data | slave callback function private data
frame_length | stores modbus data frame length
Returns
>=0: length of data to be responded to; others: exception

warning: comparison is always false due to limited range of data type [-Wtype-limits]

warning: comparison is always false due to limited range of data type [-Wtype-limits]

warning: comparison is always false due to limited range of data type [-Wtype-limits]

Definition at line 1203 of file agile_modbus.c.

Here is the call graph for this function:

◆ agile_modbus_slave_io_set()

void agile_modbus_slave_io_set | ( | uint8_t * | buf,
 |  | int | index,
 |  | int | status
 | ) |  |

slave IO settings

Parameters
buf | stores IO data area
index | IO index (number of IO)
status | IO status

Definition at line 1139 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_slave_io_get()

uint8_t agile_modbus_slave_io_get | ( | uint8_t * | buf,
 |  | int | index
 | ) |  |

Read slave IO status.

Parameters
buf | IO data area
index | IO index (number of IO)
Returns
IO status (1/0)

Definition at line 1156 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_slave_register_set()

void agile_modbus_slave_register_set | ( | uint8_t * | buf,
 |  | int | index,
 |  | uint16_t | data
 | ) |  |

slave register settings

Parameters
buf | storage data area
index | register index (register number)
data | register data

Definition at line 1172 of file agile_modbus.c.

Here is the caller graph for this function:

◆ agile_modbus_slave_register_get()

uint16_t agile_modbus_slave_register_get | ( | uint8_t * | buf,
 |  | int | index
 | ) |  |

Read slave register data.

Parameters
buf | register data area
index | register index (register number)
Returns
register data

Definition at line 1184 of file agile_modbus.c.

Here is the caller graph for this function:


Agile Modbus: RTU

Modules
RTU
Collaboration diagram for RTU:

Modules

RTU Exported Constants
RTU Exported Types
RTU Private Constants
RTU Private Functions
RTU Exported Functions

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: RTU Exported Constants

Macros
RTU Exported Constants
RTU
Collaboration diagram for RTU Exported Constants:

Macros

#define | AGILE_MODBUS_RTU_HEADER_LENGTH 1
#define | AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6
#define | AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2
#define | AGILE_MODBUS_RTU_CHECKSUM_LENGTH 2
#define | AGILE_MODBUS_RTU_MAX_ADU_LENGTH 256

Detailed Description

Macro Definition Documentation

◆ AGILE_MODBUS_RTU_HEADER_LENGTH

#define AGILE_MODBUS_RTU_HEADER_LENGTH 1

Definition at line 32 of file agile_modbus_rtu.h.

◆ AGILE_MODBUS_RTU_PRESET_REQ_LENGTH

#define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6

Definition at line 33 of file agile_modbus_rtu.h.

◆ AGILE_MODBUS_RTU_PRESET_RSP_LENGTH

#define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2

Definition at line 34 of file agile_modbus_rtu.h.

◆ AGILE_MODBUS_RTU_CHECKSUM_LENGTH

#define AGILE_MODBUS_RTU_CHECKSUM_LENGTH 2

Definition at line 36 of file agile_modbus_rtu.h.

◆ AGILE_MODBUS_RTU_MAX_ADU_LENGTH

#define AGILE_MODBUS_RTU_MAX_ADU_LENGTH 256
   Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
   RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes

Definition at line 45 of file agile_modbus_rtu.h.


Agile Modbus: RTU Exported Types

Data Structures | Typedefs
RTU Exported Types
RTU
Collaboration diagram for RTU Exported Types:

Data Structures

struct agile_modbus_rtu
RTU structure. More...

Typedefs

typedef struct agile_modbus_rtu agile_modbus_rtu_t
RTU structure. More...

Detailed Description

Typedef Documentation

◆ agile_modbus_rtu_t

typedef struct agile_modbus_rtu agile_modbus_rtu_t

RTU structure.


Agile Modbus: agile_modbus_rtu Struct Reference

Data Fields
agile_modbus_rtu Struct Reference
RTU » RTU Exported Types

RTU structure. More...

#include <agile_modbus_rtu.h>

Collaboration diagram for agile_modbus_rtu:
[legend]

Data Fields

agile_modbus_t _ctx
modbus handle More...

Detailed Description

RTU structure.

Definition at line 57 of file agile_modbus_rtu.h.

Field Documentation

◆ _ctx

agile_modbus_t agile_modbus_rtu::_ctx

modbus handle

Definition at line 58 of file agile_modbus_rtu.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_rtu.h


Agile Modbus: RTU Private Constants

Variables
RTU Private Constants
RTU
Collaboration diagram for RTU Private Constants:

Variables

static const uint8_t | _table_crc_hi []
Table of CRC values for high-order byte. More...
static const uint8_t | _table_crc_lo []
Table of CRC values for low-order byte. More...
static const agile_modbus_backend_t | agile_modbus_rtu_backend
RTU backend interface. More...

Detailed Description

Variable Documentation

◆ _table_crc_hi

const uint8_t _table_crc_hi[]
static
Initial value:
=
{
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 high-order byte.

Definition at line 28 of file agile_modbus_rtu.c.

◆ _table_crc_lo

const uint8_t _table_crc_lo[]
static
Initial value:
=
{
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}

Table of CRC values for low-order byte.

Definition at line 58 of file agile_modbus_rtu.c.

◆ agile_modbus_rtu_backend

const agile_modbus_backend_t agile_modbus_rtu_backend
static
Initial value:
=
{
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}
AGILE_MODBUS_BACKEND_TYPE_RTU
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU.
Definition: agile_modbus.h:165
AGILE_MODBUS_RTU_HEADER_LENGTH
#define AGILE_MODBUS_RTU_HEADER_LENGTH
Definition: agile_modbus_rtu.h:32
AGILE_MODBUS_RTU_MAX_ADU_LENGTH
#define AGILE_MODBUS_RTU_MAX_ADU_LENGTH
Definition: agile_modbus_rtu.h:45
AGILE_MODBUS_RTU_CHECKSUM_LENGTH
#define AGILE_MODBUS_RTU_CHECKSUM_LENGTH
Definition: agile_modbus_rtu.h:36
agile_modbus_rtu_build_response_basis
static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
RTU builds the basic response message interface (header message)
Definition: agile_modbus_rtu.c:157
agile_modbus_rtu_send_msg_pre
static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
RTU pre-send data interface.
Definition: agile_modbus_rtu.c:186
agile_modbus_rtu_build_request_basis
static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
RTU builds the basic request message interface (header message)
Definition: agile_modbus_rtu.c:137
agile_modbus_rtu_set_slave
static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
RTU sets the address interface.
Definition: agile_modbus_rtu.c:122
agile_modbus_rtu_check_integrity
static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
RTU check received data integrity interface (CRC16 comparison)
Definition: agile_modbus_rtu.c:202
agile_modbus_rtu_pre_check_confirmation
static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
RTU pre-check confirmation interface (request response address comparison)
Definition: agile_modbus_rtu.c:226
agile_modbus_rtu_prepare_response_tid
static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
RTU ready response interface.
Definition: agile_modbus_rtu.c:172

RTU backend interface.

Definition at line 248 of file agile_modbus_rtu.c.


Agile Modbus: RTU Private Functions

Functions
RTU Private Functions
RTU
Collaboration diagram for RTU Private Functions:

Functions

static uint16_t | agile_modbus_rtu_crc16 (uint8_t *buffer, uint16_t buffer_length)
RTU CRC16 calculation. More...
static int | agile_modbus_rtu_set_slave (agile_modbus_t *ctx, int slave)
RTU sets the address interface. More...
static int | agile_modbus_rtu_build_request_basis (agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
RTU builds the basic request message interface (header message) More...
static int | agile_modbus_rtu_build_response_basis (agile_modbus_sft_t *sft, uint8_t *rsp)
RTU builds the basic response message interface (header message) More...
static int | agile_modbus_rtu_prepare_response_tid (const uint8_t *req, int *req_length)
RTU ready response interface. More...
static int | agile_modbus_rtu_send_msg_pre (uint8_t *req, int req_length)
RTU pre-send data interface. More...
static int | agile_modbus_rtu_check_integrity (agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
RTU check received data integrity interface (CRC16 comparison) More...
static int | agile_modbus_rtu_pre_check_confirmation (agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
RTU pre-check confirmation interface (request response address comparison) More...

Detailed Description

Function Documentation

◆ agile_modbus_rtu_crc16()

static uint16_t agile_modbus_rtu_crc16 | ( | uint8_t * | buffer,
 |  | uint16_t | buffer_length
 | ) |  |
static

RTU CRC16 calculation.

Parameters
buffer | data pointer
buffer_length | data length
Returns
CRC16 value

Definition at line 100 of file agile_modbus_rtu.c.

Here is the caller graph for this function:

◆ agile_modbus_rtu_set_slave()

static int agile_modbus_rtu_set_slave | ( | agile_modbus_t * | ctx,
 |  | int | slave
 | ) |  |
static

RTU sets the address interface.

Parameters
ctx | modbus handle
slave | slave address
Returns
0: success

Definition at line 122 of file agile_modbus_rtu.c.

◆ agile_modbus_rtu_build_request_basis()

static int agile_modbus_rtu_build_request_basis | ( | agile_modbus_t * | ctx,
 |  | int | function,
 |  | int | addr,
 |  | int | nb,
 |  | uint8_t * | req
 | ) |  |
static

RTU builds the basic request message interface (header message)

Parameters
ctx | modbus handle
function | function code
addr | register address
nb | number of registers
req | data storage pointer
Returns
data length

Definition at line 137 of file agile_modbus_rtu.c.

◆ agile_modbus_rtu_build_response_basis()

static int agile_modbus_rtu_build_response_basis | ( | agile_modbus_sft_t * | sft,
 |  | uint8_t * | rsp
 | ) |  |
static

RTU builds the basic response message interface (header message)

Parameters
sft | modbus header parameter structure pointer
rsp | data storage pointer
Returns
data length

Definition at line 157 of file agile_modbus_rtu.c.

◆ agile_modbus_rtu_prepare_response_tid()

static int agile_modbus_rtu_prepare_response_tid | ( | const uint8_t * | req,
 |  | int * | req_length
 | ) |  |
static

RTU ready response interface.

Note
This API will automatically subtract the AGILE_MODBUS_RTU_CHECKSUM_LENGTH length from req_length
Parameters
req | request data pointer
req_length | request data length
Returns
0 (RTU has no transaction identifier)

Definition at line 172 of file agile_modbus_rtu.c.

◆ agile_modbus_rtu_send_msg_pre()

static int agile_modbus_rtu_send_msg_pre | ( | uint8_t * | req,
 |  | int | req_length
 | ) |  |
static

RTU pre-send data interface.

Note
This API will calculate CRC16 and automatically fill in the tail
Parameters
req | data storage pointer
req_length | existing data length
Returns
data length

Definition at line 186 of file agile_modbus_rtu.c.

Here is the call graph for this function:

◆ agile_modbus_rtu_check_integrity()

static int agile_modbus_rtu_check_integrity | ( | agile_modbus_t * | ctx,
 |  | uint8_t * | msg,
 |  | const int | msg_length
 | ) |  |
static

RTU check received data integrity interface (CRC16 comparison)

Parameters
ctx | modbus handle
msg | Receive data pointer
msg_length | valid data length
Returns
>0: valid data length; others: exception

Definition at line 202 of file agile_modbus_rtu.c.

Here is the call graph for this function:

◆ agile_modbus_rtu_pre_check_confirmation()

static int agile_modbus_rtu_pre_check_confirmation | ( | agile_modbus_t * | ctx,
 |  | const uint8_t * | req,
 |  | const uint8_t * | rsp,
 |  | int | rsp_length
 | ) |  |
static

RTU pre-check confirmation interface (request response address comparison)

Note
If the request address is broadcast address 0, return success
Parameters
ctx | modbus handle
req | request data pointer
rsp | response data pointer
rsp_length | response data length
Returns
0: success; others: exception

Definition at line 226 of file agile_modbus_rtu.c.


Agile Modbus: RTU Exported Functions

Functions
RTU Exported Functions
RTU
Collaboration diagram for RTU Exported Functions:

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)
RTU initialization. More...

Detailed Description

Function Documentation

◆ agile_modbus_rtu_init()

int agile_modbus_rtu_init | ( | agile_modbus_rtu_t * | ctx,
 |  | uint8_t * | send_buf,
 |  | int | send_bufsz,
 |  | uint8_t * | read_buf,
 |  | int | read_bufsz
 | ) |  |

RTU initialization.

Parameters
ctx | RTU handle
send_buf | send buffer
send_bufsz | send buffer size
read_buf | receive buffer
read_bufsz | receive buffer size
Returns
0: success

Definition at line 279 of file agile_modbus_rtu.c.

Here is the call graph for this function:


Agile Modbus: TCP

Modules
TCP
Collaboration diagram for TCP:

Modules

TCP Exported Constants
TCP Exported Types
TCP Private Functions
TCP Private Constants
TCP Exported Functions

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: TCP Exported Constants

Macros
TCP Exported Constants
TCP
Collaboration diagram for TCP Exported Constants:

Macros

#define | AGILE_MODBUS_TCP_HEADER_LENGTH 7
#define | AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12
#define | AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8
#define | AGILE_MODBUS_TCP_CHECKSUM_LENGTH 0
#define | AGILE_MODBUS_TCP_MAX_ADU_LENGTH 260

Detailed Description

Macro Definition Documentation

◆ AGILE_MODBUS_TCP_HEADER_LENGTH

#define AGILE_MODBUS_TCP_HEADER_LENGTH 7

Definition at line 32 of file agile_modbus_tcp.h.

◆ AGILE_MODBUS_TCP_PRESET_REQ_LENGTH

#define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12

Definition at line 33 of file agile_modbus_tcp.h.

◆ AGILE_MODBUS_TCP_PRESET_RSP_LENGTH

#define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8

Definition at line 34 of file agile_modbus_tcp.h.

◆ AGILE_MODBUS_TCP_CHECKSUM_LENGTH

#define AGILE_MODBUS_TCP_CHECKSUM_LENGTH 0

Definition at line 36 of file agile_modbus_tcp.h.

◆ AGILE_MODBUS_TCP_MAX_ADU_LENGTH

#define AGILE_MODBUS_TCP_MAX_ADU_LENGTH 260
   Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
   TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes

Definition at line 45 of file agile_modbus_tcp.h.


Agile Modbus: TCP Exported Types

Data Structures | Typedefs
TCP Exported Types
TCP
Collaboration diagram for TCP Exported Types:

Data Structures

struct agile_modbus_tcp
TCP structure. More...

Typedefs

typedef struct agile_modbus_tcp agile_modbus_tcp_t
TCP structure. More...

Detailed Description

Typedef Documentation

◆ agile_modbus_tcp_t

typedef struct agile_modbus_tcp agile_modbus_tcp_t

TCP structure.


Agile Modbus: agile_modbus_tcp Struct Reference

Data Fields
agile_modbus_tcp Struct Reference
TCP » TCP Exported Types

TCP structure. More...

#include <agile_modbus_tcp.h>

Collaboration diagram for agile_modbus_tcp:
[legend]

Data Fields

agile_modbus_t _ctx
modbus handle More...
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. More...

Detailed Description

TCP structure.

Definition at line 57 of file agile_modbus_tcp.h.

Field Documentation

◆ _ctx

agile_modbus_t agile_modbus_tcp::_ctx

modbus handle

Definition at line 58 of file agile_modbus_tcp.h.

◆ t_id

uint16_t agile_modbus_tcp::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.

Definition at line 59 of file agile_modbus_tcp.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_tcp.h


Agile Modbus: TCP Private Functions

Functions
TCP Private Functions
TCP
Collaboration diagram for TCP Private Functions:

Functions

static int | agile_modbus_tcp_set_slave (agile_modbus_t *ctx, int slave)
TCP set address interface. More...
static int | agile_modbus_tcp_build_request_basis (agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
TCP builds the basic request message interface (header message) More...
static int | agile_modbus_tcp_build_response_basis (agile_modbus_sft_t *sft, uint8_t *rsp)
TCP builds a basic response message interface (header message) More...
static int | agile_modbus_tcp_prepare_response_tid (const uint8_t *req, int *req_length)
TCP ready response interface. More...
static int | agile_modbus_tcp_send_msg_pre (uint8_t *req, int req_length)
TCP pre-send data interface (calculate the value of the length field and store it) More...
static int | agile_modbus_tcp_check_integrity (agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
TCP check receiving data integrity interface. More...
static int | agile_modbus_tcp_pre_check_confirmation (agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
TCP pre-check confirmation interface (compare transaction identifier and protocol identifier) More...

Detailed Description

Function Documentation

◆ agile_modbus_tcp_set_slave()

static int agile_modbus_tcp_set_slave | ( | agile_modbus_t * | ctx,
 |  | int | slave
 | ) |  |
static

TCP set address interface.

Parameters
ctx | modbus handle
slave | slave address
Returns
0: success

Definition at line 34 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_build_request_basis()

static int agile_modbus_tcp_build_request_basis | ( | agile_modbus_t * | ctx,
 |  | int | function,
 |  | int | addr,
 |  | int | nb,
 |  | uint8_t * | req
 | ) |  |
static

TCP builds the basic request message interface (header message)

Parameters
ctx | modbus handle
function | function code
addr | register address
nb | number of registers
req | data storage pointer
Returns
data length

Definition at line 49 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_build_response_basis()

static int agile_modbus_tcp_build_response_basis | ( | agile_modbus_sft_t * | sft,
 |  | uint8_t * | rsp
 | ) |  |
static

TCP builds a basic response message interface (header message)

Parameters
sft | modbus header parameter structure pointer
rsp | data storage pointer
Returns
data length

Definition at line 86 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_prepare_response_tid()

static int agile_modbus_tcp_prepare_response_tid | ( | const uint8_t * | req,
 |  | int * | req_length
 | ) |  |
static

TCP ready response interface.

Parameters
req | request data pointer
req_length | request data length
Returns
transaction identifier

Definition at line 114 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_send_msg_pre()

static int agile_modbus_tcp_send_msg_pre | ( | uint8_t * | req,
 |  | int | req_length
 | ) |  |
static

TCP pre-send data interface (calculate the value of the length field and store it)

Parameters
req | data storage pointer
req_length | existing data length
Returns
data length

Definition at line 125 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_check_integrity()

static int agile_modbus_tcp_check_integrity | ( | agile_modbus_t * | ctx,
 |  | uint8_t * | msg,
 |  | const int | msg_length
 | ) |  |
static

TCP check receiving data integrity interface.

Parameters
ctx | modbus handle
msg | Receive data pointer
msg_length | valid data length
Returns
valid data length

Definition at line 143 of file agile_modbus_tcp.c.

◆ agile_modbus_tcp_pre_check_confirmation()

static int agile_modbus_tcp_pre_check_confirmation | ( | agile_modbus_t * | ctx,
 |  | const uint8_t * | req,
 |  | const uint8_t * | rsp,
 |  | int | rsp_length
 | ) |  |
static

TCP pre-check confirmation interface (compare transaction identifier and protocol identifier)

Parameters
ctx | modbus handle
req | request data pointer
rsp | response data pointer
rsp_length | response data length
Returns
0: success; others: exception

Definition at line 156 of file agile_modbus_tcp.c.


Agile Modbus: TCP Private Constants

Variables
TCP Private Constants
TCP
Collaboration diagram for TCP Private Constants:

Variables

static const agile_modbus_backend_t agile_modbus_tcp_backend
TCP backend interface. More...

Detailed Description

Variable Documentation

◆ agile_modbus_tcp_backend

const agile_modbus_backend_t agile_modbus_tcp_backend
static
Initial value:
=
{
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}
AGILE_MODBUS_BACKEND_TYPE_TCP
@ AGILE_MODBUS_BACKEND_TYPE_TCP
TCP.
Definition: agile_modbus.h:166
AGILE_MODBUS_TCP_CHECKSUM_LENGTH
#define AGILE_MODBUS_TCP_CHECKSUM_LENGTH
Definition: agile_modbus_tcp.h:36
AGILE_MODBUS_TCP_MAX_ADU_LENGTH
#define AGILE_MODBUS_TCP_MAX_ADU_LENGTH
Definition: agile_modbus_tcp.h:45
AGILE_MODBUS_TCP_HEADER_LENGTH
#define AGILE_MODBUS_TCP_HEADER_LENGTH
Definition: agile_modbus_tcp.h:32
agile_modbus_tcp_pre_check_confirmation
static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
TCP pre-check confirmation interface (compare transaction identifier and protocol identifier)
Definition: agile_modbus_tcp.c:156
agile_modbus_tcp_set_slave
static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
TCP set address interface.
Definition: agile_modbus_tcp.c:34
agile_modbus_tcp_check_integrity
static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
TCP check receiving data integrity interface.
Definition: agile_modbus_tcp.c:143
agile_modbus_tcp_send_msg_pre
static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
TCP pre-send data interface (calculate the value of the length field and store it)
Definition: agile_modbus_tcp.c:125
agile_modbus_tcp_build_request_basis
static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
TCP builds the basic request message interface (header message)
Definition: agile_modbus_tcp.c:49
agile_modbus_tcp_build_response_basis
static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
TCP builds a basic response message interface (header message)
Definition: agile_modbus_tcp.c:86
agile_modbus_tcp_prepare_response_tid
static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
TCP ready response interface.
Definition: agile_modbus_tcp.c:114

TCP backend interface.

Definition at line 181 of file agile_modbus_tcp.c.


Agile Modbus: TCP Exported Functions

Functions
TCP Exported Functions
TCP
Collaboration diagram for TCP Exported Functions:

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)
TCP initialization. More...

Detailed Description

Function Documentation

◆ agile_modbus_tcp_init()

int agile_modbus_tcp_init | ( | agile_modbus_tcp_t * | ctx,
 |  | uint8_t * | send_buf,
 |  | int | send_bufsz,
 |  | uint8_t * | read_buf,
 |  | int | read_bufsz
 | ) |  |

TCP initialization.

Parameters
ctx | TCP handle
send_buf | send buffer
send_bufsz | send buffer size
read_buf | receive buffer
read_bufsz | receive buffer size
Returns
0: success

Definition at line 212 of file agile_modbus_tcp.c.

Here is the call graph for this function:


Agile Modbus: UTIL

Modules
UTIL
Collaboration diagram for UTIL:

Modules

Slave Util

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Slave Util

Modules
Slave Util
UTIL
Collaboration diagram for Slave Util:

Modules

Slave Util Private Functions
Slave Util Exported Functions
Slave Util Exported Types

Detailed Description

Generated by doxygen 1.9.2


Agile Modbus: Slave Util Private Functions

Functions
Slave Util Private Functions
UTIL » Slave Util
Collaboration diagram for Slave Util Private Functions:

Functions

static const agile_modbus_slave_util_map_t * | get_map_by_addr (const agile_modbus_slave_util_map_t *maps, int nb_maps, int address)
Get the mapping object from the mapping object array according to the register address. More...
static int | read_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
read register More...
static int | write_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
write register More...
static int | mask_write_register (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
mask write register More...
static int | write_read_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
Write and read registers. More...

Detailed Description

Function Documentation

◆ get_map_by_addr()

static const agile_modbus_slave_util_map_t * get_map_by_addr | ( | const agile_modbus_slave_util_map_t * | maps,
 |  | int | nb_maps,
 |  | int | address
 | ) |  |
static

Get the mapping object from the mapping object array according to the register address.

Parameters
maps | mapping object array
nb_maps | number of arrays
address | register address
Returns
!=NULL: mapping object; =NULL: failure

Definition at line 37 of file agile_modbus_slave_util.c.

Here is the caller graph for this function:

◆ read_registers()

static int read_registers | ( | agile_modbus_t * | ctx,
 |  | struct agile_modbus_slave_info * | slave_info,
 |  | const agile_modbus_slave_util_t * | slave_util
 | ) |  |
static

read register

Parameters
ctx | modbus handle
slave_info | slave information body
slave_util | slave function structure
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 58 of file agile_modbus_slave_util.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_registers()

static int write_registers | ( | agile_modbus_t * | ctx,
 |  | struct agile_modbus_slave_info * | slave_info,
 |  | const agile_modbus_slave_util_t * | slave_util
 | ) |  |
static

write register

Parameters
ctx | modbus handle
slave_info | slave information body
slave_util | slave function structure
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 141 of file agile_modbus_slave_util.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mask_write_register()

static int mask_write_register | ( | agile_modbus_t * | ctx,
 |  | struct agile_modbus_slave_info * | slave_info,
 |  | const agile_modbus_slave_util_t * | slave_util
 | ) |  |
static

mask write register

Parameters
ctx | modbus handle
slave_info | slave information body
slave_util | slave function structure
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 244 of file agile_modbus_slave_util.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_read_registers()

static int write_read_registers | ( | agile_modbus_t * | ctx,
 |  | struct agile_modbus_slave_info * | slave_info,
 |  | const agile_modbus_slave_util_t * | slave_util
 | ) |  |
static

Write and read registers.

Parameters
ctx | modbus handle
slave_info | slave information body
slave_util | slave function structure
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 291 of file agile_modbus_slave_util.c.

Here is the call graph for this function:
Here is the caller graph for this function:


Agile Modbus: Slave Util Exported Functions

Functions
Slave Util Exported Functions
UTIL » Slave Util
Collaboration diagram for Slave Util Exported Functions:

Functions

int agile_modbus_slave_util_callback (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function. More...

Detailed Description

Function Documentation

◆ agile_modbus_slave_util_callback()

int agile_modbus_slave_util_callback | ( | agile_modbus_t * | ctx,
 |  | struct agile_modbus_slave_info * | slave_info,
 |  | const void * | data
 | ) |  |

Slave callback function.

Parameters
ctx | modbus handle
slave_info | slave information body
data | private data
Returns
=0: normal; <0: Abnormal (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): Unknown exception, the slave will not package the response data) (Other negative exception codes: package exception response data from the opportunity)

Definition at line 387 of file agile_modbus_slave_util.c.

Here is the call graph for this function:


Agile Modbus: Slave Util Exported Types

Data Structures | Typedefs
Slave Util Exported Types
UTIL » Slave Util
Collaboration diagram for Slave Util Exported Types:

Data Structures

struct | agile_modbus_slave_util_map
slave register mapping structure More...
struct | agile_modbus_slave_util
slave function structure More...

Typedefs

typedef struct agile_modbus_slave_util_map | agile_modbus_slave_util_map_t
slave register mapping structure More...
typedef struct agile_modbus_slave_util | agile_modbus_slave_util_t
slave function structure More...

Detailed Description

Typedef Documentation

◆ agile_modbus_slave_util_map_t

typedef struct agile_modbus_slave_util_map agile_modbus_slave_util_map_t

slave register mapping structure

◆ agile_modbus_slave_util_t

typedef struct agile_modbus_slave_util agile_modbus_slave_util_t

slave function structure


Agile Modbus: agile_modbus_slave_util_map Struct Reference

Data Fields
agile_modbus_slave_util_map Struct Reference
UTIL » Slave Util » Slave Util Exported Types

slave register mapping structure More...

#include <agile_modbus_slave_util.h>

Data Fields

int | start_addr
Start address. More...
int | end_addr
end address More...
int(* | get )(void *buf, int bufsz)
Get register data interface. More...
int(* | set )(int index, int len, void *buf, int bufsz)
Set register data interface. More...

Detailed Description

slave register mapping structure

Definition at line 38 of file agile_modbus_slave_util.h.

Field Documentation

◆ start_addr

int agile_modbus_slave_util_map::start_addr

Start address.

Definition at line 39 of file agile_modbus_slave_util.h.

◆ end_addr

int agile_modbus_slave_util_map::end_addr

end address

Definition at line 40 of file agile_modbus_slave_util.h.

◆ get

int(* agile_modbus_slave_util_map::get) (void *buf, int bufsz)

Get register data interface.

Definition at line 41 of file agile_modbus_slave_util.h.

◆ set

int(* agile_modbus_slave_util_map::set) (int index, int len, void *buf, int bufsz)

Set register data interface.

Definition at line 42 of file agile_modbus_slave_util.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.h


Agile Modbus: agile_modbus_slave_util Struct Reference

Data Fields
agile_modbus_slave_util Struct Reference
UTIL » Slave Util » Slave Util Exported Types

slave function structure More...

#include <agile_modbus_slave_util.h>

Collaboration diagram for agile_modbus_slave_util:
[legend]

Data Fields

const agile_modbus_slave_util_map_t * | tab_bits
Coil register definition array. More...
int | nb_bits
The number of coil register definition arrays. More...
const agile_modbus_slave_util_map_t * | tab_input_bits
Discrete input register definition array. More...
int | nb_input_bits
The number of discrete input register definition arrays. More...
const agile_modbus_slave_util_map_t * | tab_registers
Holding register definition array. More...
int | nb_registers
Number of holding register definition arrays. More...
const agile_modbus_slave_util_map_t * | tab_input_registers
Input register definition array. More...
int | nb_input_registers
Input register definition array number. More...
int(* | addr_check )(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Address checking interface. More...
int(* | special_function )(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Special function code processing interface. More...
int(* | done )(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)
Processing end interface. More...

Detailed Description

slave function structure

Definition at line 48 of file agile_modbus_slave_util.h.

Field Documentation

◆ tab_bits

const agile_modbus_slave_util_map_t* agile_modbus_slave_util::tab_bits

Coil register definition array.

Definition at line 49 of file agile_modbus_slave_util.h.

◆ nb_bits

int agile_modbus_slave_util::nb_bits

The number of coil register definition arrays.

Definition at line 50 of file agile_modbus_slave_util.h.

◆ tab_input_bits

const agile_modbus_slave_util_map_t* agile_modbus_slave_util::tab_input_bits

Discrete input register definition array.

Definition at line 51 of file agile_modbus_slave_util.h.

◆ nb_input_bits

int agile_modbus_slave_util::nb_input_bits

The number of discrete input register definition arrays.

Definition at line 52 of file agile_modbus_slave_util.h.

◆ tab_registers

const agile_modbus_slave_util_map_t* agile_modbus_slave_util::tab_registers

Holding register definition array.

Definition at line 53 of file agile_modbus_slave_util.h.

◆ nb_registers

int agile_modbus_slave_util::nb_registers

Number of holding register definition arrays.

Definition at line 54 of file agile_modbus_slave_util.h.

◆ tab_input_registers

const agile_modbus_slave_util_map_t* agile_modbus_slave_util::tab_input_registers

Input register definition array.

Definition at line 55 of file agile_modbus_slave_util.h.

◆ nb_input_registers

int agile_modbus_slave_util::nb_input_registers

Input register definition array number.

Definition at line 56 of file agile_modbus_slave_util.h.

◆ addr_check

int(* agile_modbus_slave_util::addr_check) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)

Address checking interface.

Definition at line 57 of file agile_modbus_slave_util.h.

◆ special_function

int(* agile_modbus_slave_util::special_function) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)

Special function code processing interface.

Definition at line 58 of file agile_modbus_slave_util.h.

◆ done

int(* agile_modbus_slave_util::done) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)

Processing end interface.

Definition at line 59 of file agile_modbus_slave_util.h.

The documentation for this struct was generated from the following file:
  • C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.h


Agile Modbus: Data Structures

Data Structures
Here are the data structures with brief descriptions:
C agile_modbus | Agile Modbus structure
C agile_modbus_backend | Agile Modbus backend interface structure
C agile_modbus_rtu | RTU structure
C agile_modbus_sft | Modbus header parameter structure
C agile_modbus_slave_info | Agile Modbus slave information structure
C agile_modbus_slave_util | Slave function structure
C agile_modbus_slave_util_map | Slave register mapping structure
C agile_modbus_tcp | TCP structure


Agile Modbus: Data Structure Index

Data Structure Index
A
A
agile_modbus
agile_modbus_backend
agile_modbus_rtu
agile_modbus_sft
agile_modbus_slave_info
agile_modbus_slave_util
agile_modbus_slave_util_map
agile_modbus_tcp


Agile Modbus: Data Fields

Here is a list of all struct and union fields with links to the structures/unions they belong to:

- _ -

  • _ctx : agile_modbus_rtu, agile_modbus_tcp

- a -

  • addr_check : agile_modbus_slave_util
  • address : agile_modbus_slave_info

- b -

  • backend : agile_modbus
  • backend_data : agile_modbus
  • backend_type : agile_modbus_backend
  • buf : agile_modbus_slave_info
  • build_request_basis : agile_modbus_backend
  • build_response_basis : agile_modbus_backend

- c -

  • check_integrity : agile_modbus_backend
  • checksum_length : agile_modbus_backend
  • compute_data_length_after_meta : agile_modbus
  • compute_meta_length_after_function : agile_modbus

- d -

  • done : agile_modbus_slave_util

- e -

  • end_addr : agile_modbus_slave_util_map

- f -

  • function : agile_modbus_sft

- g -

  • get : agile_modbus_slave_util_map

- h -

  • header_length : agile_modbus_backend

- m -

  • max_adu_length : agile_modbus_backend

- n -

  • nb : agile_modbus_slave_info
  • nb_bits : agile_modbus_slave_util
  • nb_input_bits : agile_modbus_slave_util
  • nb_input_registers : agile_modbus_slave_util
  • nb_registers : agile_modbus_slave_util

- p -

  • pre_check_confirmation : agile_modbus_backend
  • prepare_response_tid : agile_modbus_backend

- r -

  • read_buf : agile_modbus
  • read_bufsz : agile_modbus
  • rsp_length : agile_modbus_slave_info

- s -

  • send_buf : agile_modbus
  • send_bufsz : agile_modbus
  • send_index : agile_modbus_slave_info
  • send_msg_pre : agile_modbus_backend
  • set : agile_modbus_slave_util_map
  • set_slave : agile_modbus_backend
  • sft : agile_modbus_slave_info
  • slave : agile_modbus, agile_modbus_sft
  • special_function : agile_modbus_slave_util
  • start_addr : agile_modbus_slave_util_map

- t -

  • t_id : agile_modbus_sft, agile_modbus_tcp
  • tab_bits : agile_modbus_slave_util
  • tab_input_bits : agile_modbus_slave_util
  • tab_input_registers : agile_modbus_slave_util
  • tab_registers : agile_modbus_slave_util


Agile Modbus: Data Fields - Variables

- _ -

  • _ctx : agile_modbus_rtu, agile_modbus_tcp

- a -

  • addr_check : agile_modbus_slave_util
  • address : agile_modbus_slave_info

- b -

  • backend : agile_modbus
  • backend_data : agile_modbus
  • backend_type : agile_modbus_backend
  • buf : agile_modbus_slave_info
  • build_request_basis : agile_modbus_backend
  • build_response_basis : agile_modbus_backend

- c -

  • check_integrity : agile_modbus_backend
  • checksum_length : agile_modbus_backend
  • compute_data_length_after_meta : agile_modbus
  • compute_meta_length_after_function : agile_modbus

- d -

  • done : agile_modbus_slave_util

- e -

  • end_addr : agile_modbus_slave_util_map

- f -

  • function : agile_modbus_sft

- g -

  • get : agile_modbus_slave_util_map

- h -

  • header_length : agile_modbus_backend

- m -

  • max_adu_length : agile_modbus_backend

- n -

  • nb : agile_modbus_slave_info
  • nb_bits : agile_modbus_slave_util
  • nb_input_bits : agile_modbus_slave_util
  • nb_input_registers : agile_modbus_slave_util
  • nb_registers : agile_modbus_slave_util

- p -

  • pre_check_confirmation : agile_modbus_backend
  • prepare_response_tid : agile_modbus_backend

- r -

  • read_buf : agile_modbus
  • read_bufsz : agile_modbus
  • rsp_length : agile_modbus_slave_info

- s -

  • send_buf : agile_modbus
  • send_bufsz : agile_modbus
  • send_index : agile_modbus_slave_info
  • send_msg_pre : agile_modbus_backend
  • set : agile_modbus_slave_util_map
  • set_slave : agile_modbus_backend
  • sft : agile_modbus_slave_info
  • slave : agile_modbus, agile_modbus_sft
  • special_function : agile_modbus_slave_util
  • start_addr : agile_modbus_slave_util_map

- t -

  • t_id : agile_modbus_sft, agile_modbus_tcp
  • tab_bits : agile_modbus_slave_util
  • tab_input_bits : agile_modbus_slave_util
  • tab_input_registers : agile_modbus_slave_util
  • tab_registers : agile_modbus_slave_util


Agile Modbus: File List

File List
Here is a list of all files with brief descriptions:
[detail level 12]
examples |
▼ inc |
agile_modbus.h | Agile Modbus software package common header file
agile_modbus_rtu.h | Agile Modbus package RTU header file
agile_modbus_tcp.h | Agile Modbus package TCP header file
▼ src |
agile_modbus.c | Agile Modbus software package common source file
agile_modbus_rtu.c | Agile Modbus package RTU source file
agile_modbus_tcp.c | Agile Modbus package TCP source file
▼ util |
agile_modbus_slave_util.c | Agile Modbus software package provides simple slave access source files
agile_modbus_slave_util.h | The simple slave access header file provided by the Agile Modbus software package


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/examples Directory Reference

  • examples
examples Directory Reference


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc Directory Reference

  • inc
inc Directory Reference

Files

file | agile_modbus.h [code]
Agile Modbus software package common header file.
file | agile_modbus_rtu.h [code]
Agile Modbus package RTU header file.
file | agile_modbus_tcp.h [code]
Agile Modbus package TCP header file.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h File Reference

  • inc
Data Structures | Macros | Typedefs | Enumerations | Functions
agile_modbus.h File Reference

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

#include <stdint.h>
#include "agile_modbus_rtu.h"
#include "agile_modbus_tcp.h"
Include dependency graph for agile_modbus.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct | agile_modbus_sft
contains the modbus header parameter structure More...
struct | agile_modbus_backend
Agile Modbus backend interface structure. More...
struct | agile_modbus
Agile Modbus structure. More...
struct | agile_modbus_slave_info
Agile Modbus slave information structure. More...

Macros

#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_WRITE_AND_READ_REGISTERS 0x17
#define | AGILE_MODBUS_VERSION_STRING "AMB_1.1.3"
Agile Modbus version number. More...
#define | AGILE_MODBUS_BROADCAST_ADDRESS 0
Modbus broadcast address. More...
#define | AGILE_MODBUS_MAX_PDU_LENGTH 253
#define | AGILE_MODBUS_MAX_ADU_LENGTH 260
Quantity limit of Coils
   Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
   Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
   (chapter 6 section 11 page 29)
   Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
#define | AGILE_MODBUS_MAX_READ_BITS 2000
#define | AGILE_MODBUS_MAX_WRITE_BITS 1968
Quantity limit of Registers
   Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
   Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
   (chapter 6 section 12 page 31)
   Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
   (chapter 6 section 17 page 38)
   Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
#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

Typedefs

typedef struct agile_modbus_sft | agile_modbus_sft_t
contains the modbus header parameter structure More...
typedef struct agile_modbus | agile_modbus_t
Agile Modbus structure. More...
typedef struct agile_modbus_backend | agile_modbus_backend_t
Agile Modbus backend interface structure. More...
typedef int(* | agile_modbus_slave_callback_t) (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function. More...

Enumerations

enum {
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01 , AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS , AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE , AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE ,
AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE , AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY , AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE , AGILE_MODBUS_EXCEPTION_MEMORY_PARITY ,
AGILE_MODBUS_EXCEPTION_NOT_DEFINED , AGILE_MODBUS_EXCEPTION_GATEWAY_PATH , AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET , AGILE_MODBUS_EXCEPTION_UNKNOW = 0xff
}
Modbus exception code. More...
enum | agile_modbus_backend_type_t { AGILE_MODBUS_BACKEND_TYPE_RTU = 0 , AGILE_MODBUS_BACKEND_TYPE_TCP }
@ brief Modbus backend type More...
enum | agile_modbus_msg_type_t { AGILE_MODBUS_MSG_INDICATION , AGILE_MODBUS_MSG_CONFIRMATION }
Modbus received message type. More...

Functions

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...
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_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...
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...
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...

Detailed Description

Agile Modbus software package common header file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2022-07-28
Attention

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

Definition in file agile_modbus.h.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_rtu.h File Reference

  • inc
Data Structures | Macros | Typedefs | Functions
agile_modbus_rtu.h File Reference

Agile Modbus package RTU header file. More...

#include <stdint.h>
Include dependency graph for agile_modbus_rtu.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct agile_modbus_rtu
RTU structure. More...

Macros

#define | AGILE_MODBUS_RTU_HEADER_LENGTH 1
#define | AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6
#define | AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2
#define | AGILE_MODBUS_RTU_CHECKSUM_LENGTH 2
#define | AGILE_MODBUS_RTU_MAX_ADU_LENGTH 256

Typedefs

typedef struct agile_modbus_rtu agile_modbus_rtu_t
RTU structure. More...

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)
RTU initialization. More...

Detailed Description

Agile Modbus package RTU header file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2021-12-02
Attention

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

Definition in file agile_modbus_rtu.h.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_tcp.h File Reference

  • inc
Data Structures | Macros | Typedefs | Functions
agile_modbus_tcp.h File Reference

Agile Modbus package TCP header file. More...

#include <stdint.h>
Include dependency graph for agile_modbus_tcp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct agile_modbus_tcp
TCP structure. More...

Macros

#define | AGILE_MODBUS_TCP_HEADER_LENGTH 7
#define | AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12
#define | AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8
#define | AGILE_MODBUS_TCP_CHECKSUM_LENGTH 0
#define | AGILE_MODBUS_TCP_MAX_ADU_LENGTH 260

Typedefs

typedef struct agile_modbus_tcp agile_modbus_tcp_t
TCP structure. More...

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)
TCP initialization. More...

Detailed Description

Agile Modbus package TCP header file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2021-12-02
Attention

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

Definition in file agile_modbus_tcp.h.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src Directory Reference

  • src
src Directory Reference
Directory dependency graph for src:

Files

file | agile_modbus.c [code]
Agile Modbus software package common source file.
file | agile_modbus_rtu.c [code]
Agile Modbus package RTU source file.
file | agile_modbus_tcp.c [code]
Agile Modbus package TCP source file.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus.c File Reference

  • src
Macros | Functions
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_compute_response_length_from_request (agile_modbus_t *ctx, uint8_t *req)
Calculate the expected response data length. 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_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.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus_rtu.c File Reference

  • src
Functions | Variables
agile_modbus_rtu.c File Reference

Agile Modbus package RTU source file. More...

#include "agile_modbus.h"
#include "agile_modbus_rtu.h"
Include dependency graph for agile_modbus_rtu.c:

Go to the source code of this file.

Functions

static uint16_t | agile_modbus_rtu_crc16 (uint8_t *buffer, uint16_t buffer_length)
RTU CRC16 calculation. More...
static int | agile_modbus_rtu_set_slave (agile_modbus_t *ctx, int slave)
RTU sets the address interface. More...
static int | agile_modbus_rtu_build_request_basis (agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
RTU builds the basic request message interface (header message) More...
static int | agile_modbus_rtu_build_response_basis (agile_modbus_sft_t *sft, uint8_t *rsp)
RTU builds the basic response message interface (header message) More...
static int | agile_modbus_rtu_prepare_response_tid (const uint8_t *req, int *req_length)
RTU ready response interface. More...
static int | agile_modbus_rtu_send_msg_pre (uint8_t *req, int req_length)
RTU pre-send data interface. More...
static int | agile_modbus_rtu_check_integrity (agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
RTU check received data integrity interface (CRC16 comparison) More...
static int | agile_modbus_rtu_pre_check_confirmation (agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
RTU pre-check confirmation interface (request response address comparison) More...
int | agile_modbus_rtu_init (agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
RTU initialization. More...

Variables

static const uint8_t | _table_crc_hi []
Table of CRC values for high-order byte. More...
static const uint8_t | _table_crc_lo []
Table of CRC values for low-order byte. More...
static const agile_modbus_backend_t | agile_modbus_rtu_backend
RTU backend interface. More...

Detailed Description

Agile Modbus package RTU source file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2021-12-02
Attention

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

Definition in file agile_modbus_rtu.c.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus_tcp.c File Reference

  • src
Functions | Variables
agile_modbus_tcp.c File Reference

Agile Modbus package TCP source file. More...

#include "agile_modbus.h"
#include "agile_modbus_tcp.h"
Include dependency graph for agile_modbus_tcp.c:

Go to the source code of this file.

Functions

static int | agile_modbus_tcp_set_slave (agile_modbus_t *ctx, int slave)
TCP set address interface. More...
static int | agile_modbus_tcp_build_request_basis (agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
TCP builds the basic request message interface (header message) More...
static int | agile_modbus_tcp_build_response_basis (agile_modbus_sft_t *sft, uint8_t *rsp)
TCP builds a basic response message interface (header message) More...
static int | agile_modbus_tcp_prepare_response_tid (const uint8_t *req, int *req_length)
TCP ready response interface. More...
static int | agile_modbus_tcp_send_msg_pre (uint8_t *req, int req_length)
TCP pre-send data interface (calculate the value of the length field and store it) More...
static int | agile_modbus_tcp_check_integrity (agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
TCP check receiving data integrity interface. More...
static int | agile_modbus_tcp_pre_check_confirmation (agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
TCP pre-check confirmation interface (compare transaction identifier and protocol identifier) More...
int | agile_modbus_tcp_init (agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
TCP initialization. More...

Variables

static const agile_modbus_backend_t agile_modbus_tcp_backend
TCP backend interface. More...

Detailed Description

Agile Modbus package TCP source file.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2021-12-02
Attention

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

Definition in file agile_modbus_tcp.c.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util Directory Reference

  • util
util Directory Reference
Directory dependency graph for util:

Files

file | agile_modbus_slave_util.c [code]
Agile Modbus software package provides simple slave access source files.
file | agile_modbus_slave_util.h [code]
The simple slave access header file provided by the Agile Modbus software package.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.c File Reference

  • util
Functions
agile_modbus_slave_util.c File Reference

Agile Modbus software package provides simple slave access source files. More...

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

Go to the source code of this file.

Functions

static const agile_modbus_slave_util_map_t * | get_map_by_addr (const agile_modbus_slave_util_map_t *maps, int nb_maps, int address)
Get the mapping object from the mapping object array according to the register address. More...
static int | read_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
read register More...
static int | write_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
write register More...
static int | mask_write_register (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
mask write register More...
static int | write_read_registers (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
Write and read registers. More...
int | agile_modbus_slave_util_callback (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function. More...

Detailed Description

Agile Modbus software package provides simple slave access source files.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2022-07-28
Attention

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

Definition in file agile_modbus_slave_util.c.


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.h File Reference

  • util
Data Structures | Typedefs | Functions
agile_modbus_slave_util.h File Reference

The simple slave access header file provided by the Agile Modbus software package. More...

#include <stdint.h>
Include dependency graph for agile_modbus_slave_util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct | agile_modbus_slave_util_map
slave register mapping structure More...
struct | agile_modbus_slave_util
slave function structure More...

Typedefs

typedef struct agile_modbus_slave_util_map | agile_modbus_slave_util_map_t
slave register mapping structure More...
typedef struct agile_modbus_slave_util | agile_modbus_slave_util_t
slave function structure More...

Functions

int agile_modbus_slave_util_callback (agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function. More...

Detailed Description

The simple slave access header file provided by the Agile Modbus software package.

Author
Ma Longwei (25440.nosp@m.4721.nosp@m.3@qq..nosp@m.com)
Date
2022-07-28
Attention

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

Definition in file agile_modbus_slave_util.h.


Agile Modbus: Globals

Here is a list of all functions, variables, defines, enums, and typedefs with links to the files they belong to:

- _ -

  • _table_crc_hi : agile_modbus_rtu.c
  • _table_crc_lo : agile_modbus_rtu.c

- a -

  • agile_modbus_backend_t : agile_modbus.h
  • AGILE_MODBUS_BACKEND_TYPE_RTU : agile_modbus.h
  • agile_modbus_backend_type_t : agile_modbus.h
  • AGILE_MODBUS_BACKEND_TYPE_TCP : agile_modbus.h
  • AGILE_MODBUS_BROADCAST_ADDRESS : agile_modbus.h
  • agile_modbus_check_confirmation() : agile_modbus.c
  • agile_modbus_common_init() : agile_modbus.h
  • agile_modbus_compute_data_length_after_meta() : agile_modbus.c
  • agile_modbus_compute_meta_length_after_function() : agile_modbus.c
  • agile_modbus_compute_response_length_from_request() : agile_modbus.c
  • agile_modbus_deserialize_mask_write_register() : agile_modbus.h
  • agile_modbus_deserialize_raw_response() : agile_modbus.h
  • agile_modbus_deserialize_read_bits() : agile_modbus.h
  • agile_modbus_deserialize_read_input_bits() : agile_modbus.h
  • agile_modbus_deserialize_read_input_registers() : agile_modbus.h
  • agile_modbus_deserialize_read_registers() : agile_modbus.h
  • agile_modbus_deserialize_report_slave_id() : agile_modbus.h
  • agile_modbus_deserialize_write_and_read_registers() : agile_modbus.h
  • agile_modbus_deserialize_write_bit() : agile_modbus.h
  • agile_modbus_deserialize_write_bits() : agile_modbus.h
  • agile_modbus_deserialize_write_register() : agile_modbus.h
  • agile_modbus_deserialize_write_registers() : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_GATEWAY_PATH : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_MEMORY_PARITY : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_NOT_DEFINED : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_UNKNOW : agile_modbus.h
  • AGILE_MODBUS_FC_MASK_WRITE_REGISTER : agile_modbus.h
  • AGILE_MODBUS_FC_READ_COILS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_DISCRETE_INPUTS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_EXCEPTION_STATUS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_HOLDING_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_INPUT_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_REPORT_SLAVE_ID : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_SINGLE_COIL : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER : agile_modbus.h
  • AGILE_MODBUS_MAX_ADU_LENGTH : agile_modbus.h
  • AGILE_MODBUS_MAX_PDU_LENGTH : agile_modbus.h
  • AGILE_MODBUS_MAX_READ_BITS : agile_modbus.h
  • AGILE_MODBUS_MAX_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WR_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WR_WRITE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WRITE_BITS : agile_modbus.h
  • AGILE_MODBUS_MAX_WRITE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MSG_CONFIRMATION : agile_modbus.h
  • AGILE_MODBUS_MSG_INDICATION : agile_modbus.h
  • AGILE_MODBUS_MSG_LENGTH_UNDEFINED : agile_modbus.c
  • agile_modbus_msg_type_t : agile_modbus.h
  • agile_modbus_receive_judge() : agile_modbus.h
  • agile_modbus_receive_msg_judge() : agile_modbus.c
  • agile_modbus_rtu_backend : agile_modbus_rtu.c
  • agile_modbus_rtu_build_request_basis() : agile_modbus_rtu.c
  • agile_modbus_rtu_build_response_basis() : agile_modbus_rtu.c
  • agile_modbus_rtu_check_integrity() : agile_modbus_rtu.c
  • AGILE_MODBUS_RTU_CHECKSUM_LENGTH : agile_modbus_rtu.h
  • agile_modbus_rtu_crc16() : agile_modbus_rtu.c
  • AGILE_MODBUS_RTU_HEADER_LENGTH : agile_modbus_rtu.h
  • agile_modbus_rtu_init() : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_MAX_ADU_LENGTH : agile_modbus_rtu.h
  • agile_modbus_rtu_pre_check_confirmation() : agile_modbus_rtu.c
  • agile_modbus_rtu_prepare_response_tid() : agile_modbus_rtu.c
  • AGILE_MODBUS_RTU_PRESET_REQ_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_PRESET_RSP_LENGTH : agile_modbus_rtu.h
  • agile_modbus_rtu_send_msg_pre() : agile_modbus_rtu.c
  • agile_modbus_rtu_set_slave() : agile_modbus_rtu.c
  • agile_modbus_rtu_t : agile_modbus_rtu.h
  • agile_modbus_serialize_mask_write_register() : agile_modbus.h
  • agile_modbus_serialize_raw_request() : agile_modbus.h
  • agile_modbus_serialize_read_bits() : agile_modbus.h
  • agile_modbus_serialize_read_input_bits() : agile_modbus.h
  • agile_modbus_serialize_read_input_registers() : agile_modbus.h
  • agile_modbus_serialize_read_registers() : agile_modbus.h
  • agile_modbus_serialize_report_slave_id() : agile_modbus.h
  • agile_modbus_serialize_response_exception() : agile_modbus.c
  • agile_modbus_serialize_write_and_read_registers() : agile_modbus.h
  • agile_modbus_serialize_write_bit() : agile_modbus.h
  • agile_modbus_serialize_write_bits() : agile_modbus.h
  • agile_modbus_serialize_write_register() : agile_modbus.h
  • agile_modbus_serialize_write_registers() : agile_modbus.h
  • agile_modbus_set_compute_data_length_after_meta_cb() : agile_modbus.h
  • agile_modbus_set_compute_meta_length_after_function_cb() : agile_modbus.h
  • agile_modbus_set_slave() : agile_modbus.h
  • agile_modbus_sft_t : agile_modbus.h
  • agile_modbus_slave_callback_t : agile_modbus.h
  • agile_modbus_slave_handle() : agile_modbus.h
  • agile_modbus_slave_io_get() : agile_modbus.h
  • agile_modbus_slave_io_set() : agile_modbus.h
  • agile_modbus_slave_register_get() : agile_modbus.h
  • agile_modbus_slave_register_set() : agile_modbus.h
  • agile_modbus_slave_util_callback() : agile_modbus_slave_util.c
  • agile_modbus_slave_util_map_t : agile_modbus_slave_util.h
  • agile_modbus_slave_util_t : agile_modbus_slave_util.h
  • agile_modbus_t : agile_modbus.h
  • agile_modbus_tcp_backend : agile_modbus_tcp.c
  • agile_modbus_tcp_build_request_basis() : agile_modbus_tcp.c
  • agile_modbus_tcp_build_response_basis() : agile_modbus_tcp.c
  • agile_modbus_tcp_check_integrity() : agile_modbus_tcp.c
  • AGILE_MODBUS_TCP_CHECKSUM_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_HEADER_LENGTH : agile_modbus_tcp.h
  • agile_modbus_tcp_init() : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_MAX_ADU_LENGTH : agile_modbus_tcp.h
  • agile_modbus_tcp_pre_check_confirmation() : agile_modbus_tcp.c
  • agile_modbus_tcp_prepare_response_tid() : agile_modbus_tcp.c
  • AGILE_MODBUS_TCP_PRESET_REQ_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_PRESET_RSP_LENGTH : agile_modbus_tcp.h
  • agile_modbus_tcp_send_msg_pre() : agile_modbus_tcp.c
  • agile_modbus_tcp_set_slave() : agile_modbus_tcp.c
  • agile_modbus_tcp_t : agile_modbus_tcp.h
  • AGILE_MODBUS_VERSION_STRING : agile_modbus.h

- g -

  • get_map_by_addr() : agile_modbus_slave_util.c

- m -

  • mask_write_register() : agile_modbus_slave_util.c

- r -

  • read_registers() : agile_modbus_slave_util.c

- w -

  • write_read_registers() : agile_modbus_slave_util.c
  • write_registers() : agile_modbus_slave_util.c


Agile Modbus: Globals

- a -

  • agile_modbus_check_confirmation() : agile_modbus.c
  • agile_modbus_common_init() : agile_modbus.h
  • agile_modbus_compute_data_length_after_meta() : agile_modbus.c
  • agile_modbus_compute_meta_length_after_function() : agile_modbus.c
  • agile_modbus_compute_response_length_from_request() : agile_modbus.c
  • agile_modbus_deserialize_mask_write_register() : agile_modbus.h
  • agile_modbus_deserialize_raw_response() : agile_modbus.h
  • agile_modbus_deserialize_read_bits() : agile_modbus.h
  • agile_modbus_deserialize_read_input_bits() : agile_modbus.h
  • agile_modbus_deserialize_read_input_registers() : agile_modbus.h
  • agile_modbus_deserialize_read_registers() : agile_modbus.h
  • agile_modbus_deserialize_report_slave_id() : agile_modbus.h
  • agile_modbus_deserialize_write_and_read_registers() : agile_modbus.h
  • agile_modbus_deserialize_write_bit() : agile_modbus.h
  • agile_modbus_deserialize_write_bits() : agile_modbus.h
  • agile_modbus_deserialize_write_register() : agile_modbus.h
  • agile_modbus_deserialize_write_registers() : agile_modbus.h
  • agile_modbus_receive_judge() : agile_modbus.h
  • agile_modbus_receive_msg_judge() : agile_modbus.c
  • agile_modbus_rtu_build_request_basis() : agile_modbus_rtu.c
  • agile_modbus_rtu_build_response_basis() : agile_modbus_rtu.c
  • agile_modbus_rtu_check_integrity() : agile_modbus_rtu.c
  • agile_modbus_rtu_crc16() : agile_modbus_rtu.c
  • agile_modbus_rtu_init() : agile_modbus_rtu.h
  • agile_modbus_rtu_pre_check_confirmation() : agile_modbus_rtu.c
  • agile_modbus_rtu_prepare_response_tid() : agile_modbus_rtu.c
  • agile_modbus_rtu_send_msg_pre() : agile_modbus_rtu.c
  • agile_modbus_rtu_set_slave() : agile_modbus_rtu.c
  • agile_modbus_serialize_mask_write_register() : agile_modbus.h
  • agile_modbus_serialize_raw_request() : agile_modbus.h
  • agile_modbus_serialize_read_bits() : agile_modbus.h
  • agile_modbus_serialize_read_input_bits() : agile_modbus.h
  • agile_modbus_serialize_read_input_registers() : agile_modbus.h
  • agile_modbus_serialize_read_registers() : agile_modbus.h
  • agile_modbus_serialize_report_slave_id() : agile_modbus.h
  • agile_modbus_serialize_response_exception() : agile_modbus.c
  • agile_modbus_serialize_write_and_read_registers() : agile_modbus.h
  • agile_modbus_serialize_write_bit() : agile_modbus.h
  • agile_modbus_serialize_write_bits() : agile_modbus.h
  • agile_modbus_serialize_write_register() : agile_modbus.h
  • agile_modbus_serialize_write_registers() : agile_modbus.h
  • agile_modbus_set_compute_data_length_after_meta_cb() : agile_modbus.h
  • agile_modbus_set_compute_meta_length_after_function_cb() : agile_modbus.h
  • agile_modbus_set_slave() : agile_modbus.h
  • agile_modbus_slave_handle() : agile_modbus.h
  • agile_modbus_slave_io_get() : agile_modbus.h
  • agile_modbus_slave_io_set() : agile_modbus.h
  • agile_modbus_slave_register_get() : agile_modbus.h
  • agile_modbus_slave_register_set() : agile_modbus.h
  • agile_modbus_slave_util_callback() : agile_modbus_slave_util.c
  • agile_modbus_tcp_build_request_basis() : agile_modbus_tcp.c
  • agile_modbus_tcp_build_response_basis() : agile_modbus_tcp.c
  • agile_modbus_tcp_check_integrity() : agile_modbus_tcp.c
  • agile_modbus_tcp_init() : agile_modbus_tcp.h
  • agile_modbus_tcp_pre_check_confirmation() : agile_modbus_tcp.c
  • agile_modbus_tcp_prepare_response_tid() : agile_modbus_tcp.c
  • agile_modbus_tcp_send_msg_pre() : agile_modbus_tcp.c
  • agile_modbus_tcp_set_slave() : agile_modbus_tcp.c

- g -

  • get_map_by_addr() : agile_modbus_slave_util.c

- m -

  • mask_write_register() : agile_modbus_slave_util.c

- r -

  • read_registers() : agile_modbus_slave_util.c

- w -

  • write_read_registers() : agile_modbus_slave_util.c
  • write_registers() : agile_modbus_slave_util.c


Agile Modbus: Globals

  • _table_crc_hi : agile_modbus_rtu.c
  • _table_crc_lo : agile_modbus_rtu.c
  • agile_modbus_rtu_backend : agile_modbus_rtu.c
  • agile_modbus_tcp_backend : agile_modbus_tcp.c


Agile Modbus: Globals

  • agile_modbus_backend_t : agile_modbus.h
  • agile_modbus_rtu_t : agile_modbus_rtu.h
  • agile_modbus_sft_t : agile_modbus.h
  • agile_modbus_slave_callback_t : agile_modbus.h
  • agile_modbus_slave_util_map_t : agile_modbus_slave_util.h
  • agile_modbus_slave_util_t : agile_modbus_slave_util.h
  • agile_modbus_t : agile_modbus.h
  • agile_modbus_tcp_t : agile_modbus_tcp.h


Agile Modbus: Globals

  • agile_modbus_backend_type_t : agile_modbus.h
  • agile_modbus_msg_type_t : agile_modbus.h


Agile Modbus: Globals

  • AGILE_MODBUS_BACKEND_TYPE_RTU : agile_modbus.h
  • AGILE_MODBUS_BACKEND_TYPE_TCP : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_GATEWAY_PATH : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_MEMORY_PARITY : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_NOT_DEFINED : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE : agile_modbus.h
  • AGILE_MODBUS_EXCEPTION_UNKNOW : agile_modbus.h
  • AGILE_MODBUS_MSG_CONFIRMATION : agile_modbus.h
  • AGILE_MODBUS_MSG_INDICATION : agile_modbus.h


Agile Modbus: Globals

- a -

  • AGILE_MODBUS_BROADCAST_ADDRESS : agile_modbus.h
  • AGILE_MODBUS_FC_MASK_WRITE_REGISTER : agile_modbus.h
  • AGILE_MODBUS_FC_READ_COILS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_DISCRETE_INPUTS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_EXCEPTION_STATUS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_HOLDING_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_READ_INPUT_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_REPORT_SLAVE_ID : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_SINGLE_COIL : agile_modbus.h
  • AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER : agile_modbus.h
  • AGILE_MODBUS_MAX_ADU_LENGTH : agile_modbus.h
  • AGILE_MODBUS_MAX_PDU_LENGTH : agile_modbus.h
  • AGILE_MODBUS_MAX_READ_BITS : agile_modbus.h
  • AGILE_MODBUS_MAX_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WR_READ_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WR_WRITE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MAX_WRITE_BITS : agile_modbus.h
  • AGILE_MODBUS_MAX_WRITE_REGISTERS : agile_modbus.h
  • AGILE_MODBUS_MSG_LENGTH_UNDEFINED : agile_modbus.c
  • AGILE_MODBUS_RTU_CHECKSUM_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_HEADER_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_MAX_ADU_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_PRESET_REQ_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_RTU_PRESET_RSP_LENGTH : agile_modbus_rtu.h
  • AGILE_MODBUS_TCP_CHECKSUM_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_HEADER_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_MAX_ADU_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_PRESET_REQ_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_TCP_PRESET_RSP_LENGTH : agile_modbus_tcp.h
  • AGILE_MODBUS_VERSION_STRING : agile_modbus.h


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/ChangeLog.md File Reference

C:/Users/25440/Desktop/agile_modbus/ChangeLog.md File Reference


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/README.md File Reference

C:/Users/25440/Desktop/agile_modbus/README.md File Reference


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus.c Source File

  • src
agile_modbus.c
Go to the documentation of this file.
1
39#include "agile_modbus.h"
40#include <string.h>
41
49#define AGILE_MODBUS_MSG_LENGTH_UNDEFINED -1
88static uint8_t agile_modbus_compute_meta_length_after_function(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
89{
90 int length;
91
92 if (msg_type == AGILE_MODBUS_MSG_INDICATION) {
93 if (function <= AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER) {
94 length = 4;
95 } else if (function == AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS ||
96 function == AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS) {
97 length = 5;
98 } else if (function == AGILE_MODBUS_FC_MASK_WRITE_REGISTER) {
99 length = 6;
100 } else if (function == AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS) {
101 length = 9;
102 } else {
103 /* MODBUS_FC_READ_EXCEPTION_STATUS, MODBUS_FC_REPORT_SLAVE_ID */
104 length = 0;
105 if (ctx->compute_meta_length_after_function)
106 length = ctx->compute_meta_length_after_function(ctx, function, msg_type);
107 }
108 } else {
109 /* MSG_CONFIRMATION */
110 switch (function) {
111 case AGILE_MODBUS_FC_READ_COILS:
112 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS:
113 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
114 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS:
115 case AGILE_MODBUS_FC_REPORT_SLAVE_ID:
116 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS:
117 length = 1;
118 break;
119
120 case AGILE_MODBUS_FC_WRITE_SINGLE_COIL:
121 case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER:
122 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS:
123 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
124 length = 4;
125 break;
126
127 case AGILE_MODBUS_FC_MASK_WRITE_REGISTER:
128 length = 6;
129 break;
130
131 default:
132 length = 1;
133 if (ctx->compute_meta_length_after_function)
134 length = ctx->compute_meta_length_after_function(ctx, function, msg_type);
135 }
136 }
137
138 return length;
139}
140
177static 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)
178{
179 int function = msg[ctx->backend->header_length];
180 int length;
181
182 if (msg_type == AGILE_MODBUS_MSG_INDICATION) {
183 switch (function) {
184 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS:
185 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
186 length = msg[ctx->backend->header_length + 5];
187 break;
188
189 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS:
190 length = msg[ctx->backend->header_length + 9];
191 break;
192
193 default:
194 length = 0;
195 if (ctx->compute_data_length_after_meta)
196 length = ctx->compute_data_length_after_meta(ctx, msg, msg_length, msg_type);
197 }
198 } else {
199 /* MSG_CONFIRMATION */
200 if (function <= AGILE_MODBUS_FC_READ_INPUT_REGISTERS ||
201 function == AGILE_MODBUS_FC_REPORT_SLAVE_ID ||
202 function == AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS) {
203 length = msg[ctx->backend->header_length + 1];
204 } else {
205 length = 0;
206 if (ctx->compute_data_length_after_meta)
207 length = ctx->compute_data_length_after_meta(ctx, msg, msg_length, msg_type);
208 }
209 }
210
211 length += ctx->backend->checksum_length;
212
213 return length;
214}
215
224static int agile_modbus_receive_msg_judge(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
225{
226 int remain_len = msg_length;
227
228 remain_len -= (ctx->backend->header_length + 1);
229 if (remain_len < 0)
230 return -1;
231 remain_len -= agile_modbus_compute_meta_length_after_function(ctx, msg[ctx->backend->header_length], msg_type);
232 if (remain_len < 0)
233 return -1;
234 remain_len -= agile_modbus_compute_data_length_after_meta(ctx, msg, msg_length, msg_type);
235 if (remain_len < 0)
236 return -1;
237
238 return ctx->backend->check_integrity(ctx, msg, msg_length - remain_len);
239}
240
257void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
258{
259 memset(ctx, 0, sizeof(agile_modbus_t));
260 ctx->slave = -1;
261 ctx->send_buf = send_buf;
262 ctx->send_bufsz = send_bufsz;
263 ctx->read_buf = read_buf;
264 ctx->read_bufsz = read_bufsz;
265}
266
273int agile_modbus_set_slave(agile_modbus_t *ctx, int slave)
274{
275 return ctx->backend->set_slave(ctx, slave);
276}
277
284void agile_modbus_set_compute_meta_length_after_function_cb(agile_modbus_t *ctx,
285 uint8_t (*cb)(agile_modbus_t *ctx, int function,
286 agile_modbus_msg_type_t msg_type))
287{
288 ctx->compute_meta_length_after_function = cb;
289}
290
297void agile_modbus_set_compute_data_length_after_meta_cb(agile_modbus_t *ctx,
298 int (*cb)(agile_modbus_t *ctx, uint8_t *msg,
299 int msg_length, agile_modbus_msg_type_t msg_type))
300{
301 ctx->compute_data_length_after_meta = cb;
302}
303
312int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type)
313{
314 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
315 return -1;
316
317 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, msg_type);
318
319 return rc;
320}
321
342static int agile_modbus_compute_response_length_from_request(agile_modbus_t *ctx, uint8_t *req)
343{
344 int length;
345 const int offset = ctx->backend->header_length;
346
347 switch (req[offset]) {
348 case AGILE_MODBUS_FC_READ_COILS:
349 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: {
350 /* Header + nb values (code from write_bits) */
351 int nb = (req[offset + 3] << 8) | req[offset + 4];
352 length = 2 + (nb / 8) + ((nb % 8) ? 1 : 0);
353 } break;
354
355 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS:
356 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
357 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS:
358 /* Header + 2 * nb values */
359 length = 2 + 2 * (req[offset + 3] << 8 | req[offset + 4]);
360 break;
361
362 case AGILE_MODBUS_FC_WRITE_SINGLE_COIL:
363 case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER:
364 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS:
365 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
366 length = 5;
367 break;
368
369 case AGILE_MODBUS_FC_MASK_WRITE_REGISTER:
370 length = 7;
371 break;
372
373 default:
374 /* The response is device specific (the header provides the
375 length) */
376 return AGILE_MODBUS_MSG_LENGTH_UNDEFINED;
377 }
378
379 return offset + length + ctx->backend->checksum_length;
380}
381
391static int agile_modbus_check_confirmation(agile_modbus_t *ctx, uint8_t *req,
392 uint8_t *rsp, int rsp_length)
393{
394 int rc;
395 int rsp_length_computed;
396 const int offset = ctx->backend->header_length;
397 const int function = rsp[offset];
398
399 if (ctx->backend->pre_check_confirmation) {
400 rc = ctx->backend->pre_check_confirmation(ctx, req, rsp, rsp_length);
401 if (rc < 0)
402 return -1;
403 }
404
405 rsp_length_computed = agile_modbus_compute_response_length_from_request(ctx, req);
406
407 /* Exception code */
408 if (function >= 0x80) {
409 if (rsp_length == (offset + 2 + (int)ctx->backend->checksum_length) && req[offset] == (rsp[offset] - 0x80))
410 return (-128 - rsp[offset + 1]);
411 else
412 return -1;
413 }
414
415 /* Check length */
416 if ((rsp_length == rsp_length_computed || rsp_length_computed == AGILE_MODBUS_MSG_LENGTH_UNDEFINED) && function < 0x80) {
417 int req_nb_value;
418 int rsp_nb_value;
419
420 /* Check function code */
421 if (function != req[offset])
422 return -1;
423
424 /* Check the number of values is corresponding to the request */
425 switch (function) {
426 case AGILE_MODBUS_FC_READ_COILS:
427 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS:
428 /* Read functions, 8 values in a byte (nb
429 * of values in the request and byte count in
430 * the response. */
431 req_nb_value = (req[offset + 3] << 8) + req[offset + 4];
432 req_nb_value = (req_nb_value / 8) + ((req_nb_value % 8) ? 1 : 0);
433 rsp_nb_value = rsp[offset + 1];
434 break;
435
436 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS:
437 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
438 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS:
439 /* Read functions 1 value = 2 bytes */
440 req_nb_value = (req[offset + 3] << 8) + req[offset + 4];
441 rsp_nb_value = (rsp[offset + 1] / 2);
442 break;
443
444 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS:
445 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
446 /* N Write functions */
447 req_nb_value = (req[offset + 3] << 8) + req[offset + 4];
448 rsp_nb_value = (rsp[offset + 3] << 8) | rsp[offset + 4];
449 break;
450
451 case AGILE_MODBUS_FC_REPORT_SLAVE_ID:
452 /* Report slave ID (bytes received) */
453 req_nb_value = rsp_nb_value = rsp[offset + 1];
454 break;
455
456 default:
457 /* 1 Write functions & others */
458 req_nb_value = rsp_nb_value = 1;
459 }
460
461 if (req_nb_value == rsp_nb_value)
462 rc = rsp_nb_value;
463 else
464 rc = -1;
465 } else
466 rc = -1;
467
468 return rc;
469}
470
493int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb)
494{
495 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
496 if (ctx->send_bufsz < min_req_length)
497 return -1;
498
499 if (nb > AGILE_MODBUS_MAX_READ_BITS)
500 return -1;
501
502 int req_length = 0;
503 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_COILS, addr, nb, ctx->send_buf);
504 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
505
506 return req_length;
507}
508
509int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
510{
511 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
512 if (ctx->send_bufsz < min_req_length)
513 return -1;
514 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
515 return -1;
516
517 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
518 if (rc < 0)
519 return -1;
520
521 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
522 if (rc < 0)
523 return rc;
524
525 int i, temp, bit;
526 int pos = 0;
527 int offset;
528 int offset_end;
529 int nb;
530
531 offset = ctx->backend->header_length + 2;
532 offset_end = offset + rc;
533 nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + ctx->send_buf[ctx->backend->header_length + 4];
534
535 for (i = offset; i < offset_end; i++) {
536 /* Shift reg hi_byte to temp */
537 temp = ctx->read_buf[i];
538
539 for (bit = 0x01; (bit & 0xff) && (pos < nb);) {
540 dest[pos++] = (temp & bit) ? 1 : 0;
541 bit = bit << 1;
542 }
543 }
544
545 return nb;
546}
547
548int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb)
549{
550 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
551 if (ctx->send_bufsz < min_req_length)
552 return -1;
553
554 if (nb > AGILE_MODBUS_MAX_READ_BITS)
555 return -1;
556
557 int req_length = 0;
558 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_DISCRETE_INPUTS, addr, nb, ctx->send_buf);
559 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
560
561 return req_length;
562}
563
564int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
565{
566 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
567 if (ctx->send_bufsz < min_req_length)
568 return -1;
569 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
570 return -1;
571
572 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
573 if (rc < 0)
574 return -1;
575
576 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
577 if (rc < 0)
578 return rc;
579
580 int i, temp, bit;
581 int pos = 0;
582 int offset;
583 int offset_end;
584 int nb;
585
586 offset = ctx->backend->header_length + 2;
587 offset_end = offset + rc;
588 nb = (ctx->send_buf[ctx->backend->header_length + 3] << 8) + ctx->send_buf[ctx->backend->header_length + 4];
589
590 for (i = offset; i < offset_end; i++) {
591 /* Shift reg hi_byte to temp */
592 temp = ctx->read_buf[i];
593
594 for (bit = 0x01; (bit & 0xff) && (pos < nb);) {
595 dest[pos++] = (temp & bit) ? 1 : 0;
596 bit = bit << 1;
597 }
598 }
599
600 return nb;
601}
602
603int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb)
604{
605 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
606 if (ctx->send_bufsz < min_req_length)
607 return -1;
608
609 if (nb > AGILE_MODBUS_MAX_READ_REGISTERS)
610 return -1;
611
612 int req_length = 0;
613 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_HOLDING_REGISTERS, addr, nb, ctx->send_buf);
614 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
615
616 return req_length;
617}
618
619int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
620{
621 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
622 if (ctx->send_bufsz < min_req_length)
623 return -1;
624 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
625 return -1;
626
627 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
628 if (rc < 0)
629 return -1;
630
631 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
632 if (rc < 0)
633 return rc;
634
635 int offset;
636 int i;
637
638 offset = ctx->backend->header_length;
639 for (i = 0; i < rc; i++) {
640 /* shift reg hi_byte to temp OR with lo_byte */
641 dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)];
642 }
643
644 return rc;
645}
646
647int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb)
648{
649 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
650 if (ctx->send_bufsz < min_req_length)
651 return -1;
652
653 if (nb > AGILE_MODBUS_MAX_READ_REGISTERS)
654 return -1;
655
656 int req_length = 0;
657 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_READ_INPUT_REGISTERS, addr, nb, ctx->send_buf);
658 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
659
660 return req_length;
661}
662
663int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
664{
665 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
666 if (ctx->send_bufsz < min_req_length)
667 return -1;
668 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
669 return -1;
670
671 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
672 if (rc < 0)
673 return -1;
674
675 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
676 if (rc < 0)
677 return rc;
678
679 int offset;
680 int i;
681
682 offset = ctx->backend->header_length;
683 for (i = 0; i < rc; i++) {
684 /* shift reg hi_byte to temp OR with lo_byte */
685 dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)];
686 }
687
688 return rc;
689}
690
691int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status)
692{
693 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
694 if (ctx->send_bufsz < min_req_length)
695 return -1;
696
697 int req_length = 0;
698 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_SINGLE_COIL, addr, status ? 0xFF00 : 0, ctx->send_buf);
699 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
700
701 return req_length;
702}
703
704int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length)
705{
706 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
707 if (ctx->send_bufsz < min_req_length)
708 return -1;
709 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
710 return -1;
711
712 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
713 if (rc < 0)
714 return -1;
715
716 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
717
718 return rc;
719}
720
721int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value)
722{
723 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
724 if (ctx->send_bufsz < min_req_length)
725 return -1;
726
727 int req_length = 0;
728 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER, addr, (int)value, ctx->send_buf);
729 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
730
731 return req_length;
732}
733
734int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length)
735{
736 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
737 if (ctx->send_bufsz < min_req_length)
738 return -1;
739 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
740 return -1;
741
742 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
743 if (rc < 0)
744 return -1;
745
746 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
747
748 return rc;
749}
750
751int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src)
752{
753 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
754 if (ctx->send_bufsz < min_req_length)
755 return -1;
756
757 if (nb > AGILE_MODBUS_MAX_WRITE_BITS)
758 return -1;
759
760 int i;
761 int byte_count;
762 int req_length;
763 int bit_check = 0;
764 int pos = 0;
765
766 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS, addr, nb, ctx->send_buf);
767 byte_count = (nb / 8) + ((nb % 8) ? 1 : 0);
768
769 min_req_length += (1 + byte_count);
770 if (ctx->send_bufsz < min_req_length)
771 return -1;
772
773 ctx->send_buf[req_length++] = byte_count;
774 for (i = 0; i < byte_count; i++) {
775 int bit;
776
777 bit = 0x01;
778 ctx->send_buf[req_length] = 0;
779
780 while ((bit & 0xFF) && (bit_check++ < nb)) {
781 if (src[pos++])
782 ctx->send_buf[req_length] |= bit;
783 else
784 ctx->send_buf[req_length] &= ~bit;
785
786 bit = bit << 1;
787 }
788 req_length++;
789 }
790
791 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
792
793 return req_length;
794}
795
796int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length)
797{
798 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
799 if (ctx->send_bufsz < min_req_length)
800 return -1;
801 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
802 return -1;
803
804 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
805 if (rc < 0)
806 return -1;
807
808 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
809
810 return rc;
811}
812
813int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src)
814{
815 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
816 if (ctx->send_bufsz < min_req_length)
817 return -1;
818
819 if (nb > AGILE_MODBUS_MAX_WRITE_REGISTERS)
820 return -1;
821
822 int i;
823 int req_length;
824 int byte_count;
825
826 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS, addr, nb, ctx->send_buf);
827 byte_count = nb * 2;
828
829 min_req_length += (1 + byte_count);
830 if (ctx->send_bufsz < min_req_length)
831 return -1;
832
833 ctx->send_buf[req_length++] = byte_count;
834 for (i = 0; i < nb; i++) {
835 ctx->send_buf[req_length++] = src[i] >> 8;
836 ctx->send_buf[req_length++] = src[i] & 0x00FF;
837 }
838
839 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
840
841 return req_length;
842}
843
844int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length)
845{
846 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
847 if (ctx->send_bufsz < min_req_length)
848 return -1;
849 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
850 return -1;
851
852 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
853 if (rc < 0)
854 return -1;
855
856 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
857
858 return rc;
859}
860
861int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask)
862{
863 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length + 2;
864 if (ctx->send_bufsz < min_req_length)
865 return -1;
866
867 int req_length = 0;
868 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_MASK_WRITE_REGISTER, addr, 0, ctx->send_buf);
869
870 /* HACKISH, count is not used */
871 req_length -= 2;
872
873 ctx->send_buf[req_length++] = and_mask >> 8;
874 ctx->send_buf[req_length++] = and_mask & 0x00ff;
875 ctx->send_buf[req_length++] = or_mask >> 8;
876 ctx->send_buf[req_length++] = or_mask & 0x00ff;
877
878 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
879
880 return req_length;
881}
882
883int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length)
884{
885 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
886 if (ctx->send_bufsz < min_req_length)
887 return -1;
888 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
889 return -1;
890
891 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
892 if (rc < 0)
893 return -1;
894
895 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
896
897 return rc;
898}
899
900int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx,
901 int write_addr, int write_nb,
902 const uint16_t *src,
903 int read_addr, int read_nb)
904{
905 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
906 if (ctx->send_bufsz < min_req_length)
907 return -1;
908
909 if (write_nb > AGILE_MODBUS_MAX_WR_WRITE_REGISTERS)
910 return -1;
911
912 if (read_nb > AGILE_MODBUS_MAX_WR_READ_REGISTERS)
913 return -1;
914
915 int req_length;
916 int i;
917 int byte_count;
918
919 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS, read_addr, read_nb, ctx->send_buf);
920 byte_count = write_nb * 2;
921
922 min_req_length += (5 + byte_count);
923 if (ctx->send_bufsz < min_req_length)
924 return -1;
925
926 ctx->send_buf[req_length++] = write_addr >> 8;
927 ctx->send_buf[req_length++] = write_addr & 0x00ff;
928 ctx->send_buf[req_length++] = write_nb >> 8;
929 ctx->send_buf[req_length++] = write_nb & 0x00ff;
930 ctx->send_buf[req_length++] = byte_count;
931 for (i = 0; i < write_nb; i++) {
932 ctx->send_buf[req_length++] = src[i] >> 8;
933 ctx->send_buf[req_length++] = src[i] & 0x00FF;
934 }
935
936 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
937
938 return req_length;
939}
940
941int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
942{
943 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
944 if (ctx->send_bufsz < min_req_length)
945 return -1;
946 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
947 return -1;
948
949 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
950 if (rc < 0)
951 return -1;
952
953 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
954 if (rc < 0)
955 return rc;
956
957 int offset;
958 int i;
959
960 offset = ctx->backend->header_length;
961 for (i = 0; i < rc; i++) {
962 /* shift reg hi_byte to temp OR with lo_byte */
963 dest[i] = (ctx->read_buf[offset + 2 + (i << 1)] << 8) | ctx->read_buf[offset + 3 + (i << 1)];
964 }
965
966 return rc;
967}
968
969int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx)
970{
971 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
972 if (ctx->send_bufsz < min_req_length)
973 return -1;
974
975 int req_length = 0;
976 req_length = ctx->backend->build_request_basis(ctx, AGILE_MODBUS_FC_REPORT_SLAVE_ID, 0, 0, ctx->send_buf);
977 /* HACKISH, addr and count are not used */
978 req_length -= 4;
979 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
980
981 return req_length;
982}
983
984int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest)
985{
986 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
987 if (ctx->send_bufsz < min_req_length)
988 return -1;
989 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
990 return -1;
991 if (max_dest <= 0)
992 return -1;
993
994 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
995 if (rc < 0)
996 return -1;
997
998 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
999 if (rc < 0)
1000 return rc;
1001
1002 int i;
1003 int offset;
1004
1005 offset = ctx->backend->header_length + 2;
1006
1007 /* Byte count, slave id, run indicator status and
1008 additional data. Truncate copy to max_dest. */
1009 for (i = 0; i < rc && i < max_dest; i++) {
1010 dest[i] = ctx->read_buf[offset + i];
1011 }
1012
1013 return rc;
1014}
1015
1031int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length)
1032{
1033 if (raw_req_length < 2) {
1034 /* The raw request must contain function and slave at least and
1035 must not be longer than the maximum pdu length plus the slave
1036 address. */
1037
1038 return -1;
1039 }
1040
1041 int min_req_length = ctx->backend->header_length + 1 + ctx->backend->checksum_length + raw_req_length - 2;
1042 if (ctx->send_bufsz < min_req_length)
1043 return -1;
1044
1045 agile_modbus_sft_t sft;
1046 int req_length;
1047
1048 sft.slave = raw_req[0];
1049 sft.function = raw_req[1];
1050 /* The t_id is left to zero */
1051 sft.t_id = 0;
1052 /* This response function only set the header so it's convenient here */
1053 req_length = ctx->backend->build_response_basis(&sft, ctx->send_buf);
1054
1055 if (raw_req_length > 2) {
1056 /* Copy data after function code */
1057 memcpy(ctx->send_buf + req_length, raw_req + 2, raw_req_length - 2);
1058 req_length += raw_req_length - 2;
1059 }
1060
1061 req_length = ctx->backend->send_msg_pre(ctx->send_buf, req_length);
1062
1063 return req_length;
1064}
1065
1073int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length)
1074{
1075 int min_req_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
1076 if (ctx->send_bufsz < min_req_length)
1077 return -1;
1078 if ((msg_length <= 0) || (msg_length > ctx->read_bufsz))
1079 return -1;
1080
1081 int rc = agile_modbus_receive_msg_judge(ctx, ctx->read_buf, msg_length, AGILE_MODBUS_MSG_CONFIRMATION);
1082 if (rc < 0)
1083 return -1;
1084
1085 rc = agile_modbus_check_confirmation(ctx, ctx->send_buf, ctx->read_buf, rc);
1086
1087 return rc;
1088}
1089
1113static int agile_modbus_serialize_response_exception(agile_modbus_t *ctx, agile_modbus_sft_t *sft, int exception_code)
1114{
1115 int rsp_length;
1116
1117 /* Build exception response */
1118 sft->function = sft->function + 0x80;
1119 rsp_length = ctx->backend->build_response_basis(sft, ctx->send_buf);
1120 ctx->send_buf[rsp_length++] = exception_code;
1121
1122 return rsp_length;
1123}
1124
1139void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
1140{
1141 int offset = index / 8;
1142 int shift = index % 8;
1143
1144 if (status)
1145 buf[offset] |= (0x01 << shift);
1146 else
1147 buf[offset] &= ~(0x01 << shift);
1148}
1149
1156uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
1157{
1158 int offset = index / 8;
1159 int shift = index % 8;
1160
1161 uint8_t status = (buf[offset] & (0x01 << shift)) ? 1 : 0;
1162
1163 return status;
1164}
1165
1172void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
1173{
1174 buf[index * 2] = data >> 8;
1175 buf[index * 2 + 1] = data & 0xFF;
1176}
1177
1184uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
1185{
1186 uint16_t data = (buf[index * 2] << 8) + buf[index * 2 + 1];
1187
1188 return data;
1189}
1190
1203int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict,
1204 agile_modbus_slave_callback_t slave_cb, const void *slave_data, int *frame_length)
1205{
1206 int min_rsp_length = ctx->backend->header_length + 5 + ctx->backend->checksum_length;
1207 if (ctx->send_bufsz < min_rsp_length)
1208 return -1;
1209
1210 int req_length = agile_modbus_receive_judge(ctx, msg_length, AGILE_MODBUS_MSG_INDICATION);
1211 if (req_length < 0)
1212 return -1;
1213 if (frame_length)
1214 *frame_length = req_length;
1215
1216 int offset;
1217 int slave;
1218 int function;
1219 uint16_t address;
1220 int rsp_length = 0;
1221 int exception_code = 0;
1222 int reg_data = 0;
1223 agile_modbus_sft_t sft;
1224 uint8_t *req = ctx->read_buf;
1225 uint8_t *rsp = ctx->send_buf;
1226
1227 memset(rsp, 0, ctx->send_bufsz);
1228 offset = ctx->backend->header_length;
1229 slave = req[offset - 1];
1230 function = req[offset];
1231 address = (req[offset + 1] << 8) + req[offset + 2];
1232
1233 sft.slave = slave;
1234 sft.function = function;
1235 sft.t_id = ctx->backend->prepare_response_tid(req, &req_length);
1236
1237 struct agile_modbus_slave_info slave_info = {0};
1238 slave_info.sft = &sft;
1239 slave_info.rsp_length = &rsp_length;
1240 slave_info.address = address;
1241
1242 if (slave_strict) {
1243 if ((slave != ctx->slave) && (slave != AGILE_MODBUS_BROADCAST_ADDRESS))
1244 return 0;
1245 }
1246
1247 switch (function) {
1248 case AGILE_MODBUS_FC_READ_COILS:
1249 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: {
1250 int nb = (req[offset + 3] << 8) + req[offset + 4];
1251 if (nb < 1 || AGILE_MODBUS_MAX_READ_BITS < nb) {
1252 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1253 break;
1254 }
1255
1256 int end_address = (int)address + nb - 1;
1257 if (end_address > 0xFFFF) {
1258 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1259 break;
1260 }
1261
1262 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1263 slave_info.nb = (nb / 8) + ((nb % 8) ? 1 : 0);
1264 rsp[rsp_length++] = slave_info.nb;
1265 slave_info.send_index = rsp_length;
1266 rsp_length += slave_info.nb;
1267 slave_info.nb = nb;
1268 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1269 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1270 break;
1271 }
1272 } break;
1273
1274 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
1275 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: {
1276 int nb = (req[offset + 3] << 8) + req[offset + 4];
1277 if (nb < 1 || AGILE_MODBUS_MAX_READ_REGISTERS < nb) {
1278 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1279 break;
1280 }
1281
1282 int end_address = (int)address + nb - 1;
1283 if (end_address > 0xFFFF) {
1284 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1285 break;
1286 }
1287
1288 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1289 slave_info.nb = nb << 1;
1290 rsp[rsp_length++] = slave_info.nb;
1291 slave_info.send_index = rsp_length;
1292 rsp_length += slave_info.nb;
1293 slave_info.nb = nb;
1294 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1295 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1296 break;
1297 }
1298 } break;
1299
1300 case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: {
1302 #if 0
1303 if (address > 0xFFFF) {
1304 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1305 break;
1306 }
1307 #endif
1308
1309 reg_data = (req[offset + 3] << 8) + req[offset + 4];
1310 if (reg_data == 0xFF00 || reg_data == 0x0)
1311 reg_data = reg_data ? 1 : 0;
1312 else {
1313 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1314 break;
1315 }
1316
1317 slave_info.buf = (uint8_t *)&reg_data;
1318 rsp_length = req_length;
1319 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1320 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1321 break;
1322 }
1323 memcpy(rsp, req, req_length);
1324 } break;
1325
1326 case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: {
1328 #if 0
1329 if (address > 0xFFFF) {
1330 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1331 break;
1332 }
1333 #endif
1334
1335 reg_data = (req[offset + 3] << 8) + req[offset + 4];
1336
1337 slave_info.buf = (uint8_t *)&reg_data;
1338 rsp_length = req_length;
1339 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1340 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1341 break;
1342 }
1343 memcpy(rsp, req, req_length);
1344 } break;
1345
1346 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: {
1347 int nb = (req[offset + 3] << 8) + req[offset + 4];
1348 int nb_bits = req[offset + 5];
1349 if (nb < 1 || AGILE_MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb) {
1350 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1351 break;
1352 }
1353
1354 int end_address = (int)address + nb - 1;
1355 if (end_address > 0xFFFF) {
1356 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1357 break;
1358 }
1359
1360 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1361 slave_info.nb = nb;
1362 slave_info.buf = &req[offset + 6];
1363 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + 4)) {
1364 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1365 break;
1366 }
1367 /* 4 to copy the bit address (2) and the quantity of bits */
1368 memcpy(rsp + rsp_length, req + rsp_length, 4);
1369 rsp_length += 4;
1370 } break;
1371
1372 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: {
1373 int nb = (req[offset + 3] << 8) + req[offset + 4];
1374 int nb_bytes = req[offset + 5];
1375 if (nb < 1 || AGILE_MODBUS_MAX_WRITE_REGISTERS < nb || nb_bytes != nb * 2) {
1376 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1377 break;
1378 }
1379
1380 int end_address = (int)address + nb - 1;
1381 if (end_address > 0xFFFF) {
1382 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1383 break;
1384 }
1385
1386 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1387 slave_info.nb = nb;
1388 slave_info.buf = &req[offset + 6];
1389 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + 4)) {
1390 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1391 break;
1392 }
1393 /* 4 to copy the address (2) and the no. of registers */
1394 memcpy(rsp + rsp_length, req + rsp_length, 4);
1395 rsp_length += 4;
1396
1397 } break;
1398
1399 case AGILE_MODBUS_FC_REPORT_SLAVE_ID: {
1400 int str_len;
1401 int byte_count_pos;
1402
1403 slave_cb = NULL;
1404 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1405 /* Skip byte count for now */
1406 byte_count_pos = rsp_length++;
1407 rsp[rsp_length++] = ctx->slave;
1408 /* Run indicator status to ON */
1409 rsp[rsp_length++] = 0xFF;
1410
1411 str_len = strlen(AGILE_MODBUS_VERSION_STRING);
1412 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length + str_len)) {
1413 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1414 break;
1415 }
1416 memcpy(rsp + rsp_length, AGILE_MODBUS_VERSION_STRING, str_len);
1417 rsp_length += str_len;
1418 rsp[byte_count_pos] = rsp_length - byte_count_pos - 1;
1419 } break;
1420
1421 case AGILE_MODBUS_FC_READ_EXCEPTION_STATUS:
1422 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
1423 break;
1424
1425 case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: {
1427 #if 0
1428 if (address > 0xFFFF) {
1429 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1430 break;
1431 }
1432 #endif
1433
1434 slave_info.buf = &req[offset + 3];
1435 rsp_length = req_length;
1436 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1437 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1438 break;
1439 }
1440 memcpy(rsp, req, req_length);
1441 } break;
1442
1443 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: {
1444 int nb = (req[offset + 3] << 8) + req[offset + 4];
1445 uint16_t address_write = (req[offset + 5] << 8) + req[offset + 6];
1446 int nb_write = (req[offset + 7] << 8) + req[offset + 8];
1447 int nb_write_bytes = req[offset + 9];
1448 if (nb_write < 1 || AGILE_MODBUS_MAX_WR_WRITE_REGISTERS < nb_write ||
1449 nb < 1 || AGILE_MODBUS_MAX_WR_READ_REGISTERS < nb ||
1450 nb_write_bytes != nb_write * 2) {
1451 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE;
1452 break;
1453 }
1454
1455 int end_address = (int)address + nb - 1;
1456 int end_address_write = (int)address_write + nb_write - 1;
1457 if (end_address > 0xFFFF || end_address_write > 0xFFFF) {
1458 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
1459 break;
1460 }
1461
1462 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1463 rsp[rsp_length++] = nb << 1;
1464 slave_info.buf = &req[offset + 3];
1465 slave_info.send_index = rsp_length;
1466 rsp_length += (nb << 1);
1467 if (ctx->send_bufsz < (int)(rsp_length + ctx->backend->checksum_length)) {
1468 exception_code = AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE;
1469 break;
1470 }
1471 } break;
1472
1473 default: {
1474 if (slave_cb == NULL)
1475 exception_code = AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
1476 else {
1477 rsp_length = ctx->backend->build_response_basis(&sft, rsp);
1478 slave_info.send_index = rsp_length;
1479 slave_info.buf = &req[offset + 1];
1480 slave_info.nb = req_length - offset - 1;
1481 }
1482 } break;
1483 }
1484
1485 if (exception_code)
1486 rsp_length = agile_modbus_serialize_response_exception(ctx, &sft, exception_code);
1487 else {
1488 if (slave_cb) {
1489 int ret = slave_cb(ctx, &slave_info, slave_data);
1490
1491 if (ret < 0) {
1492 if (ret == -AGILE_MODBUS_EXCEPTION_UNKNOW)
1493 rsp_length = 0;
1494 else
1495 rsp_length = agile_modbus_serialize_response_exception(ctx, &sft, -ret);
1496 }
1497 }
1498 }
1499
1500 if (rsp_length) {
1501 if ((ctx->backend->backend_type == AGILE_MODBUS_BACKEND_TYPE_RTU) && (slave == AGILE_MODBUS_BROADCAST_ADDRESS))
1502 return 0;
1503
1504 rsp_length = ctx->backend->send_msg_pre(rsp, rsp_length);
1505 }
1506
1507 return rsp_length;
1508}
1509
agile_modbus.h
Agile Modbus software package common header file.
agile_modbus_set_compute_data_length_after_meta_cb
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 modbu...
Definition: agile_modbus.c:297
agile_modbus_common_init
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
Definition: agile_modbus.c:257
agile_modbus_receive_judge
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.
Definition: agile_modbus.c:312
agile_modbus_set_slave
int agile_modbus_set_slave(agile_modbus_t *ctx, int slave)
set address
Definition: agile_modbus.c:273
agile_modbus_set_compute_meta_length_after_function_cb
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 ...
Definition: agile_modbus.c:284
agile_modbus_msg_type_t
agile_modbus_msg_type_t
Modbus received message type.
Definition: agile_modbus.h:179
AGILE_MODBUS_EXCEPTION_UNKNOW
@ AGILE_MODBUS_EXCEPTION_UNKNOW
Definition: agile_modbus.h:158
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
Definition: agile_modbus.h:147
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
Definition: agile_modbus.h:149
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
Definition: agile_modbus.h:148
AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
@ AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
Definition: agile_modbus.h:153
AGILE_MODBUS_BACKEND_TYPE_RTU
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU.
Definition: agile_modbus.h:165
AGILE_MODBUS_MSG_CONFIRMATION
@ AGILE_MODBUS_MSG_CONFIRMATION
Server-side request message.
Definition: agile_modbus.h:181
AGILE_MODBUS_MSG_INDICATION
@ AGILE_MODBUS_MSG_INDICATION
Host-side request message.
Definition: agile_modbus.h:180
AGILE_MODBUS_MSG_LENGTH_UNDEFINED
#define AGILE_MODBUS_MSG_LENGTH_UNDEFINED
The corresponding function code data length is undefined.
Definition: agile_modbus.c:49
agile_modbus_receive_msg_judge
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.
Definition: agile_modbus.c:224
agile_modbus_compute_meta_length_after_function
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.
Definition: agile_modbus.c:88
agile_modbus_compute_data_length_after_meta
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.
Definition: agile_modbus.c:177
agile_modbus_serialize_write_register
int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value)
Definition: agile_modbus.c:721
agile_modbus_serialize_write_and_read_registers
int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb)
Definition: agile_modbus.c:900
agile_modbus_deserialize_write_and_read_registers
int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:941
agile_modbus_deserialize_mask_write_register
int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:883
agile_modbus_deserialize_write_registers
int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:844
agile_modbus_deserialize_write_bit
int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:704
agile_modbus_serialize_read_registers
int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:603
agile_modbus_deserialize_read_input_bits
int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:564
agile_modbus_serialize_write_bits
int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src)
Definition: agile_modbus.c:751
agile_modbus_serialize_read_bits
int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:493
agile_modbus_serialize_write_registers
int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src)
Definition: agile_modbus.c:813
agile_modbus_deserialize_report_slave_id
int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest)
Definition: agile_modbus.c:984
agile_modbus_deserialize_read_input_registers
int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:663
agile_modbus_deserialize_read_bits
int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:509
agile_modbus_serialize_write_bit
int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status)
Definition: agile_modbus.c:691
agile_modbus_deserialize_write_register
int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:734
agile_modbus_serialize_mask_write_register
int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask)
Definition: agile_modbus.c:861
agile_modbus_deserialize_write_bits
int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:796
agile_modbus_serialize_read_input_registers
int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:647
agile_modbus_deserialize_read_registers
int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:619
agile_modbus_serialize_report_slave_id
int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx)
Definition: agile_modbus.c:969
agile_modbus_serialize_read_input_bits
int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:548
agile_modbus_check_confirmation
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.
Definition: agile_modbus.c:391
agile_modbus_compute_response_length_from_request
static int agile_modbus_compute_response_length_from_request(agile_modbus_t *ctx, uint8_t *req)
Calculate the expected response data length.
Definition: agile_modbus.c:342
agile_modbus_deserialize_raw_response
int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length)
parses the original response data
Definition: agile_modbus.c:1073
agile_modbus_serialize_raw_request
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.
Definition: agile_modbus.c:1031
AGILE_MODBUS_BROADCAST_ADDRESS
#define AGILE_MODBUS_BROADCAST_ADDRESS
Modbus broadcast address.
Definition: agile_modbus.h:69
AGILE_MODBUS_MAX_READ_BITS
#define AGILE_MODBUS_MAX_READ_BITS
Definition: agile_modbus.h:81
AGILE_MODBUS_VERSION_STRING
#define AGILE_MODBUS_VERSION_STRING
Agile Modbus version number.
Definition: agile_modbus.h:67
AGILE_MODBUS_MAX_WRITE_REGISTERS
#define AGILE_MODBUS_MAX_WRITE_REGISTERS
Definition: agile_modbus.h:100
AGILE_MODBUS_MAX_WRITE_BITS
#define AGILE_MODBUS_MAX_WRITE_BITS
Definition: agile_modbus.h:82
AGILE_MODBUS_MAX_WR_WRITE_REGISTERS
#define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS
Definition: agile_modbus.h:101
AGILE_MODBUS_MAX_READ_REGISTERS
#define AGILE_MODBUS_MAX_READ_REGISTERS
Definition: agile_modbus.h:99
AGILE_MODBUS_MAX_WR_READ_REGISTERS
#define AGILE_MODBUS_MAX_WR_READ_REGISTERS
Definition: agile_modbus.h:102
AGILE_MODBUS_FC_MASK_WRITE_REGISTER
#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER
Definition: agile_modbus.h:58
AGILE_MODBUS_FC_READ_COILS
#define AGILE_MODBUS_FC_READ_COILS
Definition: agile_modbus.h:48
AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER
#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER
Definition: agile_modbus.h:53
AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS
Definition: agile_modbus.h:55
AGILE_MODBUS_FC_WRITE_SINGLE_COIL
#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL
Definition: agile_modbus.h:52
AGILE_MODBUS_FC_READ_HOLDING_REGISTERS
#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS
Definition: agile_modbus.h:50
AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS
#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS
Definition: agile_modbus.h:59
AGILE_MODBUS_FC_READ_INPUT_REGISTERS
#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS
Definition: agile_modbus.h:51
AGILE_MODBUS_FC_REPORT_SLAVE_ID
#define AGILE_MODBUS_FC_REPORT_SLAVE_ID
Definition: agile_modbus.h:57
AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS
Definition: agile_modbus.h:56
AGILE_MODBUS_FC_READ_EXCEPTION_STATUS
#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS
Definition: agile_modbus.h:54
AGILE_MODBUS_FC_READ_DISCRETE_INPUTS
#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS
Definition: agile_modbus.h:49
agile_modbus_slave_callback_t
int(* agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
Definition: agile_modbus.h:265
agile_modbus_slave_handle
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
Definition: agile_modbus.c:1203
agile_modbus_slave_io_get
uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
Read slave IO status.
Definition: agile_modbus.c:1156
agile_modbus_slave_io_set
void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
slave IO settings
Definition: agile_modbus.c:1139
agile_modbus_slave_register_get
uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
Read slave register data.
Definition: agile_modbus.c:1184
agile_modbus_slave_register_set
void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
slave register settings
Definition: agile_modbus.c:1172
agile_modbus_serialize_response_exception
static int agile_modbus_serialize_response_exception(agile_modbus_t *ctx, agile_modbus_sft_t *sft, int exception_code)
packaged exception response data
Definition: agile_modbus.c:1113
agile_modbus_backend::check_integrity
int(* check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
Check the receive data integrity interface.
Definition: agile_modbus.h:209
agile_modbus_backend::set_slave
int(* set_slave)(agile_modbus_t *ctx, int slave)
Set address interface.
Definition: agile_modbus.h:203
agile_modbus_backend::pre_check_confirmation
int(* pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
Pre-check confirmation interface.
Definition: agile_modbus.h:210
agile_modbus_backend::build_response_basis
int(* build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp)
Build a basic response message interface.
Definition: agile_modbus.h:206
agile_modbus_backend::header_length
uint32_t header_length
Header length, excluding function code.
Definition: agile_modbus.h:200
agile_modbus_backend::backend_type
uint32_t backend_type
Backend type.
Definition: agile_modbus.h:199
agile_modbus_backend::send_msg_pre
int(* send_msg_pre)(uint8_t *req, int req_length)
Pre-send data interface.
Definition: agile_modbus.h:208
agile_modbus_backend::prepare_response_tid
int(* prepare_response_tid)(const uint8_t *req, int *req_length)
Prepare response interface.
Definition: agile_modbus.h:207
agile_modbus_backend::build_request_basis
int(* build_request_basis)(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
Build a basic request message interface.
Definition: agile_modbus.h:204
agile_modbus_backend::checksum_length
uint32_t checksum_length
Check data length.
Definition: agile_modbus.h:201
agile_modbus_sft
contains the modbus header parameter structure
Definition: agile_modbus.h:187
agile_modbus_sft::slave
int slave
slave address
Definition: agile_modbus.h:188
agile_modbus_sft::t_id
int t_id
Transaction identifier.
Definition: agile_modbus.h:190
agile_modbus_sft::function
int function
function code
Definition: agile_modbus.h:189
agile_modbus_slave_info
Agile Modbus slave information structure.
Definition: agile_modbus.h:246
agile_modbus_slave_info::buf
uint8_t * buf
Data fields required for different function codes.
Definition: agile_modbus.h:251
agile_modbus_slave_info::send_index
int send_index
Current index of sending buffer.
Definition: agile_modbus.h:252
agile_modbus_slave_info::sft
agile_modbus_sft_t * sft
sft structure pointer
Definition: agile_modbus.h:247
agile_modbus_slave_info::address
int address
Register address.
Definition: agile_modbus.h:249
agile_modbus_slave_info::rsp_length
int * rsp_length
Response data length pointer.
Definition: agile_modbus.h:248
agile_modbus_slave_info::nb
int nb
number
Definition: agile_modbus.h:250
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217
agile_modbus::compute_data_length_after_meta
int(* compute_data_length_after_meta)(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
Customized calculation data length interface.
Definition: agile_modbus.h:225
agile_modbus::compute_meta_length_after_function
uint8_t(* compute_meta_length_after_function)(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
Customized calculation data element length interface.
Definition: agile_modbus.h:223
agile_modbus::send_bufsz
int send_bufsz
Send buffer size.
Definition: agile_modbus.h:220
agile_modbus::send_buf
uint8_t * send_buf
Send buffer.
Definition: agile_modbus.h:219
agile_modbus::read_buf
uint8_t * read_buf
Receive buffer.
Definition: agile_modbus.h:221
agile_modbus::backend
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
agile_modbus::slave
int slave
slave address
Definition: agile_modbus.h:218
agile_modbus::read_bufsz
int read_bufsz
Receive buffer size.
Definition: agile_modbus.h:222


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus.h Source File

  • inc
agile_modbus.h
Go to the documentation of this file.
1
14#ifndef __PKG_AGILE_MODBUS_H
15#define __PKG_AGILE_MODBUS_H
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include <stdint.h>
22
30#ifndef AGILE_MODBUS_USING_RTU
31#define AGILE_MODBUS_USING_RTU 1
32#endif /* AGILE_MODBUS_USING_RTU */
33
34#ifndef AGILE_MODBUS_USING_TCP
35#define AGILE_MODBUS_USING_TCP 1
36#endif /* AGILE_MODBUS_USING_TCP */
48#define AGILE_MODBUS_FC_READ_COILS 0x01
49#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02
50#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03
51#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04
52#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05
53#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
54#define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07
55#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
56#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
57#define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11
58#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16
59#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
67#define AGILE_MODBUS_VERSION_STRING "AMB_1.1.3"
69#define AGILE_MODBUS_BROADCAST_ADDRESS 0
81#define AGILE_MODBUS_MAX_READ_BITS 2000
82#define AGILE_MODBUS_MAX_WRITE_BITS 1968
99#define AGILE_MODBUS_MAX_READ_REGISTERS 125
100#define AGILE_MODBUS_MAX_WRITE_REGISTERS 123
101#define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121
102#define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125
116#define AGILE_MODBUS_MAX_PDU_LENGTH 253
117
130#define AGILE_MODBUS_MAX_ADU_LENGTH 260
146enum {
147 AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
148 AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
149 AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
150 AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
151 AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE,
152 AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
153 AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
154 AGILE_MODBUS_EXCEPTION_MEMORY_PARITY,
155 AGILE_MODBUS_EXCEPTION_NOT_DEFINED,
156 AGILE_MODBUS_EXCEPTION_GATEWAY_PATH,
157 AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET,
158 AGILE_MODBUS_EXCEPTION_UNKNOW = 0xff
159};
160
164typedef enum {
165 AGILE_MODBUS_BACKEND_TYPE_RTU = 0,
166 AGILE_MODBUS_BACKEND_TYPE_TCP
167} agile_modbus_backend_type_t;
168
179typedef enum {
180 AGILE_MODBUS_MSG_INDICATION,
181 AGILE_MODBUS_MSG_CONFIRMATION
182} agile_modbus_msg_type_t;
183
187typedef struct agile_modbus_sft {
188 int slave;
189 int function;
190 int t_id;
191} agile_modbus_sft_t;
192
193typedef struct agile_modbus agile_modbus_t;
198typedef struct agile_modbus_backend {
199 uint32_t backend_type;
200 uint32_t header_length;
201 uint32_t checksum_length;
202 uint32_t max_adu_length;
203 int (*set_slave)(agile_modbus_t *ctx, int slave);
204 int (*build_request_basis)(agile_modbus_t *ctx, int function, int addr,
205 int nb, uint8_t *req);
206 int (*build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp);
207 int (*prepare_response_tid)(const uint8_t *req, int *req_length);
208 int (*send_msg_pre)(uint8_t *req, int req_length);
209 int (*check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length);
210 int (*pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req,
211 const uint8_t *rsp, int rsp_length);
212} agile_modbus_backend_t;
213
217struct agile_modbus {
218 int slave;
219 uint8_t *send_buf;
220 int send_bufsz;
221 uint8_t *read_buf;
222 int read_bufsz;
223 uint8_t (*compute_meta_length_after_function)(agile_modbus_t *ctx, int function,
224 agile_modbus_msg_type_t msg_type);
225 int (*compute_data_length_after_meta)(agile_modbus_t *ctx, uint8_t *msg,
226 int msg_length, agile_modbus_msg_type_t msg_type);
227 const agile_modbus_backend_t *backend;
228 void *backend_data;
229};
230
246struct agile_modbus_slave_info {
247 agile_modbus_sft_t *sft;
248 int *rsp_length;
249 int address;
250 int nb;
251 uint8_t *buf;
252 int send_index;
253};
254
265typedef int (*agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data);
266
278void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
279int agile_modbus_set_slave(agile_modbus_t *ctx, int slave);
280void agile_modbus_set_compute_meta_length_after_function_cb(agile_modbus_t *ctx,
281 uint8_t (*cb)(agile_modbus_t *ctx, int function,
282 agile_modbus_msg_type_t msg_type));
283void agile_modbus_set_compute_data_length_after_meta_cb(agile_modbus_t *ctx,
284 int (*cb)(agile_modbus_t *ctx, uint8_t *msg,
285 int msg_length, agile_modbus_msg_type_t msg_type));
286int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length, agile_modbus_msg_type_t msg_type);
298int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb);
299int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
300int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb);
301int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
302int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb);
303int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
304int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb);
305int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
306int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status);
307int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length);
308int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value);
309int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length);
310int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src);
311int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length);
312int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src);
313int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length);
314int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
315int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length);
316int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx,
317 int write_addr, int write_nb,
318 const uint16_t *src,
319 int read_addr, int read_nb);
320int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
321int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx);
322int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest);
330int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
331int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length);
347int agile_modbus_slave_handle(agile_modbus_t *ctx, int msg_length, uint8_t slave_strict,
348 agile_modbus_slave_callback_t slave_cb, const void *slave_data, int *frame_length);
349void agile_modbus_slave_io_set(uint8_t *buf, int index, int status);
350uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index);
351void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data);
352uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index);
365/* Include RTU and TCP module */
366#if AGILE_MODBUS_USING_RTU
367#include "agile_modbus_rtu.h"
368#endif /* AGILE_MODBUS_USING_RTU */
369
370#if AGILE_MODBUS_USING_TCP
371#include "agile_modbus_tcp.h"
372#endif /* AGILE_MODBUS_USING_TCP */
373
374#ifdef __cplusplus
375}
376#endif
377
378#endif /* __PKG_AGILE_MODBUS_H */
agile_modbus_rtu.h
Agile Modbus package RTU header file.
agile_modbus_tcp.h
Agile Modbus package TCP header file.
agile_modbus_set_compute_data_length_after_meta_cb
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 modbu...
Definition: agile_modbus.c:297
agile_modbus_common_init
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
Definition: agile_modbus.c:257
agile_modbus_receive_judge
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.
Definition: agile_modbus.c:312
agile_modbus_set_slave
int agile_modbus_set_slave(agile_modbus_t *ctx, int slave)
set address
Definition: agile_modbus.c:273
agile_modbus_set_compute_meta_length_after_function_cb
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 ...
Definition: agile_modbus.c:284
agile_modbus_backend_t
struct agile_modbus_backend agile_modbus_backend_t
Agile Modbus backend interface structure.
agile_modbus_backend_type_t
agile_modbus_backend_type_t
@ brief Modbus backend type
Definition: agile_modbus.h:164
agile_modbus_sft_t
struct agile_modbus_sft agile_modbus_sft_t
contains the modbus header parameter structure
agile_modbus_msg_type_t
agile_modbus_msg_type_t
Modbus received message type.
Definition: agile_modbus.h:179
AGILE_MODBUS_EXCEPTION_GATEWAY_PATH
@ AGILE_MODBUS_EXCEPTION_GATEWAY_PATH
Definition: agile_modbus.h:156
AGILE_MODBUS_EXCEPTION_UNKNOW
@ AGILE_MODBUS_EXCEPTION_UNKNOW
Definition: agile_modbus.h:158
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
Definition: agile_modbus.h:147
AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET
@ AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET
Definition: agile_modbus.h:157
AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE
@ AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE
Definition: agile_modbus.h:151
AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
@ AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
Definition: agile_modbus.h:152
AGILE_MODBUS_EXCEPTION_NOT_DEFINED
@ AGILE_MODBUS_EXCEPTION_NOT_DEFINED
Definition: agile_modbus.h:155
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
Definition: agile_modbus.h:149
AGILE_MODBUS_EXCEPTION_MEMORY_PARITY
@ AGILE_MODBUS_EXCEPTION_MEMORY_PARITY
Definition: agile_modbus.h:154
AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE
@ AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE
Definition: agile_modbus.h:150
AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
Definition: agile_modbus.h:148
AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
@ AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE
Definition: agile_modbus.h:153
AGILE_MODBUS_BACKEND_TYPE_RTU
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU.
Definition: agile_modbus.h:165
AGILE_MODBUS_BACKEND_TYPE_TCP
@ AGILE_MODBUS_BACKEND_TYPE_TCP
TCP.
Definition: agile_modbus.h:166
AGILE_MODBUS_MSG_CONFIRMATION
@ AGILE_MODBUS_MSG_CONFIRMATION
Server-side request message.
Definition: agile_modbus.h:181
AGILE_MODBUS_MSG_INDICATION
@ AGILE_MODBUS_MSG_INDICATION
Host-side request message.
Definition: agile_modbus.h:180
agile_modbus_serialize_write_register
int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value)
Definition: agile_modbus.c:721
agile_modbus_serialize_write_and_read_registers
int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb)
Definition: agile_modbus.c:900
agile_modbus_deserialize_write_and_read_registers
int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:941
agile_modbus_deserialize_mask_write_register
int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:883
agile_modbus_deserialize_write_registers
int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:844
agile_modbus_deserialize_write_bit
int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:704
agile_modbus_serialize_read_registers
int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:603
agile_modbus_deserialize_read_input_bits
int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:564
agile_modbus_serialize_write_bits
int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src)
Definition: agile_modbus.c:751
agile_modbus_serialize_read_bits
int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:493
agile_modbus_serialize_write_registers
int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src)
Definition: agile_modbus.c:813
agile_modbus_deserialize_report_slave_id
int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest)
Definition: agile_modbus.c:984
agile_modbus_deserialize_read_input_registers
int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:663
agile_modbus_deserialize_read_bits
int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest)
Definition: agile_modbus.c:509
agile_modbus_serialize_write_bit
int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status)
Definition: agile_modbus.c:691
agile_modbus_deserialize_write_register
int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:734
agile_modbus_serialize_mask_write_register
int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask)
Definition: agile_modbus.c:861
agile_modbus_deserialize_write_bits
int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length)
Definition: agile_modbus.c:796
agile_modbus_serialize_read_input_registers
int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:647
agile_modbus_deserialize_read_registers
int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest)
Definition: agile_modbus.c:619
agile_modbus_serialize_report_slave_id
int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx)
Definition: agile_modbus.c:969
agile_modbus_serialize_read_input_bits
int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb)
Definition: agile_modbus.c:548
agile_modbus_deserialize_raw_response
int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length)
parses the original response data
Definition: agile_modbus.c:1073
agile_modbus_serialize_raw_request
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.
Definition: agile_modbus.c:1031
agile_modbus_slave_callback_t
int(* agile_modbus_slave_callback_t)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
Definition: agile_modbus.h:265
agile_modbus_slave_handle
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
Definition: agile_modbus.c:1203
agile_modbus_slave_io_get
uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
Read slave IO status.
Definition: agile_modbus.c:1156
agile_modbus_slave_io_set
void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
slave IO settings
Definition: agile_modbus.c:1139
agile_modbus_slave_register_get
uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
Read slave register data.
Definition: agile_modbus.c:1184
agile_modbus_slave_register_set
void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
slave register settings
Definition: agile_modbus.c:1172
agile_modbus_backend
Agile Modbus backend interface structure.
Definition: agile_modbus.h:198
agile_modbus_backend::check_integrity
int(* check_integrity)(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
Check the receive data integrity interface.
Definition: agile_modbus.h:209
agile_modbus_backend::set_slave
int(* set_slave)(agile_modbus_t *ctx, int slave)
Set address interface.
Definition: agile_modbus.h:203
agile_modbus_backend::max_adu_length
uint32_t max_adu_length
Backend ADU length.
Definition: agile_modbus.h:202
agile_modbus_backend::pre_check_confirmation
int(* pre_check_confirmation)(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
Pre-check confirmation interface.
Definition: agile_modbus.h:210
agile_modbus_backend::build_response_basis
int(* build_response_basis)(agile_modbus_sft_t *sft, uint8_t *rsp)
Build a basic response message interface.
Definition: agile_modbus.h:206
agile_modbus_backend::header_length
uint32_t header_length
Header length, excluding function code.
Definition: agile_modbus.h:200
agile_modbus_backend::backend_type
uint32_t backend_type
Backend type.
Definition: agile_modbus.h:199
agile_modbus_backend::send_msg_pre
int(* send_msg_pre)(uint8_t *req, int req_length)
Pre-send data interface.
Definition: agile_modbus.h:208
agile_modbus_backend::prepare_response_tid
int(* prepare_response_tid)(const uint8_t *req, int *req_length)
Prepare response interface.
Definition: agile_modbus.h:207
agile_modbus_backend::build_request_basis
int(* build_request_basis)(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
Build a basic request message interface.
Definition: agile_modbus.h:204
agile_modbus_backend::checksum_length
uint32_t checksum_length
Check data length.
Definition: agile_modbus.h:201
agile_modbus_sft
contains the modbus header parameter structure
Definition: agile_modbus.h:187
agile_modbus_sft::slave
int slave
slave address
Definition: agile_modbus.h:188
agile_modbus_sft::t_id
int t_id
Transaction identifier.
Definition: agile_modbus.h:190
agile_modbus_slave_info
Agile Modbus slave information structure.
Definition: agile_modbus.h:246
agile_modbus_slave_info::buf
uint8_t * buf
Data fields required for different function codes.
Definition: agile_modbus.h:251
agile_modbus_slave_info::send_index
int send_index
Current index of sending buffer.
Definition: agile_modbus.h:252
agile_modbus_slave_info::sft
agile_modbus_sft_t * sft
sft structure pointer
Definition: agile_modbus.h:247
agile_modbus_slave_info::address
int address
Register address.
Definition: agile_modbus.h:249
agile_modbus_slave_info::rsp_length
int * rsp_length
Response data length pointer.
Definition: agile_modbus.h:248
agile_modbus_slave_info::nb
int nb
number
Definition: agile_modbus.h:250
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217
agile_modbus::compute_data_length_after_meta
int(* compute_data_length_after_meta)(agile_modbus_t *ctx, uint8_t *msg, int msg_length, agile_modbus_msg_type_t msg_type)
Customized calculation data length interface.
Definition: agile_modbus.h:225
agile_modbus::compute_meta_length_after_function
uint8_t(* compute_meta_length_after_function)(agile_modbus_t *ctx, int function, agile_modbus_msg_type_t msg_type)
Customized calculation data element length interface.
Definition: agile_modbus.h:223
agile_modbus::send_bufsz
int send_bufsz
Send buffer size.
Definition: agile_modbus.h:220
agile_modbus::send_buf
uint8_t * send_buf
Send buffer.
Definition: agile_modbus.h:219
agile_modbus::read_buf
uint8_t * read_buf
Receive buffer.
Definition: agile_modbus.h:221
agile_modbus::backend
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
agile_modbus::slave
int slave
slave address
Definition: agile_modbus.h:218
agile_modbus::read_bufsz
int read_bufsz
Receive buffer size.
Definition: agile_modbus.h:222
agile_modbus::backend_data
void * backend_data
Backend data, pointing to RTU or TCP structure.
Definition: agile_modbus.h:228


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus_rtu.c Source File

  • src
agile_modbus_rtu.c
Go to the documentation of this file.
1
14#include "agile_modbus.h"
15
16#if AGILE_MODBUS_USING_RTU
17
18#include "agile_modbus_rtu.h"
19
28static const uint8_t _table_crc_hi[] =
29 {
30 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
31 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
32 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
33 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
34 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
35 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
36 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
37 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
38 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
39 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
40 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
41 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
42 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
43 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
44 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
45 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
46 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
47 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
48 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
49 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
50 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
51 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
52 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
53 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
54 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
55 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40};
56
58static const uint8_t _table_crc_lo[] =
59 {
60 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
61 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
62 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
63 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
64 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
65 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
66 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
67 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
68 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
69 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
70 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
71 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
72 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
73 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
74 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
75 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
76 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
77 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
78 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
79 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
80 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
81 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
82 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
83 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
84 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
85 0x43, 0x83, 0x41, 0x81, 0x80, 0x40};
100static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length)
101{
102 uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
103 uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
104 unsigned int i; /* will index into CRC lookup */
105
106 /* pass through message buffer */
107 while (buffer_length--) {
108 i = crc_hi ^ *buffer++; /* calculate the CRC */
109 crc_hi = crc_lo ^ _table_crc_hi[i];
110 crc_lo = _table_crc_lo[i];
111 }
112
113 return (crc_hi << 8 | crc_lo);
114}
115
122static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
123{
124 ctx->slave = slave;
125 return 0;
126}
127
137static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function,
138 int addr, int nb,
139 uint8_t *req)
140{
141 req[0] = ctx->slave;
142 req[1] = function;
143 req[2] = addr >> 8;
144 req[3] = addr & 0x00ff;
145 req[4] = nb >> 8;
146 req[5] = nb & 0x00ff;
147
148 return AGILE_MODBUS_RTU_PRESET_REQ_LENGTH;
149}
150
157static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
158{
159 rsp[0] = sft->slave;
160 rsp[1] = sft->function;
161
162 return AGILE_MODBUS_RTU_PRESET_RSP_LENGTH;
163}
164
172static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
173{
174 (*req_length) -= AGILE_MODBUS_RTU_CHECKSUM_LENGTH;
175 /* No TID */
176 return 0;
177}
178
186static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
187{
188 uint16_t crc = agile_modbus_rtu_crc16(req, req_length);
189 req[req_length++] = crc >> 8;
190 req[req_length++] = crc & 0x00FF;
191
192 return req_length;
193}
194
202static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
203{
204 uint16_t crc_calculated;
205 uint16_t crc_received;
206
207 crc_calculated = agile_modbus_rtu_crc16(msg, msg_length - 2);
208 crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
209
210 /* Check CRC of msg */
211 if (crc_calculated == crc_received)
212 return msg_length;
213
214 return -1;
215}
216
226static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
227 const uint8_t *rsp, int rsp_length)
228{
229 /* Check responding slave is the slave we requested (except for broacast
230 * request) */
231 if (req[0] != rsp[0] && req[0] != AGILE_MODBUS_BROADCAST_ADDRESS)
232 return -1;
233
234 return 0;
235}
236
248static const agile_modbus_backend_t agile_modbus_rtu_backend =
249 {
250 AGILE_MODBUS_BACKEND_TYPE_RTU,
251 AGILE_MODBUS_RTU_HEADER_LENGTH,
252 AGILE_MODBUS_RTU_CHECKSUM_LENGTH,
253 AGILE_MODBUS_RTU_MAX_ADU_LENGTH,
254 agile_modbus_rtu_set_slave,
255 agile_modbus_rtu_build_request_basis,
256 agile_modbus_rtu_build_response_basis,
257 agile_modbus_rtu_prepare_response_tid,
258 agile_modbus_rtu_send_msg_pre,
259 agile_modbus_rtu_check_integrity,
260 agile_modbus_rtu_pre_check_confirmation};
261
279int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
280{
281 agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
282 ctx->_ctx.backend = &agile_modbus_rtu_backend;
283 ctx->_ctx.backend_data = ctx;
284
285 return 0;
286}
287
296#endif /* AGILE_MODBUS_USING_RTU */
agile_modbus.h
Agile Modbus software package common header file.
agile_modbus_rtu.h
Agile Modbus package RTU header file.
agile_modbus_common_init
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
Definition: agile_modbus.c:257
AGILE_MODBUS_BACKEND_TYPE_RTU
@ AGILE_MODBUS_BACKEND_TYPE_RTU
RTU.
Definition: agile_modbus.h:165
AGILE_MODBUS_BROADCAST_ADDRESS
#define AGILE_MODBUS_BROADCAST_ADDRESS
Modbus broadcast address.
Definition: agile_modbus.h:69
AGILE_MODBUS_RTU_HEADER_LENGTH
#define AGILE_MODBUS_RTU_HEADER_LENGTH
Definition: agile_modbus_rtu.h:32
AGILE_MODBUS_RTU_MAX_ADU_LENGTH
#define AGILE_MODBUS_RTU_MAX_ADU_LENGTH
Definition: agile_modbus_rtu.h:45
AGILE_MODBUS_RTU_PRESET_RSP_LENGTH
#define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH
Definition: agile_modbus_rtu.h:34
AGILE_MODBUS_RTU_PRESET_REQ_LENGTH
#define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH
Definition: agile_modbus_rtu.h:33
AGILE_MODBUS_RTU_CHECKSUM_LENGTH
#define AGILE_MODBUS_RTU_CHECKSUM_LENGTH
Definition: agile_modbus_rtu.h:36
agile_modbus_rtu_init
int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
RTU initialization.
Definition: agile_modbus_rtu.c:279
_table_crc_hi
static const uint8_t _table_crc_hi[]
Table of CRC values for high-order byte.
Definition: agile_modbus_rtu.c:28
agile_modbus_rtu_backend
static const agile_modbus_backend_t agile_modbus_rtu_backend
RTU backend interface.
Definition: agile_modbus_rtu.c:248
_table_crc_lo
static const uint8_t _table_crc_lo[]
Table of CRC values for low-order byte.
Definition: agile_modbus_rtu.c:58
agile_modbus_rtu_build_response_basis
static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
RTU builds the basic response message interface (header message)
Definition: agile_modbus_rtu.c:157
agile_modbus_rtu_send_msg_pre
static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
RTU pre-send data interface.
Definition: agile_modbus_rtu.c:186
agile_modbus_rtu_build_request_basis
static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
RTU builds the basic request message interface (header message)
Definition: agile_modbus_rtu.c:137
agile_modbus_rtu_set_slave
static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
RTU sets the address interface.
Definition: agile_modbus_rtu.c:122
agile_modbus_rtu_crc16
static uint16_t agile_modbus_rtu_crc16(uint8_t *buffer, uint16_t buffer_length)
RTU CRC16 calculation.
Definition: agile_modbus_rtu.c:100
agile_modbus_rtu_check_integrity
static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
RTU check received data integrity interface (CRC16 comparison)
Definition: agile_modbus_rtu.c:202
agile_modbus_rtu_pre_check_confirmation
static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
RTU pre-check confirmation interface (request response address comparison)
Definition: agile_modbus_rtu.c:226
agile_modbus_rtu_prepare_response_tid
static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
RTU ready response interface.
Definition: agile_modbus_rtu.c:172
agile_modbus_backend
Agile Modbus backend interface structure.
Definition: agile_modbus.h:198
agile_modbus_rtu
RTU structure.
Definition: agile_modbus_rtu.h:57
agile_modbus_rtu::_ctx
agile_modbus_t _ctx
modbus handle
Definition: agile_modbus_rtu.h:58
agile_modbus_sft
contains the modbus header parameter structure
Definition: agile_modbus.h:187
agile_modbus_sft::slave
int slave
slave address
Definition: agile_modbus.h:188
agile_modbus_sft::function
int function
function code
Definition: agile_modbus.h:189
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217
agile_modbus::backend
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
agile_modbus::slave
int slave
slave address
Definition: agile_modbus.h:218
agile_modbus::backend_data
void * backend_data
Backend data, pointing to RTU or TCP structure.
Definition: agile_modbus.h:228


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_rtu.h Source File

  • inc
agile_modbus_rtu.h
Go to the documentation of this file.
1
14#ifndef __PKG_AGILE_MODBUS_RTU_H
15#define __PKG_AGILE_MODBUS_RTU_H
16
17#if AGILE_MODBUS_USING_RTU
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include <stdint.h>
24
32#define AGILE_MODBUS_RTU_HEADER_LENGTH 1
33#define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6
34#define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2
35
36#define AGILE_MODBUS_RTU_CHECKSUM_LENGTH 2
37
45#define AGILE_MODBUS_RTU_MAX_ADU_LENGTH 256
57typedef struct agile_modbus_rtu {
58 agile_modbus_t _ctx;
59} agile_modbus_rtu_t;
60
68int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
77#ifdef __cplusplus
78}
79#endif
80
81#endif /* AGILE_MODBUS_USING_RTU */
82
83#endif /* __PKG_AGILE_MODBUS_RTU_H */
agile_modbus_rtu_init
int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
RTU initialization.
Definition: agile_modbus_rtu.c:279
agile_modbus_rtu_t
struct agile_modbus_rtu agile_modbus_rtu_t
RTU structure.
agile_modbus_rtu
RTU structure.
Definition: agile_modbus_rtu.h:57
agile_modbus_rtu::_ctx
agile_modbus_t _ctx
modbus handle
Definition: agile_modbus_rtu.h:58
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.c Source File

  • util
agile_modbus_slave_util.c
Go to the documentation of this file.
1
14#include "agile_modbus.h"
15#include "agile_modbus_slave_util.h"
16#include <string.h>
17
37static const agile_modbus_slave_util_map_t *get_map_by_addr(const agile_modbus_slave_util_map_t *maps, int nb_maps, int address)
38{
39 for (int i = 0; i < nb_maps; i++) {
40 const agile_modbus_slave_util_map_t *map = &maps[i];
41 if (address >= map->start_addr && address <= map->end_addr)
42 return map;
43 }
44
45 return NULL;
46}
47
58static int read_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
59{
60 uint8_t map_buf[AGILE_MODBUS_MAX_PDU_LENGTH];
61 int function = slave_info->sft->function;
62 int address = slave_info->address;
63 int nb = slave_info->nb;
64 int send_index = slave_info->send_index;
65 const agile_modbus_slave_util_map_t *maps = NULL;
66 int nb_maps = 0;
67
68 switch (function) {
69 case AGILE_MODBUS_FC_READ_COILS: {
70 maps = slave_util->tab_bits;
71 nb_maps = slave_util->nb_bits;
72 } break;
73
74 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: {
75 maps = slave_util->tab_input_bits;
76 nb_maps = slave_util->nb_input_bits;
77 } break;
78
79 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: {
80 maps = slave_util->tab_registers;
81 nb_maps = slave_util->nb_registers;
82 } break;
83
84 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: {
85 maps = slave_util->tab_input_registers;
86 nb_maps = slave_util->nb_input_registers;
87 } break;
88
89 default:
90 return -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
91 }
92
93 if (maps == NULL)
94 return 0;
95
96 for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
97 const agile_modbus_slave_util_map_t *map = get_map_by_addr(maps, nb_maps, now_address);
98 if (map == NULL)
99 continue;
100
101 int map_len = map->end_addr - now_address + 1;
102 if (map->get) {
103 memset(map_buf, 0, sizeof(map_buf));
104 map->get(map_buf, sizeof(map_buf));
105 int index = now_address - map->start_addr;
106 int need_len = address + nb - now_address;
107 if (need_len > map_len) {
108 need_len = map_len;
109 }
110
111 if (function == AGILE_MODBUS_FC_READ_COILS || function == AGILE_MODBUS_FC_READ_DISCRETE_INPUTS) {
112 uint8_t *ptr = map_buf;
113 for (int j = 0; j < need_len; j++) {
114 agile_modbus_slave_io_set(ctx->send_buf + send_index, i + j, ptr[index + j]);
115 }
116 } else {
117 uint16_t *ptr = (uint16_t *)map_buf;
118 for (int j = 0; j < need_len; j++) {
119 agile_modbus_slave_register_set(ctx->send_buf + send_index, i + j, ptr[index + j]);
120 }
121 }
122 }
123
124 now_address += map_len - 1;
125 i += map_len - 1;
126 }
127
128 return 0;
129}
130
141static int write_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
142{
143 uint8_t map_buf[AGILE_MODBUS_MAX_PDU_LENGTH];
144 int function = slave_info->sft->function;
145 int address = slave_info->address;
146 int nb = 0;
147 const agile_modbus_slave_util_map_t *maps = NULL;
148 int nb_maps = 0;
149
150 switch (function) {
151 case AGILE_MODBUS_FC_WRITE_SINGLE_COIL:
152 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: {
153 maps = slave_util->tab_bits;
154 nb_maps = slave_util->nb_bits;
155 if (function == AGILE_MODBUS_FC_WRITE_SINGLE_COIL) {
156 nb = 1;
157 } else {
158 nb = slave_info->nb;
159 }
160 } break;
161
162 case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER:
163 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: {
164 maps = slave_util->tab_registers;
165 nb_maps = slave_util->nb_registers;
166 if (function == AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER) {
167 nb = 1;
168 } else {
169 nb = slave_info->nb;
170 }
171 } break;
172
173 default:
174 return -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
175 }
176
177 if (maps == NULL)
178 return 0;
179
180 for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
181 const agile_modbus_slave_util_map_t *map = get_map_by_addr(maps, nb_maps, now_address);
182 if (map == NULL)
183 continue;
184
185 int map_len = map->end_addr - now_address + 1;
186 if (map->set) {
187 memset(map_buf, 0, sizeof(map_buf));
188 if (map->get) {
189 map->get(map_buf, sizeof(map_buf));
190 }
191
192 int index = now_address - map->start_addr;
193 int need_len = address + nb - now_address;
194 if (need_len > map_len) {
195 need_len = map_len;
196 }
197
198 if (function == AGILE_MODBUS_FC_WRITE_SINGLE_COIL || function == AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS) {
199 uint8_t *ptr = map_buf;
200 if (function == AGILE_MODBUS_FC_WRITE_SINGLE_COIL) {
201 int data = *((int *)slave_info->buf);
202 ptr[index] = data;
203 } else {
204 for (int j = 0; j < need_len; j++) {
205 uint8_t data = agile_modbus_slave_io_get(slave_info->buf, i + j);
206 ptr[index + j] = data;
207 }
208 }
209 } else {
210 uint16_t *ptr = (uint16_t *)map_buf;
211 if (function == AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER) {
212 int data = *((int *)slave_info->buf);
213 ptr[index] = data;
214 } else {
215 for (int j = 0; j < need_len; j++) {
216 uint16_t data = agile_modbus_slave_register_get(slave_info->buf, i + j);
217 ptr[index + j] = data;
218 }
219 }
220 }
221
222 int rc = map->set(index, need_len, map_buf, sizeof(map_buf));
223 if (rc != 0)
224 return rc;
225 }
226
227 now_address += map_len - 1;
228 i += map_len - 1;
229 }
230
231 return 0;
232}
233
244static int mask_write_register(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
245{
246 uint8_t map_buf[AGILE_MODBUS_MAX_PDU_LENGTH];
247 int address = slave_info->address;
248 const agile_modbus_slave_util_map_t *maps = slave_util->tab_registers;
249 int nb_maps = slave_util->nb_registers;
250
251 if (maps == NULL)
252 return 0;
253
254 const agile_modbus_slave_util_map_t *map = get_map_by_addr(maps, nb_maps, address);
255 if (map == NULL)
256 return 0;
257
258 if (map->set) {
259 memset(map_buf, 0, sizeof(map_buf));
260 if (map->get) {
261 map->get(map_buf, sizeof(map_buf));
262 }
263
264 int index = address - map->start_addr;
265 uint16_t *ptr = (uint16_t *)map_buf;
266 uint16_t data = ptr[index];
267 uint16_t and = (slave_info->buf[0] << 8) + slave_info->buf[1];
268 uint16_t or = (slave_info->buf[2] << 8) + slave_info->buf[3];
269
270 data = (data & and) | (or &(~and));
271 ptr[index] = data;
272
273 int rc = map->set(index, 1, map_buf, sizeof(map_buf));
274 if (rc != 0)
275 return rc;
276 }
277
278 return 0;
279}
280
291static int write_read_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
292{
293 uint8_t map_buf[AGILE_MODBUS_MAX_PDU_LENGTH];
294 int address = slave_info->address;
295 int nb = (slave_info->buf[0] << 8) + slave_info->buf[1];
296 int address_write = (slave_info->buf[2] << 8) + slave_info->buf[3];
297 int nb_write = (slave_info->buf[4] << 8) + slave_info->buf[5];
298 int send_index = slave_info->send_index;
299
300 const agile_modbus_slave_util_map_t *maps = slave_util->tab_registers;
301 int nb_maps = slave_util->nb_registers;
302
303 if (maps == NULL)
304 return 0;
305
306 /* Write first. 7 is the offset of the first values to write */
307 for (int now_address = address_write, i = 0; now_address < address_write + nb_write; now_address++, i++) {
308 const agile_modbus_slave_util_map_t *map = get_map_by_addr(maps, nb_maps, now_address);
309 if (map == NULL)
310 continue;
311
312 int map_len = map->end_addr - now_address + 1;
313 if (map->set) {
314 memset(map_buf, 0, sizeof(map_buf));
315 if (map->get) {
316 map->get(map_buf, sizeof(map_buf));
317 }
318
319 int index = now_address - map->start_addr;
320 uint16_t *ptr = (uint16_t *)map_buf;
321 int need_len = address_write + nb_write - now_address;
322 if (need_len > map_len) {
323 need_len = map_len;
324 }
325
326 for (int j = 0; j < need_len; j++) {
327 uint16_t data = agile_modbus_slave_register_get(slave_info->buf + 7, i + j);
328 ptr[index + j] = data;
329 }
330
331 int rc = map->set(index, need_len, map_buf, sizeof(map_buf));
332 if (rc != 0)
333 return rc;
334 }
335
336 now_address += map_len - 1;
337 i += map_len - 1;
338 }
339
340 /* and read the data for the response */
341 for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
342 const agile_modbus_slave_util_map_t *map = get_map_by_addr(maps, nb_maps, now_address);
343 if (map == NULL)
344 continue;
345
346 int map_len = map->end_addr - now_address + 1;
347 if (map->get) {
348 memset(map_buf, 0, sizeof(map_buf));
349 map->get(map_buf, sizeof(map_buf));
350 int index = now_address - map->start_addr;
351 uint16_t *ptr = (uint16_t *)map_buf;
352 int need_len = address + nb - now_address;
353 if (need_len > map_len) {
354 need_len = map_len;
355 }
356
357 for (int j = 0; j < need_len; j++) {
358 agile_modbus_slave_register_set(ctx->send_buf + send_index, i + j, ptr[index + j]);
359 }
360 }
361
362 now_address += map_len - 1;
363 i += map_len - 1;
364 }
365
366 return 0;
367}
368
387int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
388{
389 int function = slave_info->sft->function;
390 int ret = 0;
391 const agile_modbus_slave_util_t *slave_util = (const agile_modbus_slave_util_t *)data;
392
393 if (slave_util == NULL)
394 return 0;
395
396 if (slave_util->addr_check) {
397 ret = slave_util->addr_check(ctx, slave_info);
398 if (ret != 0)
399 return ret;
400 }
401
402 switch (function) {
403 case AGILE_MODBUS_FC_READ_COILS:
404 case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS:
405 case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
406 case AGILE_MODBUS_FC_READ_INPUT_REGISTERS:
407 ret = read_registers(ctx, slave_info, slave_util);
408 break;
409
410 case AGILE_MODBUS_FC_WRITE_SINGLE_COIL:
411 case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS:
412 case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER:
413 case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
414 ret = write_registers(ctx, slave_info, slave_util);
415 break;
416
417 case AGILE_MODBUS_FC_MASK_WRITE_REGISTER:
418 ret = mask_write_register(ctx, slave_info, slave_util);
419 break;
420
421 case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS:
422 ret = write_read_registers(ctx, slave_info, slave_util);
423 break;
424
425 default: {
426 if (slave_util->special_function) {
427 ret = slave_util->special_function(ctx, slave_info);
428 } else {
429 ret = -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
430 }
431 } break;
432 }
433
434 if (slave_util->done) {
435 slave_util->done(ctx, slave_info, ret);
436 }
437
438 return ret;
439}
440
agile_modbus.h
Agile Modbus software package common header file.
agile_modbus_slave_util.h
The simple slave access header file provided by the Agile Modbus software package.
AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
Definition: agile_modbus.h:147
AGILE_MODBUS_MAX_PDU_LENGTH
#define AGILE_MODBUS_MAX_PDU_LENGTH
Definition: agile_modbus.h:116
AGILE_MODBUS_FC_MASK_WRITE_REGISTER
#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER
Definition: agile_modbus.h:58
AGILE_MODBUS_FC_READ_COILS
#define AGILE_MODBUS_FC_READ_COILS
Definition: agile_modbus.h:48
AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER
#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER
Definition: agile_modbus.h:53
AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS
Definition: agile_modbus.h:55
AGILE_MODBUS_FC_WRITE_SINGLE_COIL
#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL
Definition: agile_modbus.h:52
AGILE_MODBUS_FC_READ_HOLDING_REGISTERS
#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS
Definition: agile_modbus.h:50
AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS
#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS
Definition: agile_modbus.h:59
AGILE_MODBUS_FC_READ_INPUT_REGISTERS
#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS
Definition: agile_modbus.h:51
AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS
Definition: agile_modbus.h:56
AGILE_MODBUS_FC_READ_DISCRETE_INPUTS
#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS
Definition: agile_modbus.h:49
agile_modbus_slave_util_callback
int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
Definition: agile_modbus_slave_util.c:387
write_registers
static int write_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
write register
Definition: agile_modbus_slave_util.c:141
get_map_by_addr
static const agile_modbus_slave_util_map_t * get_map_by_addr(const agile_modbus_slave_util_map_t *maps, int nb_maps, int address)
Get the mapping object from the mapping object array according to the register address.
Definition: agile_modbus_slave_util.c:37
read_registers
static int read_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
read register
Definition: agile_modbus_slave_util.c:58
mask_write_register
static int mask_write_register(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
mask write register
Definition: agile_modbus_slave_util.c:244
write_read_registers
static int write_read_registers(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const agile_modbus_slave_util_t *slave_util)
Write and read registers.
Definition: agile_modbus_slave_util.c:291
agile_modbus_slave_io_get
uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
Read slave IO status.
Definition: agile_modbus.c:1156
agile_modbus_slave_io_set
void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
slave IO settings
Definition: agile_modbus.c:1139
agile_modbus_slave_register_get
uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
Read slave register data.
Definition: agile_modbus.c:1184
agile_modbus_slave_register_set
void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
slave register settings
Definition: agile_modbus.c:1172
agile_modbus_sft::function
int function
function code
Definition: agile_modbus.h:189
agile_modbus_slave_info
Agile Modbus slave information structure.
Definition: agile_modbus.h:246
agile_modbus_slave_info::buf
uint8_t * buf
Data fields required for different function codes.
Definition: agile_modbus.h:251
agile_modbus_slave_info::send_index
int send_index
Current index of sending buffer.
Definition: agile_modbus.h:252
agile_modbus_slave_info::sft
agile_modbus_sft_t * sft
sft structure pointer
Definition: agile_modbus.h:247
agile_modbus_slave_info::address
int address
Register address.
Definition: agile_modbus.h:249
agile_modbus_slave_info::nb
int nb
number
Definition: agile_modbus.h:250
agile_modbus_slave_util_map
slave register mapping structure
Definition: agile_modbus_slave_util.h:38
agile_modbus_slave_util_map::get
int(* get)(void *buf, int bufsz)
Get register data interface.
Definition: agile_modbus_slave_util.h:41
agile_modbus_slave_util_map::start_addr
int start_addr
Start address.
Definition: agile_modbus_slave_util.h:39
agile_modbus_slave_util_map::end_addr
int end_addr
end address
Definition: agile_modbus_slave_util.h:40
agile_modbus_slave_util_map::set
int(* set)(int index, int len, void *buf, int bufsz)
Set register data interface.
Definition: agile_modbus_slave_util.h:42
agile_modbus_slave_util
slave function structure
Definition: agile_modbus_slave_util.h:48
agile_modbus_slave_util::addr_check
int(* addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Address checking interface.
Definition: agile_modbus_slave_util.h:57
agile_modbus_slave_util::nb_input_registers
int nb_input_registers
Input register definition array number.
Definition: agile_modbus_slave_util.h:56
agile_modbus_slave_util::nb_bits
int nb_bits
The number of coil register definition arrays.
Definition: agile_modbus_slave_util.h:50
agile_modbus_slave_util::tab_input_registers
const agile_modbus_slave_util_map_t * tab_input_registers
Input register definition array.
Definition: agile_modbus_slave_util.h:55
agile_modbus_slave_util::special_function
int(* special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Special function code processing interface.
Definition: agile_modbus_slave_util.h:58
agile_modbus_slave_util::nb_input_bits
int nb_input_bits
The number of discrete input register definition arrays.
Definition: agile_modbus_slave_util.h:52
agile_modbus_slave_util::tab_input_bits
const agile_modbus_slave_util_map_t * tab_input_bits
Discrete input register definition array.
Definition: agile_modbus_slave_util.h:51
agile_modbus_slave_util::done
int(* done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)
Processing end interface.
Definition: agile_modbus_slave_util.h:59
agile_modbus_slave_util::nb_registers
int nb_registers
Number of holding register definition arrays.
Definition: agile_modbus_slave_util.h:54
agile_modbus_slave_util::tab_bits
const agile_modbus_slave_util_map_t * tab_bits
Coil register definition array.
Definition: agile_modbus_slave_util.h:49
agile_modbus_slave_util::tab_registers
const agile_modbus_slave_util_map_t * tab_registers
Holding register definition array.
Definition: agile_modbus_slave_util.h:53
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217
agile_modbus::send_buf
uint8_t * send_buf
Send buffer.
Definition: agile_modbus.h:219


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util/agile_modbus_slave_util.h Source File

  • util
agile_modbus_slave_util.h
Go to the documentation of this file.
1
14#ifndef __PKG_AGILE_MODBUS_SLAVE_UTIL_H
15#define __PKG_AGILE_MODBUS_SLAVE_UTIL_H
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include <stdint.h>
22
38typedef struct agile_modbus_slave_util_map {
39 int start_addr;
40 int end_addr;
41 int (*get)(void *buf, int bufsz);
42 int (*set)(int index, int len, void *buf, int bufsz);
43} agile_modbus_slave_util_map_t;
44
48typedef struct agile_modbus_slave_util {
49 const agile_modbus_slave_util_map_t *tab_bits;
50 int nb_bits;
51 const agile_modbus_slave_util_map_t *tab_input_bits;
52 int nb_input_bits;
53 const agile_modbus_slave_util_map_t *tab_registers;
54 int nb_registers;
55 const agile_modbus_slave_util_map_t *tab_input_registers;
56 int nb_input_registers;
57 int (*addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info);
58 int (*special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info);
59 int (*done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret);
60} agile_modbus_slave_util_t;
61
69int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data);
82#ifdef __cplusplus
83}
84#endif
85
86#endif /* __PKG_AGILE_MODBUS_SLAVE_UTIL_H */
agile_modbus_slave_util_callback
int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
Definition: agile_modbus_slave_util.c:387
agile_modbus_slave_util_t
struct agile_modbus_slave_util agile_modbus_slave_util_t
slave function structure
agile_modbus_slave_util_map_t
struct agile_modbus_slave_util_map agile_modbus_slave_util_map_t
slave register mapping structure
agile_modbus_slave_info
Agile Modbus slave information structure.
Definition: agile_modbus.h:246
agile_modbus_slave_util_map
slave register mapping structure
Definition: agile_modbus_slave_util.h:38
agile_modbus_slave_util_map::get
int(* get)(void *buf, int bufsz)
Get register data interface.
Definition: agile_modbus_slave_util.h:41
agile_modbus_slave_util_map::start_addr
int start_addr
Start address.
Definition: agile_modbus_slave_util.h:39
agile_modbus_slave_util_map::end_addr
int end_addr
end address
Definition: agile_modbus_slave_util.h:40
agile_modbus_slave_util_map::set
int(* set)(int index, int len, void *buf, int bufsz)
Set register data interface.
Definition: agile_modbus_slave_util.h:42
agile_modbus_slave_util
slave function structure
Definition: agile_modbus_slave_util.h:48
agile_modbus_slave_util::addr_check
int(* addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Address checking interface.
Definition: agile_modbus_slave_util.h:57
agile_modbus_slave_util::nb_input_registers
int nb_input_registers
Input register definition array number.
Definition: agile_modbus_slave_util.h:56
agile_modbus_slave_util::nb_bits
int nb_bits
The number of coil register definition arrays.
Definition: agile_modbus_slave_util.h:50
agile_modbus_slave_util::tab_input_registers
const agile_modbus_slave_util_map_t * tab_input_registers
Input register definition array.
Definition: agile_modbus_slave_util.h:55
agile_modbus_slave_util::special_function
int(* special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Special function code processing interface.
Definition: agile_modbus_slave_util.h:58
agile_modbus_slave_util::nb_input_bits
int nb_input_bits
The number of discrete input register definition arrays.
Definition: agile_modbus_slave_util.h:52
agile_modbus_slave_util::tab_input_bits
const agile_modbus_slave_util_map_t * tab_input_bits
Discrete input register definition array.
Definition: agile_modbus_slave_util.h:51
agile_modbus_slave_util::done
int(* done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)
Processing end interface.
Definition: agile_modbus_slave_util.h:59
agile_modbus_slave_util::nb_registers
int nb_registers
Number of holding register definition arrays.
Definition: agile_modbus_slave_util.h:54
agile_modbus_slave_util::tab_bits
const agile_modbus_slave_util_map_t * tab_bits
Coil register definition array.
Definition: agile_modbus_slave_util.h:49
agile_modbus_slave_util::tab_registers
const agile_modbus_slave_util_map_t * tab_registers
Holding register definition array.
Definition: agile_modbus_slave_util.h:53
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src/agile_modbus_tcp.c Source File

  • src
agile_modbus_tcp.c
Go to the documentation of this file.
1
14#include "agile_modbus.h"
15
16#if AGILE_MODBUS_USING_TCP
17
18#include "agile_modbus_tcp.h"
19
34static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
35{
36 ctx->slave = slave;
37 return 0;
38}
39
49static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function,
50 int addr, int nb,
51 uint8_t *req)
52{
53 agile_modbus_tcp_t *ctx_tcp = ctx->backend_data;
54
55 /* Increase transaction ID */
56 if (ctx_tcp->t_id < UINT16_MAX)
57 ctx_tcp->t_id++;
58 else
59 ctx_tcp->t_id = 0;
60 req[0] = ctx_tcp->t_id >> 8;
61 req[1] = ctx_tcp->t_id & 0x00ff;
62
63 /* Protocol Modbus */
64 req[2] = 0;
65 req[3] = 0;
66
67 /* Length will be defined later by set_req_length_tcp at offsets 4
68 and 5 */
69
70 req[6] = ctx->slave;
71 req[7] = function;
72 req[8] = addr >> 8;
73 req[9] = addr & 0x00ff;
74 req[10] = nb >> 8;
75 req[11] = nb & 0x00ff;
76
77 return AGILE_MODBUS_TCP_PRESET_REQ_LENGTH;
78}
79
86static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
87{
88 /* Extract from MODBUS Messaging on TCP/IP Implementation
89 Guide V1.0b (page 23/46):
90 The transaction identifier is used to associate the future
91 response with the request. */
92 rsp[0] = sft->t_id >> 8;
93 rsp[1] = sft->t_id & 0x00ff;
94
95 /* Protocol Modbus */
96 rsp[2] = 0;
97 rsp[3] = 0;
98
99 /* Length will be set later by send_msg (4 and 5) */
100
101 /* The slave ID is copied from the indication */
102 rsp[6] = sft->slave;
103 rsp[7] = sft->function;
104
105 return AGILE_MODBUS_TCP_PRESET_RSP_LENGTH;
106}
107
114static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
115{
116 return (req[0] << 8) + req[1];
117}
118
125static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
126{
127 /* Substract the header length to the message length */
128 int mbap_length = req_length - 6;
129
130 req[4] = mbap_length >> 8;
131 req[5] = mbap_length & 0x00FF;
132
133 return req_length;
134}
135
143static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
144{
145 return msg_length;
146}
147
156static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
157 const uint8_t *rsp, int rsp_length)
158{
159 /* Check transaction ID */
160 if (req[0] != rsp[0] || req[1] != rsp[1])
161 return -1;
162
163 /* Check protocol ID */
164 if (rsp[2] != 0x0 && rsp[3] != 0x0)
165 return -1;
166
167 return 0;
168}
169
181static const agile_modbus_backend_t agile_modbus_tcp_backend =
182 {
183 AGILE_MODBUS_BACKEND_TYPE_TCP,
184 AGILE_MODBUS_TCP_HEADER_LENGTH,
185 AGILE_MODBUS_TCP_CHECKSUM_LENGTH,
186 AGILE_MODBUS_TCP_MAX_ADU_LENGTH,
187 agile_modbus_tcp_set_slave,
188 agile_modbus_tcp_build_request_basis,
189 agile_modbus_tcp_build_response_basis,
190 agile_modbus_tcp_prepare_response_tid,
191 agile_modbus_tcp_send_msg_pre,
192 agile_modbus_tcp_check_integrity,
193 agile_modbus_tcp_pre_check_confirmation};
194
212int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
213{
214 agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
215 ctx->_ctx.backend = &agile_modbus_tcp_backend;
216 ctx->_ctx.backend_data = ctx;
217
218 ctx->t_id = 0;
219
220 return 0;
221}
222
231#endif /* AGILE_MODBUS_USING_TCP */
agile_modbus.h
Agile Modbus software package common header file.
agile_modbus_tcp.h
Agile Modbus package TCP header file.
agile_modbus_common_init
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
Definition: agile_modbus.c:257
AGILE_MODBUS_BACKEND_TYPE_TCP
@ AGILE_MODBUS_BACKEND_TYPE_TCP
TCP.
Definition: agile_modbus.h:166
AGILE_MODBUS_TCP_CHECKSUM_LENGTH
#define AGILE_MODBUS_TCP_CHECKSUM_LENGTH
Definition: agile_modbus_tcp.h:36
AGILE_MODBUS_TCP_PRESET_RSP_LENGTH
#define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH
Definition: agile_modbus_tcp.h:34
AGILE_MODBUS_TCP_MAX_ADU_LENGTH
#define AGILE_MODBUS_TCP_MAX_ADU_LENGTH
Definition: agile_modbus_tcp.h:45
AGILE_MODBUS_TCP_PRESET_REQ_LENGTH
#define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH
Definition: agile_modbus_tcp.h:33
AGILE_MODBUS_TCP_HEADER_LENGTH
#define AGILE_MODBUS_TCP_HEADER_LENGTH
Definition: agile_modbus_tcp.h:32
agile_modbus_tcp_init
int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
TCP initialization.
Definition: agile_modbus_tcp.c:212
agile_modbus_tcp_backend
static const agile_modbus_backend_t agile_modbus_tcp_backend
TCP backend interface.
Definition: agile_modbus_tcp.c:181
agile_modbus_tcp_pre_check_confirmation
static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req, const uint8_t *rsp, int rsp_length)
TCP pre-check confirmation interface (compare transaction identifier and protocol identifier)
Definition: agile_modbus_tcp.c:156
agile_modbus_tcp_set_slave
static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
TCP set address interface.
Definition: agile_modbus_tcp.c:34
agile_modbus_tcp_check_integrity
static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
TCP check receiving data integrity interface.
Definition: agile_modbus_tcp.c:143
agile_modbus_tcp_send_msg_pre
static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
TCP pre-send data interface (calculate the value of the length field and store it)
Definition: agile_modbus_tcp.c:125
agile_modbus_tcp_build_request_basis
static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
TCP builds the basic request message interface (header message)
Definition: agile_modbus_tcp.c:49
agile_modbus_tcp_build_response_basis
static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
TCP builds a basic response message interface (header message)
Definition: agile_modbus_tcp.c:86
agile_modbus_tcp_prepare_response_tid
static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
TCP ready response interface.
Definition: agile_modbus_tcp.c:114
agile_modbus_backend
Agile Modbus backend interface structure.
Definition: agile_modbus.h:198
agile_modbus_sft
contains the modbus header parameter structure
Definition: agile_modbus.h:187
agile_modbus_sft::slave
int slave
slave address
Definition: agile_modbus.h:188
agile_modbus_sft::t_id
int t_id
Transaction identifier.
Definition: agile_modbus.h:190
agile_modbus_sft::function
int function
function code
Definition: agile_modbus.h:189
agile_modbus_tcp
TCP structure.
Definition: agile_modbus_tcp.h:57
agile_modbus_tcp::_ctx
agile_modbus_t _ctx
modbus handle
Definition: agile_modbus_tcp.h:58
agile_modbus_tcp::t_id
uint16_t t_id
Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b (page 23/46): The transaction iden...
Definition: agile_modbus_tcp.h:59
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217
agile_modbus::backend
const agile_modbus_backend_t * backend
Backend interface.
Definition: agile_modbus.h:227
agile_modbus::slave
int slave
slave address
Definition: agile_modbus.h:218
agile_modbus::backend_data
void * backend_data
Backend data, pointing to RTU or TCP structure.
Definition: agile_modbus.h:228


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/inc/agile_modbus_tcp.h Source File

  • inc
agile_modbus_tcp.h
Go to the documentation of this file.
1
14#ifndef __PKG_AGILE_MODBUS_TCP_H
15#define __PKG_AGILE_MODBUS_TCP_H
16
17#if AGILE_MODBUS_USING_TCP
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include <stdint.h>
24
32#define AGILE_MODBUS_TCP_HEADER_LENGTH 7
33#define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12
34#define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8
35
36#define AGILE_MODBUS_TCP_CHECKSUM_LENGTH 0
37
45#define AGILE_MODBUS_TCP_MAX_ADU_LENGTH 260
57typedef struct agile_modbus_tcp {
58 agile_modbus_t _ctx;
59 uint16_t t_id;
63} agile_modbus_tcp_t;
64
72int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
81#ifdef __cplusplus
82}
83#endif
84
85#endif /* AGILE_MODBUS_USING_TCP */
86
87#endif /* __PKG_AGILE_MODBUS_TCP_H */
agile_modbus_tcp_init
int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
TCP initialization.
Definition: agile_modbus_tcp.c:212
agile_modbus_tcp_t
struct agile_modbus_tcp agile_modbus_tcp_t
TCP structure.
agile_modbus_tcp
TCP structure.
Definition: agile_modbus_tcp.h:57
agile_modbus_tcp::_ctx
agile_modbus_t _ctx
modbus handle
Definition: agile_modbus_tcp.h:58
agile_modbus_tcp::t_id
uint16_t t_id
Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b (page 23/46): The transaction iden...
Definition: agile_modbus_tcp.h:59
agile_modbus
Agile Modbus structure.
Definition: agile_modbus.h:217


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/src -> inc Relation

  • src

src → inc Relation

File in src | Includes file in inc
agile_modbus.c | agile_modbus.h
agile_modbus_rtu.c | agile_modbus.h
agile_modbus_rtu.c | agile_modbus_rtu.h
agile_modbus_tcp.c | agile_modbus.h
agile_modbus_tcp.c | agile_modbus_tcp.h


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/util -> inc Relation

  • util

util → inc Relation

File in util | Includes file in inc
agile_modbus_slave_util.c | agile_modbus.h


Agile Modbus: C:/Users/25440/Desktop/agile_modbus/examples/README.md File Reference

C:/Users/25440/Desktop/agile_modbus/examples/README.md File Reference


Agile Modbus: Graph Legend

Graph Legend

This page explains how to interpret the graphs that are generated by doxygen.

Consider the following example:

/*! Invisible class because of truncation */
class Invisible { };
/*! Truncated class, inheritance relation is hidden */
class Truncated : public Invisible { };
/* Class not documented with doxygen comments */
class Undocumented { };
/*! Class that is inherited using public inheritance */
class PublicBase : public Truncated { };
/*! A template class */
template<class T> class Templ { };
/*! Class that is inherited using protected inheritance */
class ProtectedBase { };
/*! Class that is inherited using private inheritance */
class PrivateBase { };
/*! Class that is used by the Inherited class */
class Used { };
/*! Super class that inherits a number of other classes */
class Inherited : public PublicBase,
protected ProtectedBase,
private PrivateBase,
public Undocumented,
public Templ<int>
{
private:
Used *m_usedClass;
};

This will result in the following graph:

The boxes in the above graph have the following meaning:

  • A filled gray box represents the struct or class for which the graph is generated.
  • A box with a black border denotes a documented struct or class.
  • A box with a gray border denotes an undocumented struct or class.
  • A box with a red border denotes a documented struct or class forwhich not all inheritance/containment relations are shown. A graph is truncated if it does not fit within the specified boundaries.

The arrows have the following meaning:

  • A dark blue arrow is used to visualize a public inheritance relation between two classes.
  • A dark green arrow is used for protected inheritance.
  • A dark red arrow is used for private inheritance.
  • A purple dashed arrow is used if a class is contained or used by another class. The arrow is labelled with the variable(s) through which the pointed class or struct is accessible.
  • A yellow dashed arrow denotes a relation between a template instance and the template class it was instantiated from. The arrow is labelled with the template parameters of the instance.


Agile Modbus: Related Pages

Related Pages
Here is a list of all related documentation pages:
Update record
Example description