39 for (
int i = 0; i < nb_maps; i++) {
41 if (address >= map->
start_addr && address <= map->end_addr)
62 int address = slave_info->
address;
63 int nb = slave_info->
nb;
96 for (
int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
101 int map_len = map->
end_addr - now_address + 1;
103 memset(map_buf, 0,
sizeof(map_buf));
104 map->
get(map_buf,
sizeof(map_buf));
106 int need_len = address + nb - now_address;
107 if (need_len > map_len) {
112 uint8_t *ptr = map_buf;
113 for (
int j = 0; j < need_len; j++) {
117 uint16_t *ptr = (uint16_t *)map_buf;
118 for (
int j = 0; j < need_len; j++) {
124 now_address += map_len - 1;
145 int address = slave_info->
address;
180 for (
int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
185 int map_len = map->
end_addr - now_address + 1;
187 memset(map_buf, 0,
sizeof(map_buf));
189 map->
get(map_buf,
sizeof(map_buf));
193 int need_len = address + nb - now_address;
194 if (need_len > map_len) {
199 uint8_t *ptr = map_buf;
201 int data = *((
int *)slave_info->
buf);
204 for (
int j = 0; j < need_len; j++) {
206 ptr[index + j] = data;
210 uint16_t *ptr = (uint16_t *)map_buf;
212 int data = *((
int *)slave_info->
buf);
215 for (
int j = 0; j < need_len; j++) {
217 ptr[index + j] = data;
222 int rc = map->
set(index, need_len, map_buf,
sizeof(map_buf));
227 now_address += map_len - 1;
247 int address = slave_info->
address;
259 memset(map_buf, 0,
sizeof(map_buf));
261 map->
get(map_buf,
sizeof(map_buf));
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];
270 data = (data & and) | (or &(~and));
273 int rc = map->
set(index, 1, map_buf,
sizeof(map_buf));
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];
307 for (
int now_address = address_write, i = 0; now_address < address_write + nb_write; now_address++, i++) {
312 int map_len = map->
end_addr - now_address + 1;
314 memset(map_buf, 0,
sizeof(map_buf));
316 map->
get(map_buf,
sizeof(map_buf));
320 uint16_t *ptr = (uint16_t *)map_buf;
321 int need_len = address_write + nb_write - now_address;
322 if (need_len > map_len) {
326 for (
int j = 0; j < need_len; j++) {
328 ptr[index + j] = data;
331 int rc = map->
set(index, need_len, map_buf,
sizeof(map_buf));
336 now_address += map_len - 1;
341 for (
int now_address = address, i = 0; now_address < address + nb; now_address++, i++) {
346 int map_len = map->
end_addr - now_address + 1;
348 memset(map_buf, 0,
sizeof(map_buf));
349 map->
get(map_buf,
sizeof(map_buf));
351 uint16_t *ptr = (uint16_t *)map_buf;
352 int need_len = address + nb - now_address;
353 if (need_len > map_len) {
357 for (
int j = 0; j < need_len; j++) {
362 now_address += map_len - 1;
393 if (slave_util == NULL)
397 ret = slave_util->
addr_check(ctx, slave_info);
434 if (slave_util->
done) {
435 slave_util->
done(ctx, slave_info, ret);
Agile Modbus software package common header file.
The simple slave access header file provided by the Agile Modbus software package.
@ AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION
#define AGILE_MODBUS_MAX_PDU_LENGTH
#define AGILE_MODBUS_FC_MASK_WRITE_REGISTER
#define AGILE_MODBUS_FC_READ_COILS
#define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS
#define AGILE_MODBUS_FC_WRITE_SINGLE_COIL
#define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS
#define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS
#define AGILE_MODBUS_FC_READ_INPUT_REGISTERS
#define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS
#define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS
int agile_modbus_slave_util_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, const void *data)
Slave callback function.
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.
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
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
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
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.
uint8_t agile_modbus_slave_io_get(uint8_t *buf, int index)
Read slave IO status.
void agile_modbus_slave_io_set(uint8_t *buf, int index, int status)
slave IO settings
uint16_t agile_modbus_slave_register_get(uint8_t *buf, int index)
Read slave register data.
void agile_modbus_slave_register_set(uint8_t *buf, int index, uint16_t data)
slave register settings
int function
function code
Agile Modbus slave information structure.
uint8_t * buf
Data fields required for different function codes.
int send_index
Current index of sending buffer.
agile_modbus_sft_t * sft
sft structure pointer
int address
Register address.
slave register mapping structure
int(* get)(void *buf, int bufsz)
Get register data interface.
int start_addr
Start address.
int(* set)(int index, int len, void *buf, int bufsz)
Set register data interface.
int(* addr_check)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Address checking interface.
int nb_input_registers
Input register definition array number.
int nb_bits
The number of coil register definition arrays.
const agile_modbus_slave_util_map_t * tab_input_registers
Input register definition array.
int(* special_function)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info)
Special function code processing interface.
int nb_input_bits
The number of discrete input register definition arrays.
const agile_modbus_slave_util_map_t * tab_input_bits
Discrete input register definition array.
int(* done)(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info, int ret)
Processing end interface.
int nb_registers
Number of holding register definition arrays.
const agile_modbus_slave_util_map_t * tab_bits
Coil register definition array.
const agile_modbus_slave_util_map_t * tab_registers
Holding register definition array.
uint8_t * send_buf
Send buffer.