Skip to content

Commit d8802c6

Browse files
committed
Simplify serial auto detect, only /dev/serial/by-id
1 parent 365dbbc commit d8802c6

File tree

1 file changed

+2
-57
lines changed

1 file changed

+2
-57
lines changed

Source/Device/Serial.cpp

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -317,22 +317,8 @@ namespace Device
317317
{
318318
device_list.clear();
319319

320-
#ifdef _WIN32
321-
for (int i = 1; i <= 256; i++)
322-
{
323-
std::string port_name = "\\\\.\\COM" + std::to_string(i);
324-
HANDLE h = CreateFileA(port_name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
325-
if (h != INVALID_HANDLE_VALUE)
326-
{
327-
CloseHandle(h);
328-
std::string display_name = "COM" + std::to_string(i);
329-
uint64_t handle = device_list.size();
330-
device_list.push_back(port_name);
331-
DeviceList.push_back(Description("Serial", "COM Port", display_name, handle, Type::SERIALPORT));
332-
}
333-
}
334-
#else
335-
// First, scan /dev/serial/by-id for persistent device names (symlinks)
320+
#ifndef _WIN32
321+
// Scan /dev/serial/by-id for persistent device names (symlinks)
336322
std::vector<std::string> by_id_files = Util::Helper::getFilesInDirectory("/dev/serial/by-id");
337323
for (const auto &name : by_id_files)
338324
{
@@ -342,47 +328,6 @@ namespace Device
342328
device_list.push_back(device_path);
343329
DeviceList.push_back(Description("Serial", "USB Serial", name, handle, Type::SERIALPORT));
344330
}
345-
346-
std::vector<std::string> dev_files = Util::Helper::getFilesInDirectory("/dev");
347-
for (const auto &name : dev_files)
348-
{
349-
// Check for common serial device patterns
350-
if (name.compare(0, 6, "ttyUSB") == 0 || name.compare(0, 6, "ttyACM") == 0 || name.compare(0, 4, "ttyS") == 0)
351-
{
352-
std::string device_path = "/dev/" + name;
353-
struct stat st;
354-
if (stat(device_path.c_str(), &st) == 0 && S_ISCHR(st.st_mode))
355-
{
356-
// Only add if not already found via by-id
357-
bool already_listed = false;
358-
for (const auto &dev : device_list)
359-
{
360-
char resolved_path[PATH_MAX];
361-
if (realpath(dev.c_str(), resolved_path) != nullptr)
362-
{
363-
if (device_path == std::string(resolved_path))
364-
{
365-
already_listed = true;
366-
break;
367-
}
368-
}
369-
}
370-
371-
if (!already_listed)
372-
{
373-
std::string type_str = "Serial Port";
374-
if (name.compare(0, 6, "ttyUSB") == 0)
375-
type_str = "USB Serial";
376-
else if (name.compare(0, 6, "ttyACM") == 0)
377-
type_str = "ACM Serial";
378-
379-
uint64_t handle = device_list.size();
380-
device_list.push_back(device_path);
381-
DeviceList.push_back(Description("Serial", type_str, name, handle, Type::SERIALPORT));
382-
}
383-
}
384-
}
385-
}
386331
#endif
387332
}
388333
}

0 commit comments

Comments
 (0)