19 #include <uuid/modbus.h>
28 #include <make_unique.cpp>
35 uint16_t device, uint16_t address, uint16_t size, uint16_t timeout_ms) {
36 auto response = std::make_shared<RegisterDataResponse>();
38 if (device < DeviceAddressType::MIN_UNICAST
39 || device > DeviceAddressType::MAX_UNICAST
40 || size < 1 || size > 0x007D) {
41 response->status(ResponseStatus::FAILURE_INVALID);
43 if (timeout_ms == 0) {
47 requests_.push_back(std::make_unique<RegisterRequest>(device,
48 FunctionCode::READ_HOLDING_REGISTERS, timeout_ms, address, size,
56 uint16_t device, uint16_t address, uint16_t size, uint16_t timeout_ms) {
57 auto response = std::make_shared<RegisterDataResponse>();
59 if (device < DeviceAddressType::MIN_UNICAST
60 || device > DeviceAddressType::MAX_UNICAST
61 || size < 1 || size > 0x007D) {
62 response->status(ResponseStatus::FAILURE_INVALID);
64 if (timeout_ms == 0) {
68 requests_.push_back(std::make_unique<RegisterRequest>(device,
69 FunctionCode::READ_INPUT_REGISTERS, timeout_ms, address, size,
77 uint16_t device, uint16_t address, uint16_t value, uint16_t timeout_ms) {
78 auto response = std::make_shared<RegisterWriteResponse>();
80 if (device > DeviceAddressType::MAX_UNICAST) {
81 response->status(ResponseStatus::FAILURE_INVALID);
83 if (timeout_ms == 0) {
84 if (device == DeviceAddressType::BROADCAST) {
91 requests_.push_back(std::make_unique<RegisterRequest>(device,
92 FunctionCode::WRITE_SINGLE_REGISTER, timeout_ms, address, value,
100 uint16_t timeout_ms, uint16_t address, uint16_t data,
101 const std::shared_ptr<Response> &response)
102 :
Request(device, function_code, timeout_ms, response),
103 address_(address), data_(data) {
111 frame[4] =
data() >> 8;
112 frame[5] =
data() & 0xFF;
118 logger.
err(F(
"Incomplete message for function %02X from device %u, expected 3+ received %u"),
119 frame[1], frame[0], len);
120 return ResponseStatus::FAILURE_LENGTH;
122 return ResponseStatus::FAILURE_LENGTH;
123 }
else if (frame[2] & 1) {
124 logger.
err(F(
"Invalid message for function %02X from device %u, byte count %u is not a multiple of 2"),
125 frame[1], frame[0], frame[2]);
126 return ResponseStatus::FAILURE_LENGTH;
129 for (uint16_t i = 0; i < frame[2]; i += 2) {
130 data_.emplace_back((frame[3 + i] << 8) | frame[4 + i]);
133 return ResponseStatus::SUCCESS;
138 return ResponseStatus::FAILURE_LENGTH;
141 address_ = (frame[2] << 8) | frame[3];
142 data_.emplace_back((frame[4] << 8) | frame[5]);
144 return ResponseStatus::SUCCESS;