Added code to detect valid serial ports.

Next we have to tie this into the UI, so AtariVox port can be more easily detected.
This commit is contained in:
Stephen Anthony 2020-09-12 16:44:55 -02:30
parent 0703606c89
commit 67db826bc5
9 changed files with 121 additions and 12 deletions

View File

@ -65,6 +65,13 @@ class SerialPort
*/ */
virtual bool isCTS() { return true; } virtual bool isCTS() { return true; }
/**
Get all valid serial ports detected on this system.
@return The (possibly empty) list of detected serial ports
*/
virtual StringList portNames() { return StringList{}; }
private: private:
// Following constructors and assignment operators not supported // Following constructors and assignment operators not supported
SerialPort(const SerialPort&) = delete; SerialPort(const SerialPort&) = delete;

View File

@ -25,6 +25,7 @@
#include <sys/filio.h> #include <sys/filio.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "FSNode.hxx"
#include "SerialPortMACOS.hxx" #include "SerialPortMACOS.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -92,3 +93,26 @@ bool SerialPortMACOS::isCTS()
} }
return false; return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StringList SerialPortMACOS::portNames()
{
StringList ports;
// Get all possible devices in the '/dev' directory
FilesystemNode::NameFilter filter = [](const FilesystemNode& node) {
return BSPF::startsWithIgnoreCase(node.getPath(), "/dev/tty.usb");
};
FSList portList;
portList.reserve(16);
FilesystemNode dev("/dev/");
dev.getChildren(portList, FilesystemNode::ListMode::All, filter, false);
// Add only those that can be opened
for(const auto& port: portList)
if(openPort(port.getPath()))
ports.emplace_back(port.getPath());
return ports;
}

View File

@ -21,8 +21,7 @@
#include "SerialPort.hxx" #include "SerialPort.hxx"
/** /**
Implement reading and writing from a serial port under macOS. For now, Implement reading and writing from a serial port under macOS.
reading isn't actually supported at all.
@author Stephen Anthony & D. Spice @author Stephen Anthony & D. Spice
*/ */
@ -63,6 +62,13 @@ class SerialPortMACOS : public SerialPort
*/ */
bool isCTS() override; bool isCTS() override;
/**
Get all valid serial ports detected on this system.
@return The (possibly empty) list of detected serial ports
*/
StringList portNames() override;
private: private:
// File descriptor for serial connection // File descriptor for serial connection
int myHandle{0}; int myHandle{0};

View File

@ -24,6 +24,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <cstring> #include <cstring>
#include "FSNode.hxx"
#include "SerialPortUNIX.hxx" #include "SerialPortUNIX.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -95,3 +96,28 @@ bool SerialPortUNIX::isCTS()
} }
return false; return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StringList SerialPortUNIX::portNames()
{
StringList ports;
// Get all possible devices in the '/dev' directory
FilesystemNode::NameFilter filter = [](const FilesystemNode& node) {
return BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyACM") ||
BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyS") ||
BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyUSB");
};
FSList portList;
portList.reserve(16);
FilesystemNode dev("/dev/");
dev.getChildren(portList, FilesystemNode::ListMode::All, filter, false);
// Add only those that can be opened
for(const auto& port: portList)
if(openPort(port.getPath()))
ports.emplace_back(port.getPath());
return ports;
}

View File

@ -22,7 +22,7 @@
/** /**
Implement reading and writing from a serial port under UNIX. For now, Implement reading and writing from a serial port under UNIX. For now,
it seems to be Linux-only, and reading isn't actually supported at all. it seems to be Linux-only.
@author Stephen Anthony @author Stephen Anthony
*/ */
@ -63,6 +63,13 @@ class SerialPortUNIX : public SerialPort
*/ */
bool isCTS() override; bool isCTS() override;
/**
Get all valid serial ports detected on this system.
@return The (possibly empty) list of detected serial ports
*/
StringList portNames() override;
private: private:
// File descriptor for serial connection // File descriptor for serial connection
int myHandle{0}; int myHandle{0};

View File

@ -112,3 +112,39 @@ bool SerialPortWINDOWS::isCTS()
} }
return false; return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StringList SerialPortWINDOWS::portNames()
{
StringList ports;
HKEY hKey = NULL;
LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS)
{
TCHAR deviceName[2048], friendlyName[32];
DWORD numValues = 0;
result = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
&numValues, NULL, NULL, NULL, NULL);
if (result == ERROR_SUCCESS)
{
DWORD type = 0;
DWORD deviceNameLen = 2047;
DWORD friendlyNameLen = 31;
for (DWORD i = 0; i < numValues; ++i)
{
result = RegEnumValue(hKey, i, deviceName, &deviceNameLen,
NULL, &type, (LPBYTE)friendlyName, &friendlyNameLen);
if (result == ERROR_SUCCESS && type == REG_SZ)
ports.emplace_back(friendlyName);
}
}
}
RegCloseKey(hKey);
return ports;
}

View File

@ -63,6 +63,13 @@ class SerialPortWINDOWS : public SerialPort
*/ */
bool isCTS() override; bool isCTS() override;
/**
Get all valid serial ports detected on this system.
@return The (possibly empty) list of detected serial ports
*/
StringList portNames() override;
private: private:
// Handle to serial port // Handle to serial port
HANDLE myHandle{0}; HANDLE myHandle{0};

View File

@ -1742,7 +1742,6 @@
<ClInclude Include="..\emucore\AmigaMouse.hxx" /> <ClInclude Include="..\emucore\AmigaMouse.hxx" />
<ClInclude Include="..\emucore\AtariMouse.hxx" /> <ClInclude Include="..\emucore\AtariMouse.hxx" />
<ClInclude Include="..\emucore\Bankswitch.hxx" /> <ClInclude Include="..\emucore\Bankswitch.hxx" />
<ClInclude Include="..\emucore\BSType.hxx" />
<ClInclude Include="..\emucore\Cart3EPlus.hxx" /> <ClInclude Include="..\emucore\Cart3EPlus.hxx" />
<ClInclude Include="..\emucore\Cart3EX.hxx" /> <ClInclude Include="..\emucore\Cart3EX.hxx" />
<ClInclude Include="..\emucore\Cart4KSC.hxx" /> <ClInclude Include="..\emucore\Cart4KSC.hxx" />

View File

@ -1829,9 +1829,6 @@
<ClInclude Include="..\common\tv_filters\AtariNTSC.hxx"> <ClInclude Include="..\common\tv_filters\AtariNTSC.hxx">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\emucore\BSType.hxx">
<Filter>Header Files\emucore</Filter>
</ClInclude>
<ClInclude Include="..\emucore\CartDetector.hxx"> <ClInclude Include="..\emucore\CartDetector.hxx">
<Filter>Header Files\emucore</Filter> <Filter>Header Files\emucore</Filter>
</ClInclude> </ClInclude>