Skip to content

Commit

Permalink
discovery: specify range of serial IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
willeccles committed May 14, 2024
1 parent 4902b92 commit 963238e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/bci/abs/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ using SerialDeviceList = std::vector<SerialDevice>;
Result<EthernetDeviceList> MulticastDiscovery(std::string_view interface_ip);

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

} // namespace bci::abs

Expand Down
15 changes: 9 additions & 6 deletions src/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ Result<EthernetDeviceList> MulticastDiscovery(std::string_view interface_ip) {
}

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

if (max_devices == 0 || max_devices > 256) {
max_devices = 256;
if (last_id < first_id) {
return Err(ec::kInvalidArgument);
}

drivers::SerialDriver driver;
Expand All @@ -64,15 +65,17 @@ Result<SerialDeviceList> SerialDiscovery(std::string_view port,
return Err(ret);
}

for (std::uint8_t id = 0; max_devices > 0; ++id, --max_devices) {
driver.SetDeviceID(id);
int total = last_id - first_id + 1;

for (int i = 0; i < total; ++i) {
driver.SetDeviceID(first_id + i);
ret = driver.Write("*IDN?\r\n", 100);
if (auto resp = driver.ReadLine(50)) {
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]));
devices.emplace_back(first_id + i, std::string(idn[2]));
} else {
if (resp.error() != ec::kReadTimedOut) {
return Err(resp.error());
Expand Down

0 comments on commit 963238e

Please sign in to comment.