My Project
Public Member Functions | Private Member Functions | Private Attributes | List of all members
uuid::modbus::SerialClient Class Reference

Serial client used to process requests. More...

#include <uuid/modbus.h>

Collaboration diagram for uuid::modbus::SerialClient:
Collaboration graph
[legend]

Public Member Functions

 SerialClient (::HardwareSerial &serial)
 Create a new client. More...
 
void loop ()
 Loop function that must be called regularly to send and receive messages. More...
 
uint16_t default_unicast_timeout_ms () const
 Get the default timeout for new unicast requests. More...
 
void default_unicast_timeout_ms (uint16_t timeout_ms)
 Set the default timeout for new unicast requests. More...
 
uint16_t default_broadcast_delay_ms () const
 Get the default timeout for new broadcast requests. More...
 
void default_broadcast_delay_ms (uint16_t timeout_ms)
 Set the default timeout for new broadcast requests. More...
 
std::shared_ptr< const RegisterDataResponseread_holding_registers (uint16_t device, uint16_t address, uint16_t size, uint16_t timeout_ms=0)
 Read a contiguous block of holding registers from a remote device. More...
 
std::shared_ptr< const RegisterDataResponseread_input_registers (uint16_t device, uint16_t address, uint16_t size, uint16_t timeout_ms=0)
 Read a contiguous block of input registers from a remote device. More...
 
std::shared_ptr< const RegisterWriteResponsewrite_holding_register (uint16_t device, uint16_t address, uint16_t value, uint16_t timeout_ms=0)
 Write to a single holding register in a remote device. More...
 
std::shared_ptr< const ExceptionStatusResponseread_exception_status (uint16_t device, uint16_t timeout_ms=0)
 Read exception status from a remote device. More...
 

Private Member Functions

void idle ()
 Receive messages while idle. More...
 
void encode ()
 Encode the request message at the top of the queue. More...
 
void transmit ()
 Transmit the current message frame on the serial port device. More...
 
uint32_t input ()
 Read message frames from the serial port device. More...
 
void receive ()
 Receive a message frame and identify the end of a message frame (or timeout). More...
 
void complete ()
 Finish current request and populate response. More...
 
void log_frame (const __FlashStringHelper *prefix)
 Log the contents of the current message frame. More...
 
uint16_t calc_crc () const
 Calculate CRC for the current frame;. More...
 

Private Attributes

::HardwareSerial & serial_
 
std::deque< std::unique_ptr< Request > > requests_
 
uint16_t default_unicast_timeout_ms_ = DEFAULT_UNICAST_TIMEOUT_MS
 
uint16_t default_broadcast_timeout_ms_ = DEFAULT_BROADCAST_TIMEOUT_MS
 
bool idle_frame_ = false
 
frame_buffer_t frame_
 
uint16_t frame_pos_ = 0
 
uint32_t last_rx_ms_ = 0
 
uint16_t tx_frame_size_ = 0
 
uint32_t last_tx_ms_ = 0
 

Detailed Description

Serial client used to process requests.

Since
0.1.0

Definition at line 474 of file modbus.h.

Constructor & Destructor Documentation

◆ SerialClient()

uuid::modbus::SerialClient::SerialClient ( ::HardwareSerial &  serial)

Create a new client.

Parameters
[in]serialSerial port device.
Since
0.1.0

Definition at line 34 of file serial_client.cpp.

Member Function Documentation

◆ calc_crc()

uint16_t uuid::modbus::SerialClient::calc_crc ( ) const
private

Calculate CRC for the current frame;.

Returns
CRC value.
Since
0.1.0

Definition at line 268 of file serial_client.cpp.

◆ complete()

void uuid::modbus::SerialClient::complete ( )
private

Finish current request and populate response.

Since
0.1.0

Definition at line 178 of file serial_client.cpp.

◆ default_broadcast_delay_ms() [1/2]

uint16_t uuid::modbus::SerialClient::default_broadcast_delay_ms ( ) const
inline

Get the default timeout for new broadcast requests.

Returns
Delay after a request in milliseconds.
Since
0.2.0

Definition at line 514 of file modbus.h.

◆ default_broadcast_delay_ms() [2/2]

void uuid::modbus::SerialClient::default_broadcast_delay_ms ( uint16_t  timeout_ms)
inline

Set the default timeout for new broadcast requests.

Parameters
[in]timeout_msDelay after a request in milliseconds.
Since
0.2.0

Definition at line 521 of file modbus.h.

◆ default_unicast_timeout_ms() [1/2]

uint16_t uuid::modbus::SerialClient::default_unicast_timeout_ms ( ) const
inline

Get the default timeout for new unicast requests.

Returns
Timeout to wait for a response in milliseconds.
Since
0.2.0

Definition at line 499 of file modbus.h.

◆ default_unicast_timeout_ms() [2/2]

void uuid::modbus::SerialClient::default_unicast_timeout_ms ( uint16_t  timeout_ms)
inline

Set the default timeout for new unicast requests.

Parameters
[in]timeout_msTimeout to wait for a response in milliseconds.
Since
0.2.0

Definition at line 506 of file modbus.h.

◆ encode()

void uuid::modbus::SerialClient::encode ( )
private

Encode the request message at the top of the queue.

Since
0.1.0

Definition at line 85 of file serial_client.cpp.

◆ idle()

void uuid::modbus::SerialClient::idle ( )
private

Receive messages while idle.

Since
0.1.0

Definition at line 68 of file serial_client.cpp.

◆ input()

uint32_t uuid::modbus::SerialClient::input ( )
private

Read message frames from the serial port device.

Returns
Current time from millis() at the last read event.
Since
0.1.0

Definition at line 127 of file serial_client.cpp.

◆ log_frame()

void uuid::modbus::SerialClient::log_frame ( const __FlashStringHelper *  prefix)
private

Log the contents of the current message frame.

Parameters
[in]prefixMessage prefix ("<-" or "->").
Since
0.1.0

Definition at line 245 of file serial_client.cpp.

◆ loop()

void uuid::modbus::SerialClient::loop ( )

Loop function that must be called regularly to send and receive messages.

Since
0.1.0

Definition at line 37 of file serial_client.cpp.

◆ read_exception_status()

std::shared_ptr< const ExceptionStatusResponse > uuid::modbus::SerialClient::read_exception_status ( uint16_t  device,
uint16_t  timeout_ms = 0 
)

Read exception status from a remote device.

Parameters
[in]deviceDevice address (DeviceAddressTypes::MIN_UNICAST to DeviceAddressTypes::MAX_UNICAST).
[in]timeout_msTimeout to wait for a response in milliseconds (0 = default).
Returns
A response message that will contain the outcome and output data in the future when processing is complete.
Since
0.1.0

Definition at line 34 of file diagnostic_functions.cpp.

◆ read_holding_registers()

std::shared_ptr< const RegisterDataResponse > uuid::modbus::SerialClient::read_holding_registers ( uint16_t  device,
uint16_t  address,
uint16_t  size,
uint16_t  timeout_ms = 0 
)

Read a contiguous block of holding registers from a remote device.

The response message contains the register values returned.

Parameters
[in]deviceDevice address (DeviceAddressTypes::MIN_UNICAST to DeviceAddressTypes::MAX_UNICAST).
[in]addressStarting address (0x0000 to 0xFFFF).
[in]sizeQuantity of registers (0x0001 to 0x007D).
[in]timeout_msTimeout to wait for a response in milliseconds (0 = default).
Returns
A response message that will contain the outcome and data in the future when processing is complete.
Since
0.1.0

Definition at line 34 of file register_functions.cpp.

◆ read_input_registers()

std::shared_ptr< const RegisterDataResponse > uuid::modbus::SerialClient::read_input_registers ( uint16_t  device,
uint16_t  address,
uint16_t  size,
uint16_t  timeout_ms = 0 
)

Read a contiguous block of input registers from a remote device.

The response message contains the register values returned.

Parameters
[in]deviceDevice address (DeviceAddressTypes::MIN_UNICAST to DeviceAddressTypes::MAX_UNICAST).
[in]addressStarting address (0x0000 to 0xFFFF).
[in]sizeQuantity of registers (0x0001 to 0x007D).
[in]timeout_msTimeout to wait for a response in milliseconds (0 = default).
Returns
A response message that will contain the outcome and data in the future when processing is complete.
Since
0.1.0

Definition at line 55 of file register_functions.cpp.

◆ receive()

void uuid::modbus::SerialClient::receive ( )
private

Receive a message frame and identify the end of a message frame (or timeout).

Since
0.1.0

Definition at line 157 of file serial_client.cpp.

◆ transmit()

void uuid::modbus::SerialClient::transmit ( )
private

Transmit the current message frame on the serial port device.

Since
0.1.0

Definition at line 107 of file serial_client.cpp.

◆ write_holding_register()

std::shared_ptr< const RegisterWriteResponse > uuid::modbus::SerialClient::write_holding_register ( uint16_t  device,
uint16_t  address,
uint16_t  value,
uint16_t  timeout_ms = 0 
)

Write to a single holding register in a remote device.

The response message contains the register address followed by the register value returned.

For a broadcast request, use timeout_ms to set the turnaround delay.

Parameters
[in]deviceDevice address (DeviceAddressTypes::BROADCAST to DeviceAddressTypes::MAX_UNICAST).
[in]addressRegister address (0x0000 to 0xFFFF).
[in]valueRegister value.
[in]timeout_msTimeout to wait for a response (or turnaround delay) in milliseconds (0 = default).
Returns
A response message that will contain the outcome and echoed data in the future when processing is complete.
Since
0.1.0

Definition at line 76 of file register_functions.cpp.

Member Data Documentation

◆ default_broadcast_timeout_ms_

uint16_t uuid::modbus::SerialClient::default_broadcast_timeout_ms_ = DEFAULT_BROADCAST_TIMEOUT_MS
private

Default timeout for new broadcast requests.

Since
0.2.0

Definition at line 650 of file modbus.h.

◆ default_unicast_timeout_ms_

uint16_t uuid::modbus::SerialClient::default_unicast_timeout_ms_ = DEFAULT_UNICAST_TIMEOUT_MS
private

Default timeout for new unicast requests.

Since
0.2.0

Definition at line 649 of file modbus.h.

◆ frame_

frame_buffer_t uuid::modbus::SerialClient::frame_
private

Current message frame.

Since
0.1.0

Definition at line 654 of file modbus.h.

◆ frame_pos_

uint16_t uuid::modbus::SerialClient::frame_pos_ = 0
private

Position in message frame.

Since
0.1.0

Definition at line 655 of file modbus.h.

◆ idle_frame_

bool uuid::modbus::SerialClient::idle_frame_ = false
private

Message frame being received while idle.

Since
0.1.0

Definition at line 652 of file modbus.h.

◆ last_rx_ms_

uint32_t uuid::modbus::SerialClient::last_rx_ms_ = 0
private

Time that the last character was received.

Since
0.1.0

Definition at line 657 of file modbus.h.

◆ last_tx_ms_

uint32_t uuid::modbus::SerialClient::last_tx_ms_ = 0
private

Time that the last character was transmitted.

Since
0.1.0

Definition at line 660 of file modbus.h.

◆ requests_

std::deque<std::unique_ptr<Request> > uuid::modbus::SerialClient::requests_
private

Pending requests.

Since
0.1.0

Definition at line 648 of file modbus.h.

◆ serial_

::HardwareSerial& uuid::modbus::SerialClient::serial_
private

Serial port device.

Since
0.1.0

Definition at line 647 of file modbus.h.

◆ tx_frame_size_

uint16_t uuid::modbus::SerialClient::tx_frame_size_ = 0
private

Size of request frame to transmit.

Since
0.1.0

Definition at line 659 of file modbus.h.


The documentation for this class was generated from the following files: