[−][src]Trait libmodbus::ModbusClient
The Modbus protocol defines different data types and functions to read and write them from/to remote devices. The following functions are used by the clients to send Modbus requests:
- Read data
- Write data
- Write and read data
- Raw requests
- Reply an exception
Required methods
fn read_bits(
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
fn read_input_bits(
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
fn read_registers(
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
fn read_input_registers(
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
fn report_slave_id(
&self,
max_dest: usize,
dest: &mut [u8]
) -> Result<u16, Error>
&self,
max_dest: usize,
dest: &mut [u8]
) -> Result<u16, Error>
fn write_bit(&self, address: u16, status: bool) -> Result<(), Error>
fn write_bits(&self, address: u16, num: u16, src: &[u8]) -> Result<u16, Error>
fn write_register(&self, address: u16, value: u16) -> Result<(), Error>
fn write_registers(
&self,
address: u16,
num: u16,
src: &[u16]
) -> Result<u16, Error>
&self,
address: u16,
num: u16,
src: &[u16]
) -> Result<u16, Error>
fn write_and_read_registers(
&self,
write_address: u16,
write_num: u16,
src: &[u16],
read_address: u16,
read_num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
&self,
write_address: u16,
write_num: u16,
src: &[u16],
read_address: u16,
read_num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
fn mask_write_register(
&self,
address: u16,
and_mask: u16,
or_mask: u16
) -> Result<(), Error>
&self,
address: u16,
and_mask: u16,
or_mask: u16
) -> Result<(), Error>
fn send_raw_request(
&self,
raw_request: &mut [u8],
lenght: usize
) -> Result<u16, Error>
&self,
raw_request: &mut [u8],
lenght: usize
) -> Result<u16, Error>
fn receive_confirmation(&self, response: &mut [u8]) -> Result<u16, Error>
Implementors
impl ModbusClient for Modbus
[src]
fn read_bits(
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
[src]
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
read_bits
- read many bits
The read_bits()
function shall read the status of the num
bits (coils) to the
address
of the remote device. The result of reading is stored in dest
slice as unsigned bytes (8 bits) set
to TRUE or FALSE.
The function uses the Modbus function code 0x01 (read coil status).
Return value
The function returns a Result
containing the number of read bits if successful. Otherwise it returns an Error.
Parameters
address
- address of the remote devicenum
- number of coils to readdest
- the result of the reading is stored here
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut dest = vec![0u8; 100]; assert!(modbus.read_bits(0, 1, &mut dest).is_ok());
fn read_input_bits(
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
[src]
&self,
address: u16,
num: u16,
dest: &mut [u8]
) -> Result<u16, Error>
read_input_bits
- read many input bits
The read_input_bits()
function shall read the content of the num
input bits to
the address
of the remote device. The result of reading is stored in dest
slice as unsigned bytes (8 bits)
set to TRUE or FALSE.
The function uses the Modbus function code 0x02 (read input status).
Return value
The function returns a Result
containing the number of read bits if successful. Otherwise it returns an Error.
Parameters
address
- address of the remote devicenum
- number of input bits to readdest
- the result of the reading is stored here
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut dest = vec![0u8; 100]; assert!(modbus.read_input_bits(0, 1, &mut dest).is_ok());
fn read_registers(
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
[src]
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
read_registers
- read many registers
The read_registers()
function shall read the content of the num
holding registers
to the address
of the remote device. The result of reading is stored in dest
slice as u16 word values.
The function uses the Modbus function code 0x03 (read holding registers).
Return value
The function returns a Result
containing the number of read bits if successful. Otherwise it returns an Error.
Parameters
address
- address of the remote devicenum
- number of holding registers to readdest
- the result of the reading is stored here
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut dest = vec![0u16; 100]; assert!(modbus.read_registers(0, 1, &mut dest).is_ok());
fn read_input_registers(
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
[src]
&self,
address: u16,
num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
read_input_registers
- read many input registers
The read_input_registers()
function shall read the content of the num
holding registers to the address
of the remote device. The result of reading is stored in dest
slice as u16
word values.
The function uses the Modbus function code 0x04 (read input registers). The holding registers and input registers have different historical meaning, but nowadays it’s more common to use holding registers only.
Return value
The function returns a Result
containing the number of read bits if successful. Otherwise it returns an Error.
Parameters
address
- address of the remote devicenum
- number of input registers to readdest
- the result of the reading is stored here
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut dest = vec![0u16; 100]; assert!(modbus.read_input_registers(0, 1, &mut dest).is_ok());
fn report_slave_id(
&self,
max_dest: usize,
dest: &mut [u8]
) -> Result<u16, Error>
[src]
&self,
max_dest: usize,
dest: &mut [u8]
) -> Result<u16, Error>
report_slave_id
- returns a description of the controller
The report_slave_id()
function shall send a request to the controller to obtain a
description of the controller. The response stored in dest
contains:
* the slave ID, this unique ID is in reality not unique at all so it's not possible to depend on it to know
how the information are packed in the response.
* the run indicator status (0x00 = OFF, 0xFF = ON)
* additional data specific to each controller. For example, libmodbus returns the version of the library as
a string.
Return value
The function returns a Result
containing the number of read bits if successful. If the output was truncated
due the max_dest
limit then the return value is the number of bytes which would have been written to dest
.
Thus, a return value greater than the max_dest
means that the resonse data was truncated.
Otherwise the Result contains an Error.
Parameters
max_dest
- limit, writemax_dest
bytes from the response todest
dest
- the result of the reading is stored here
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut bytes = vec![0u8; Modbus::MAX_PDU_LENGTH]; assert!(modbus.report_slave_id(Modbus::MAX_PDU_LENGTH, &mut bytes).is_ok()); // assert_eq!(bytes, vec![180, 255, 76, 77, 66, 51, 46, 49, 46, 52]));
fn write_bit(&self, address: u16, status: bool) -> Result<(), Error>
[src]
write_bit
- write a single bit
The write_bit()
function shall write the status
at the address
of the remote device.
The value must be set to true
of false
.
The function uses the Modbus function code 0x05 (force single coil).
Return value
The function return an OK Result, containing a one, if successful. Otherwise it contains an Error.
Parameters
address
- address of the remote devicestatus
- status that should write at the addressaddr
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let address = 1; assert!(modbus.write_bit(address, true).is_ok());
fn write_register(&self, address: u16, value: u16) -> Result<(), Error>
[src]
write_register
- write a single register
The write_register()
function shall write the value of value holding registers at
the address addr of the remote device.
The function uses the Modbus function code 0x06 (preset single register).
Return value
The function return an OK Result, containing a one, if successful. Otherwise it contains an Error.
Parameters
address
- address of the remote devicevalue
- vec with the value of the holding register which shall be written
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let address = 1; let value = u16::max_value(); assert!(modbus.write_register(address, value).is_ok());
fn write_bits(&self, address: u16, num: u16, src: &[u8]) -> Result<u16, Error>
[src]
write_bits
- write many bits
The write_bits()
function shall write the status of the bits (coils) from src
at the
address
of the remote device. The src
array must contains bytes set to TRUE or FALSE.
The function shall return the number of written bits if successful. Otherwise it contains an Error.
Return value
The function returns a Ok Result containing the number of written bits. Otherwise it contains an Error.
Parameters
address
- address of the remote devicenum
- number or bits that should be writen at the addressaddress
src
- vec of0
and1
(true and false) values
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let address = 1; let tab_bytes = vec![0u8]; assert_eq!(modbus.write_bits(address, 1, &tab_bytes).unwrap(), 1);
fn write_registers(
&self,
address: u16,
num: u16,
src: &[u16]
) -> Result<u16, Error>
[src]
&self,
address: u16,
num: u16,
src: &[u16]
) -> Result<u16, Error>
write_registers
- write many registers
The write_registers()
function shall write the content of the num
holding
registers
from the array src
at address
of the remote device.
The function uses the Modbus function code 0x10 (preset multiple registers).
Return value
The function returns a Ok Result containing the number of written bytes. Otherwise it contains an Error.
Parameters
address
- address of the remote devicenum
- number of holding registers that should write at the addressaddress
src
- holding register
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let address = 1; let tab_bytes = vec![0u16]; assert_eq!(modbus.write_registers(address, 1, &tab_bytes).unwrap(), 1);
fn write_and_read_registers(
&self,
write_address: u16,
write_num: u16,
src: &[u16],
read_address: u16,
read_num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
[src]
&self,
write_address: u16,
write_num: u16,
src: &[u16],
read_address: u16,
read_num: u16,
dest: &mut [u16]
) -> Result<u16, Error>
write_and_read_registers
- write and read many registers in a single transaction
The write_and_read_registers()
function shall write the content of the
write_nb holding registers from the array src to the address write_addr of the remote device then shall read
the content of the read_nb holding registers to the address read_addr of the remote device. The result of
reading is stored in dest array as word values (16 bits).
The function uses the Modbus function code 0x17 (write/read registers).
Return value
The function returns a Ok Result containing the number of read registers. Otherwise it contains an Error.
Parameters
write_address
- address of the remote devicewrite_num
- number of holding registerssrc
- holding registerread_address
- address of the remote deviceread_num
- number of holding registersdest
- holding register
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let address = 1; let request_bytes = vec![1u16]; let mut response_bytes = vec![0u16]; assert_eq!(modbus.write_and_read_registers( address, 1, &request_bytes, address, 1, &mut response_bytes).unwrap(), 1);
fn mask_write_register(
&self,
address: u16,
and_mask: u16,
or_mask: u16
) -> Result<(), Error>
[src]
&self,
address: u16,
and_mask: u16,
or_mask: u16
) -> Result<(), Error>
mask_write_register
- mask a single register
The mask_write_register()
function shall modify the value of the
holding register at the address address
of the remote device using the algorithm:
new value = (current value AND 'and') OR ('or' AND (NOT 'and'))
The function uses the Modbus function code 0x16 (mask single register).
Return value
The function returns a Ok Result if succesful. Otherwise it contains an Error.
Parameters
address
- address of the remote deviceand_mask
- AND maskor_mask
- OR mask
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); assert!(modbus.mask_write_register(1, 0xF2, 0x25).is_ok());
fn send_raw_request(
&self,
raw_request: &mut [u8],
lenght: usize
) -> Result<u16, Error>
[src]
&self,
raw_request: &mut [u8],
lenght: usize
) -> Result<u16, Error>
send_raw_request
- send a raw request
The send_raw_request()
function shall send a request via the socket of the
current modbus contest.
This function must be used for debugging purposes because you have to take care to make a valid request by hand.
The function only adds to the message, the header or CRC of the selected backend, so raw_request
must start
and contain at least a slave/unit identifier and a function code.
This function can be used to send request not handled by the library.
The enum FunctionCode
provides a list of supported Modbus functions codes, to help
build of raw requests.
Parameters
raw_request
- raw request to sendlength
- raw request length
Return value
The function returns a Result, containing the full message lenght, counting the extra data relating to the backend, if successful. Otherwise it contains an Error.
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP, FunctionCode}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut raw_request: Vec<u8> = vec![0xFF, FunctionCode::ReadHoldingRegisters as u8, 0x00, 0x01, 0x0, 0x05]; let mut response = vec![0u8; Modbus::TCP_MAX_ADU_LENGTH]; let request_len = raw_request.len(); assert_eq!(modbus.send_raw_request(&mut raw_request, request_len).unwrap(), 6); assert!(modbus.receive_confirmation(&mut response).is_ok());
fn receive_confirmation(&self, response: &mut [u8]) -> Result<u16, Error>
[src]
receive_confirmation
- receive a confirmation request
The receive_confirmation()
function shall receive a request via the socket of
the context ctx
Member of the Modbus struct.
This function must be used for debugging purposes because the received response isn’t checked against the
initial request.
This function can be used to receive request not handled by the library.
The maximum size of the response depends on the used backend, in RTU the response
array
must be Modbus::RTU_MAX_ADU_LENGTH
bytes and in TCP it must be
Modbus::TCP_MAX_ADU_LENGTH
bytes. If you want to write code compatible with both,
you can use the constant Modbus::MAX_ADU_LENGTH
(maximum value of all libmodbus backends).
Return value
The function returns a Result containing the response length if successful. The returned request length can be zero if the indication request is ignored (eg. a query for another slave in RTU mode). Otherwise it contains an Error.
Parameters
response
- store for the received response
Examples
use libmodbus::{Modbus, ModbusClient, ModbusTCP}; let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); let mut response = vec![0u8; Modbus::MAX_ADU_LENGTH]; assert!(modbus.receive_confirmation(&mut response).is_ok());