19 #ifndef UUID_MODBUS_H_
20 #define UUID_MODBUS_H_
134 inline bool done()
const {
return status_ >= ResponseStatus::SUCCESS; }
256 inline const std::vector<uint16_t>&
data()
const {
return data_; };
364 const std::shared_ptr<Response> &
response);
437 const std::shared_ptr<Response> &
response);
537 uint16_t address, uint16_t size, uint16_t timeout_ms = 0);
553 uint16_t address, uint16_t size, uint16_t timeout_ms = 0);
572 uint16_t address, uint16_t value, uint16_t timeout_ms = 0);
584 uint16_t timeout_ms = 0);
637 void log_frame(
const __FlashStringHelper *prefix);
Logger instance used to make log messages.
Exception status response message.
uint8_t data() const
Get the output data from the device response.
ResponseStatus parse(frame_buffer_t &frame, uint16_t len) override
Parse a message frame buffer and store the outcome in this response.
Register data response message.
ResponseStatus parse(frame_buffer_t &frame, uint16_t len) override
Parse a message frame buffer and store the outcome in this response.
const std::vector< uint16_t > & data() const
Data from the device response, which may be fewer or more register values than requested.
std::vector< uint16_t > data_
Request message for register functions.
uint16_t data() const
Get the register size or value.
uint16_t address() const
Get the register address.
RegisterRequest(uint16_t device, uint8_t function_code, uint16_t timeout_ms, uint16_t address, uint16_t data, const std::shared_ptr< Response > &response)
Create a new register request message (not directly useful).
uint16_t encode(frame_buffer_t &frame) override
Encode this request and store it in a message frame buffer.
Register write response message.
uint16_t address() const
Get the address from the device response, which should match the address that was requested.
ResponseStatus parse(frame_buffer_t &frame, uint16_t len) override
Parse a message frame buffer and store the outcome in this response.
const uint8_t function_code_
virtual uint16_t encode(frame_buffer_t &frame)
Encode this request and store it in a message frame buffer.
uint8_t function_code() const
Get the function code of the request.
Request(uint16_t device, uint8_t function_code, uint16_t timeout_ms, const std::shared_ptr< Response > &response)
Create a new request message (not directly useful).
uint16_t device() const
Get the destination device address.
const std::shared_ptr< Response > response_
const uint16_t timeout_ms_
Response & response() const
Get the response object.
uint16_t timeout_ms() const
Get the timeout to wait for a response in milliseconds.
uint8_t exception_code() const
Get the exception code from the device response.
bool success() const
Determine if the request was successful.
ResponseStatus status() const
Get the status of the response message.
void status(ResponseStatus status)
Set the status of the response message.
bool check_length(frame_buffer_t &frame, uint16_t actual, uint16_t expected)
Check the length of the message frame is correct and log an error if it is not.
bool pending() const
Determine if the request is still pending.
bool failed() const
Determine if the request failed for a reason other than an exception.
bool done() const
Determine if the request is complete.
bool exception() const
Determine if the request returned an exception.
void exception_code(uint8_t exception_code)
Set the exception code from the device response.
virtual ResponseStatus parse(frame_buffer_t &frame, uint16_t len)=0
Parse a message frame buffer and store the outcome in this response.
Serial client used to process requests.
uint16_t calc_crc() const
Calculate CRC for the current frame;.
void loop()
Loop function that must be called regularly to send and receive messages.
SerialClient(::HardwareSerial &serial)
Create a new client.
uint16_t default_broadcast_timeout_ms_
uint16_t default_unicast_timeout_ms_
void default_unicast_timeout_ms(uint16_t timeout_ms)
Set the default timeout for new unicast requests.
void transmit()
Transmit the current message frame on the serial port device.
void default_broadcast_delay_ms(uint16_t timeout_ms)
Set the default timeout for new broadcast requests.
uint16_t default_broadcast_delay_ms() const
Get the default timeout for new broadcast requests.
void complete()
Finish current request and populate response.
void log_frame(const __FlashStringHelper *prefix)
Log the contents of the current message frame.
uint32_t input()
Read message frames from the serial port device.
::HardwareSerial & serial_
void idle()
Receive messages while idle.
uint16_t default_unicast_timeout_ms() const
Get the default timeout for new unicast requests.
void encode()
Encode the request message at the top of the queue.
void receive()
Receive a message frame and identify the end of a message frame (or timeout).
std::shared_ptr< const ExceptionStatusResponse > read_exception_status(uint16_t device, uint16_t timeout_ms=0)
Read exception status from a remote device.
std::shared_ptr< const RegisterDataResponse > 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.
std::shared_ptr< const RegisterWriteResponse > 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.
std::shared_ptr< const RegisterDataResponse > 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.
std::deque< std::unique_ptr< Request > > requests_
constexpr uint16_t MESSAGE_CRC_SIZE
constexpr uint16_t MESSAGE_HEADER_SIZE
constexpr uint16_t MAX_MESSAGE_SIZE
FunctionCode
Function codes.
DeviceAddressType
Device address types.
constexpr uint32_t INTER_FRAME_TIMEOUT_MS
Timeout between frames (in milliseconds).
constexpr uint16_t DEFAULT_UNICAST_TIMEOUT_MS
constexpr uint16_t DEFAULT_BROADCAST_TIMEOUT_MS
const uuid::log::Logger logger
ResponseStatus
Status of response messages.
std::array< uint8_t, MAX_MESSAGE_SIZE+1 > frame_buffer_t