Skip to content

Commit

Permalink
discovery: add serial discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
willeccles committed May 14, 2024
1 parent 493f9de commit fedfafc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
13 changes: 12 additions & 1 deletion include/bci/abs/Discovery.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ABS_SCPI_DRIVER_INCLUDE_BCI_ABS_DISCOVERY_H
#define ABS_SCPI_DRIVER_INCLUDE_BCI_ABS_DISCOVERY_H

#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -14,9 +15,19 @@ struct EthernetDevice {
std::string serial;
};

struct SerialDevice {
std::uint8_t id;
std::string serial;
};

using EthernetDeviceList = std::vector<EthernetDevice>;

Result<EthernetDeviceList> MulticastDiscover(std::string_view interface_ip);
using SerialDeviceList = std::vector<SerialDevice>;

Result<EthernetDeviceList> MulticastDiscovery(std::string_view interface_ip);

Result<SerialDeviceList> SerialDiscovery(std::string_view port,
unsigned int max_devices);

} // namespace bci::abs

Expand Down
46 changes: 40 additions & 6 deletions src/Discovery.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <bci/abs/Discovery.h>

#include <array>
#include <memory>
#include <string>
#include <string_view>

#include <bci/abs/CommonTypes.h>
#include <bci/abs/SerialDriver.h>
#include <bci/abs/UdpMulticastDriver.h>

#include "ScpiUtil.h"
Expand All @@ -16,24 +16,24 @@ namespace bci::abs {
using ec = ErrorCode;
using util::Err;

Result<EthernetDeviceList> MulticastDiscover(std::string_view interface_ip) {
Result<EthernetDeviceList> MulticastDiscovery(std::string_view interface_ip) {
EthernetDeviceList devices;

auto driver = std::make_shared<drivers::UdpMcastDriver>();
drivers::UdpMcastDriver driver;

ec ret = driver->Open(interface_ip);
ec ret = driver.Open(interface_ip);
if (ret != ec::kSuccess) {
return Err(ret);
}

ret = driver->Write("*IDN?\r\n", 100);
ret = driver.Write("*IDN?\r\n", 100);
if (ret != ec::kSuccess) {
return Err(ret);
}

bool done = false;
while (!done) {
if (auto resp = driver->ReadLineFrom(100)) {
if (auto resp = driver.ReadLineFrom(100)) {
std::array<std::string_view, 4> idn;
if (scpi::SplitRespMnemonics(resp->data, idn) != ErrorCode::kSuccess) {
return Err(ErrorCode::kInvalidResponse);
Expand All @@ -50,4 +50,38 @@ Result<EthernetDeviceList> MulticastDiscover(std::string_view interface_ip) {
return devices;
}

Result<SerialDeviceList> SerialDiscovery(std::string_view port,
unsigned int max_devices) {
SerialDeviceList devices;

if (max_devices == 0 || max_devices > 256) {
max_devices = 256;
}

drivers::SerialDriver driver;

ec ret = driver.Open(std::string(port));
if (ret != ec::kSuccess) {
return Err(ret);
}

for (std::uint8_t id = 0; max_devices > 0; ++id, --max_devices) {
driver.SetDeviceID(id);
ret = driver.Write("*IDN?\r\n", 100);
if (auto resp = driver.ReadLine(10)) {
std::array<std::string_view, 4> idn;
if (scpi::SplitRespMnemonics(*resp, idn) != ErrorCode::kSuccess) {
return Err(ErrorCode::kInvalidResponse);
}
devices.emplace_back(id, std::string(idn[2]));
} else {
if (resp.error() != ec::kReadTimedOut) {
return Err(resp.error());
}
}
}

return devices;
}

} // namespace bci::abs

0 comments on commit fedfafc

Please sign in to comment.