Modbus function interface and programming process

functional interface

modbus_t* modbus_new_tcp(const char *ip, int port)

Function: Create a Modbus instance in TCP mode and initialize it

parameter:

ip: ip address

port: port number

Return value: Success: Modbus instance

Failed: NULL

int modbus_set_slave(modbus_t *ctx, int slave)

Function: set slave ID

parameter:

ctx: Modbus instance

slave: slave ID

Return Value: Success: 0

Failed: - 1

int modbus_connect(modbus_t *ctx)

Function: establish a connection with the slave (slave)

parameter:

ctx: Modbus instance

Return Value: Success: 0

Failed: - 1

void modbus_free(modbus_t *ctx)

Function: release Modbus instance

Parameters: ctx: Modbus instance

void modbus_close(modbus_t *ctx)

Function: close socket

Parameters: ctx: Modbus instance

int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)

Function: read coil status, can read the status of multiple continuous coils (corresponding function code is 0x01 )

parameter:

ctx: Modbus instance

addr: register start address

nb : number of registers

dest : the obtained state value

int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)

Function: read input status, can read the status of multiple continuous inputs (corresponding function code is 0x02 )

parameter:

ctx: Modbus instance

addr: register start address

nb : number of registers

dest : the obtained state value

Return value: success: return the value of nb

int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)

Function: Read the value of the holding register, and can read the values ​​of multiple continuous holding registers (the corresponding function code is 0x03 )

parameter:

ctx: Modbus instance

addr: register start address

nb : number of registers

dest: the value of the obtained register

Return value: success: the number of registers read

Failed: - 1

int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)

Function: read the value of the input register, can read the value of multiple continuous input registers (the corresponding function code is 0x04 )

parameter:

ctx: Modbus instance

addr: register start address

nb : number of registers

dest: the value of the obtained register

Return value: success: the number of registers read

Failed: - 1

int modbus_write_bit(modbus_t *ctx, int addr, int status);

Function: Write the state of a single coil (the corresponding function code is 0x05 )

parameter:

ctx: Modbus instance

addr: coil address

status: coil status

Return Value: Success: 0

Failed: - 1

int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *src);

Function: Write the state of multiple continuous coils (the corresponding function code is 15 )

parameter:

ctx: Modbus instance

addr: coil address

nb : number of coils

src : multiple coil states

Return Value: Success: 0

Failed: - 1

int modbus_write_register(modbus_t *ctx, int addr, int value);

Function: Write a single register (corresponding function code is 0x06 )

parameter:

ctx: Modbus instance

addr: register address

value : the value of the register

Return Value: Success: 0

Failed: - 1

int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *src);

Function: Write multiple consecutive registers (corresponding function code is 16 )

parameter:

ctx: Modbus instance

addr: register address

nb : the number of registers

src : the value of multiple registers

Return Value: Success: 0

Failed: - 1

programming process

1. Create an instance

modbus_new_tcp

2. Set the slave ID

modbus_set_slave

3. Connect with the slave

modbus_connect

4. Register operation

Function code corresponding function

5. Close the socket

modbus_close

6. Release the instance

modbus_free

 

Guess you like

Origin blog.csdn.net/m0_68672255/article/details/130413885