Skip to content

Commit

Permalink
Updated error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
pkubanek committed May 6, 2024
1 parent 95f550f commit 4b385b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions include/Modbus/BusList.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Modbus {
* Error thrown when a response is missing. This is mostly caused by an @glos{ILC} on
* the bus being dead/not reacting to the command send.
*/
class MissingResponse : std::runtime_error {
class MissingResponse : public std::runtime_error {
public:
/**
* Construct missing response exception.
Expand All @@ -49,8 +49,9 @@ class MissingResponse : std::runtime_error {
* @param func Expected function which wasn't responded by the @glos{ILC}
*/
MissingResponse(uint8_t address, uint8_t func)
: std::runtime_error(fmt::format("Missing response for function {} from ILC with address {}",
func, address)) {}
: std::runtime_error(
fmt::format("Missing response for function {0} (0x{0:02x}) from ILC with address {1}",
func, address)) {}
};

/**
Expand All @@ -59,7 +60,7 @@ class MissingResponse : std::runtime_error {
*
* @see BusList::addResponse
*/
class UnexpectedResponse : std::runtime_error {
class UnexpectedResponse : public std::runtime_error {
public:
/**
* Construct unexpected response.
Expand Down
8 changes: 6 additions & 2 deletions include/Modbus/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <string>
#include <vector>

#include <spdlog/fmt/fmt.h>

#include <Modbus/Buffer.h>

namespace Modbus {
Expand Down Expand Up @@ -78,7 +80,7 @@ class CRCError : public std::runtime_error {
/**
* Thrown when response continue after CRC.
*/
class LongResponse : std::runtime_error {
class LongResponse : public std::runtime_error {
public:
/**
* Construct LongResponse from the given buffer.
Expand Down Expand Up @@ -124,7 +126,9 @@ class Parser : public std::vector<uint8_t> {
*/
void readBuffer(void *buf, size_t len) {
if (_data + len > size()) {
throw std::out_of_range("Trying to access data beyond buffer are.");
throw std::out_of_range(fmt::format(
"Attempt to access data beyond buffer end (buffer index {}, but buffer length is {}).",
_data + len, size()));
}
memcpy(buf, data() + _data, len);
_data += len;
Expand Down
2 changes: 1 addition & 1 deletion src/ILC/ILCBusList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ std::vector<const char *> ILCBusList::getFaultString(uint16_t fault) {
void ILCBusList::changeILCMode(uint8_t address, uint16_t mode) {
uint32_t timeout = 335;
try {
if ((getLastMode(address) == Mode::Standby && mode == Mode::Bootloader) ||
if ((mode == Mode::Bootloader) ||
(getLastMode(address) == Mode::Bootloader && mode == Mode::Standby)) {
timeout = 100000;
}
Expand Down
5 changes: 3 additions & 2 deletions src/LSST/cRIO/FPGA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ void FPGA::ilcCommands(ILC::ILCBusList &ilc, int32_t timeout) {
uint16_t responseLen;

readU16ResponseFIFO(&responseLen, 1, 20);
if (responseLen < 4) {
// minimal response is timestamp + 4 ILC bytes
if (responseLen < 8) {
if (responseLen > 0) {
uint16_t buffer[responseLen];
readU16ResponseFIFO(buffer, responseLen, 10);
}
throw std::runtime_error("FPGA::ilcCommands timeout on response: " + std::to_string(responseLen));
throw Modbus::MissingResponse(ilc[0].buffer.address(), ilc[0].buffer.func());
}

uint16_t buffer[responseLen];
Expand Down

0 comments on commit 4b385b6

Please sign in to comment.