Skip to content

Commit

Permalink
feat: allow changing the client read timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
willeccles committed Sep 26, 2024
1 parent 5d5eb9e commit de6c471
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 14 additions & 1 deletion include/bci/abs/ScpiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace bci::abs {
class ScpiClient {
public:
/// Default CTOR.
ScpiClient() = default;
ScpiClient() noexcept;

/**
* @brief Initialize a ScpiClient with a driver handle.
Expand Down Expand Up @@ -103,6 +103,16 @@ class ScpiClient {
*/
void SetDriver(std::shared_ptr<drivers::CommDriver> driver) noexcept;

/**
* @brief Set the read timeout for the client. In most cases, this is
* unnecessary. The default is 150ms.
*
* @param[in] timeout_ms new read timeout in milliseconds
*
* @return The previous timeout value.
*/
unsigned int SetReadTimeout(unsigned int timeout_ms) noexcept;

/**
* @brief Change the targeted device ID. This is currently only meaningful for
* RS-485.
Expand Down Expand Up @@ -1609,6 +1619,9 @@ class ScpiClient {
private:
/// Driver handle.
std::shared_ptr<drivers::CommDriver> driver_;

/// Read timeout.
unsigned int read_timeout_ms_;
};

} // namespace bci::abs
Expand Down
15 changes: 11 additions & 4 deletions src/ScpiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
namespace bci::abs {

static constexpr unsigned int kWriteTimeoutMs = 10;
static constexpr unsigned int kReadTimeoutMs = 150;

using util::Err;
using ec = ErrorCode;

ScpiClient::ScpiClient() noexcept : ScpiClient(nullptr) {}

ScpiClient::ScpiClient(std::shared_ptr<drivers::CommDriver> driver) noexcept
: driver_{std::move(driver)} {}
: driver_{std::move(driver)}, read_timeout_ms_{150U} {}

ScpiClient::ScpiClient(ScpiClient&& other) noexcept
: driver_{std::move(other.driver_)} {}
: driver_{std::move(other.driver_)},
read_timeout_ms_{std::move(other.read_timeout_ms_)} {}

ScpiClient& ScpiClient::operator=(ScpiClient&& rhs) noexcept {
driver_ = std::move(rhs.driver_);
read_timeout_ms_ = std::move(rhs.read_timeout_ms_);
return *this;
}

Expand All @@ -48,6 +51,10 @@ void ScpiClient::SetDriver(
driver_ = std::move(driver);
}

unsigned int ScpiClient::SetReadTimeout(unsigned int timeout_ms) noexcept {
return std::exchange(read_timeout_ms_, timeout_ms);
}

ErrorCode ScpiClient::SetTargetDeviceID(unsigned int id) {
if (driver_) {
driver_->SetDeviceID(id);
Expand Down Expand Up @@ -84,7 +91,7 @@ Result<std::string> ScpiClient::SendAndRecv(std::string_view buf) const {
if (res != ErrorCode::kSuccess) {
return Err(res);
}
return driver_->ReadLine(kReadTimeoutMs);
return driver_->ReadLine(read_timeout_ms_);
}

} // namespace bci::abs

0 comments on commit de6c471

Please sign in to comment.