My Project
diagnostic_functions.cpp
1 /*
2  * uuid-modbus - Microcontroller asynchronous Modbus library
3  * Copyright 2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/modbus.h>
20 
21 #include <Arduino.h>
22 
23 #include <algorithm>
24 #include <cstdarg>
25 #include <cstdint>
26 #include <memory>
27 
28 #include <make_unique.cpp>
29 
30 namespace uuid {
31 
32 namespace modbus {
33 
34 std::shared_ptr<const ExceptionStatusResponse> SerialClient::read_exception_status(
35  uint16_t device, uint16_t timeout_ms) {
36  auto response = std::make_shared<ExceptionStatusResponse>();
37 
38  if (device < DeviceAddressType::MIN_UNICAST
39  || device > DeviceAddressType::MAX_UNICAST) {
40  response->status(ResponseStatus::FAILURE_INVALID);
41  } else {
42  if (timeout_ms == 0) {
43  timeout_ms = default_unicast_timeout_ms_;
44  }
45 
46  requests_.push_back(std::make_unique<Request>(device,
47  FunctionCode::READ_EXCEPTION_STATUS, timeout_ms, response));
48  }
49 
50  return response;
51 }
52 
54  if (!check_length(frame, len, 3)) {
55  return ResponseStatus::FAILURE_LENGTH;
56  }
57 
58  data_ = frame[2];
59 
60  return ResponseStatus::SUCCESS;
61 }
62 
63 } // namespace modbus
64 
65 } // namespace uuid
uuid::modbus::frame_buffer_t
std::array< uint8_t, MAX_MESSAGE_SIZE+1 > frame_buffer_t
Definition: modbus.h:67
uuid::modbus::ExceptionStatusResponse::data_
uint8_t data_
Definition: modbus.h:335
uuid::modbus::ResponseStatus
ResponseStatus
Status of response messages.
Definition: modbus.h:99
uuid::modbus::ExceptionStatusResponse::parse
ResponseStatus parse(frame_buffer_t &frame, uint16_t len) override
Parse a message frame buffer and store the outcome in this response.
Definition: diagnostic_functions.cpp:53
uuid::modbus::Response::check_length
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.
Definition: generic_functions.cpp:44
uuid::modbus::SerialClient::read_exception_status
std::shared_ptr< const ExceptionStatusResponse > read_exception_status(uint16_t device, uint16_t timeout_ms=0)
Read exception status from a remote device.
Definition: diagnostic_functions.cpp:34
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::modbus::SerialClient::requests_
std::deque< std::unique_ptr< Request > > requests_
Definition: modbus.h:648
uuid::modbus::SerialClient::default_unicast_timeout_ms_
uint16_t default_unicast_timeout_ms_
Definition: modbus.h:649