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