pub struct Frame { /* private fields */ }
Expand description

Modbus Frame

Implementations

Create a TCP frame

A Modbus variant used for communications over TCP/IP networks.

Examples
use easy_modbus::Frame;
let tcp = Frame::tcp();

Create a RTU frame

Used in serial communication, and is the most common implementation available for Modbus.

Examples
use easy_modbus::Frame;
let rut = Frame::rtu();

Create a read coils request (Function Code: 0x01)

  • unit_id - Server address
  • first_address - Address of first coil to read
  • number - Number of coils to read
Examples
use easy_modbus::Frame;
let request = Frame::tcp().read_coils_request(0x01, 0x02, 0x08);

Create a read discrete Request (Function Code: 0x02)

  • unit_id - Server address
  • first_address - Address of first discrete input to read
  • number - Number of discrete input to read
Examples
use easy_modbus::Frame;
let request = Frame::tcp().read_discrete_request(0x0B, 0x007A, 0x001C);

Create a read multiple holding registers request (Function Code: 0x03)

  • unit_id - Server address
  • first_address - Address of first register to read
  • number - Number of discrete input to read
Examples
use easy_modbus::Frame;
let request = Frame::tcp().read_multiple_holding_registers_request(0x0B, 0x006F, 0x0003);

Create a read input registers request (Function Code: 0x04)

  • unit_id - Server address
  • first_address - Address of first register to read
  • number - Number of registers to read
Examples
use easy_modbus::Frame;
let request = Frame::tcp().read_input_registers_request(0x0B, 0x000A, 0x0001);

Create a write single coil request (Function Code: 0x05)

  • unit_id - Server address
  • address - Address of coil to write
  • value - Value to write. 0 (0x0000) for off, 65,280 (0xFF00) for on.
Examples
use easy_modbus::Frame;
let request = Frame::tcp().write_single_coil_request(0x0B, 0x00BF, 0x0000);

Create a write single holding register request (Function Code: 0x06)

  • unit_id - Server address
  • address - Address of Holding Register to write
  • value - Value to write
Examples
use easy_modbus::Frame;
let request = Frame::tcp().write_single_holding_register_request(0x0B, 0x0004, 0xABCD);

Create a write multiple coils request (Function Code: 0x0F)

  • unit_id - Server address
  • address - Address of Holding Register to write
  • coils_number - Number of coils to write
  • values - Coil values. Value of each coil is binary (0 for off, 1 for on).
Examples
use easy_modbus::Frame;
let request = Frame::tcp().write_multiple_coils_request(
    0x0B,
    0x001B,
    0x0009,
    vec![0x4D, 0x01]
);

Create a write multiple coils request (Function Code: 0x10)

  • unit_id - Server address
  • address - Address of first holding registers to write
  • values - New values of holding registers
Examples
use easy_modbus::Frame;
let request = Frame::tcp().write_multiple_holding_registers_request(
    0x0B,
    0x0012,
    vec![0x0B, 0x0A, 0xC1, 0x02],
);

Create a read coils response (Function Code: 0x01)

  • unit_id - Server address
  • values - Coil input values, Values of each coil input is binary (0 for off, 1 for on). First requested coil input is as least significant bit of first byte in reply. If number of coils inputs is not a multiple of 8, most significant bits of last byte will be stuffed zeros.
Examples
use easy_modbus::Frame;
let response = Frame::tcp().read_coils_response(0x0B, vec![0xCD, 0x6B, 0xB2, 0x7F]);

Create a read discrete response (Function Code: 0x02)

  • unit_id - Server address
  • values - Discrete input values
Examples
use easy_modbus::Frame;
let response = Frame::tcp().read_discrete_response(0x0B, vec![0xAC, 0xDB, 0xFB, 0x0D]);

Create a read holding register response (Function Code: 0x03)

  • unit_id - Server address
  • values - Discrete input values
Examples
use easy_modbus::Frame;
let response = Frame::tcp().read_discrete_response(0x0B, vec![0xAC, 0xDB, 0xFB, 0x0D]);

Create a read input register response (Function Code: 0x04)

  • unit_id - Server address
  • values - Register values
Examples
use easy_modbus::Frame;
let response = Frame::tcp().read_input_register_response(0x01, vec![0x10, 0x2F]);

Create a write single coil response (Function Code: 0x05)

  • unit_id - Server address
  • values - Register values
Examples
use easy_modbus::Frame;
let response = Frame::tcp().write_single_coil_response(0x01, 0x00BF, 0x0000);

Create a write single coil response (Function Code: 0x06)

  • unit_id - Server address
  • address - Address of coil
  • value - Value to write. 0 (0x0000) for off, 65,280 (0xFF00) for on.
Examples
use easy_modbus::Frame;
let response = Frame::tcp().write_single_holding_register_response(0x01, 0x0004, 0xFF00);

Create a write multiple coils response (Function Code: 0x0F)

  • unit_id - Server address
  • address - Address of first written holding register
  • coils_number - Number of written holding registers
Examples
use easy_modbus::Frame;
let response = Frame::tcp().write_multiple_coils_response(0x01, 0x001B, 0x0009);

Create a write multiple holding registers response (Function Code: 0x10)

  • unit_id - Server address
  • address - Address of first written holding register
  • coils_number - Number of written holding registers
Examples
use easy_modbus::Frame;
let response = Frame::tcp().write_multiple_holding_registers_response(0x01, 0x0012, 0x0002);

Create a exception response

  • unit_id - Server address
  • function - Modbus Function enum
  • exception - Modbus Exception enum
Examples
use easy_modbus::{Exception, Frame, Function};
let response = Frame::tcp().exception_response(
    0x0A,
    Function::ReadCoils,
    Exception::IllegalDataAddress,
);

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more