Add macOS USB UART support
This commit is contained in:
+57
-21
@@ -10,38 +10,70 @@
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef Q_OS_MACOS
|
||||
#include <IOKit/serial/ioss.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr qsizetype MaxBytesPerReadCycle = 64 * 1024;
|
||||
|
||||
speed_t baudToSpeed(int baudRate)
|
||||
bool applyBaudRate(termios &options, int baudRate)
|
||||
{
|
||||
speed_t speed = 0;
|
||||
switch (baudRate) {
|
||||
case 1200:
|
||||
return B1200;
|
||||
speed = B1200;
|
||||
break;
|
||||
case 2400:
|
||||
return B2400;
|
||||
speed = B2400;
|
||||
break;
|
||||
case 4800:
|
||||
return B4800;
|
||||
speed = B4800;
|
||||
break;
|
||||
case 9600:
|
||||
return B9600;
|
||||
speed = B9600;
|
||||
break;
|
||||
case 19200:
|
||||
return B19200;
|
||||
speed = B19200;
|
||||
break;
|
||||
case 38400:
|
||||
return B38400;
|
||||
speed = B38400;
|
||||
break;
|
||||
case 57600:
|
||||
return B57600;
|
||||
speed = B57600;
|
||||
break;
|
||||
case 115200:
|
||||
return B115200;
|
||||
speed = B115200;
|
||||
break;
|
||||
#ifdef B230400
|
||||
case 230400:
|
||||
return B230400;
|
||||
speed = B230400;
|
||||
break;
|
||||
#endif
|
||||
#ifdef B460800
|
||||
case 460800:
|
||||
return B460800;
|
||||
speed = B460800;
|
||||
break;
|
||||
#endif
|
||||
#ifdef B921600
|
||||
case 921600:
|
||||
return B921600;
|
||||
speed = B921600;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
#ifdef Q_OS_MACOS
|
||||
// IOSSIOSPEED below applies the exact requested rate. A valid termios
|
||||
// placeholder is still required before that ioctl can be issued.
|
||||
speed = B9600;
|
||||
break;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
return ::cfsetispeed(&options, speed) == 0
|
||||
&& ::cfsetospeed(&options, speed) == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -176,12 +208,6 @@ void SerialReader::flushPartialLine()
|
||||
|
||||
bool SerialReader::configurePort(int baudRate)
|
||||
{
|
||||
const speed_t speed = baudToSpeed(baudRate);
|
||||
if (speed == 0) {
|
||||
emit errorOccurred(QStringLiteral("Desteklenmeyen baud rate: %1").arg(baudRate));
|
||||
return false;
|
||||
}
|
||||
|
||||
termios options {};
|
||||
if (::tcgetattr(m_fd, &options) != 0) {
|
||||
emitSystemError(QStringLiteral("Port ayarlari okunamadi"));
|
||||
@@ -189,8 +215,10 @@ bool SerialReader::configurePort(int baudRate)
|
||||
}
|
||||
|
||||
::cfmakeraw(&options);
|
||||
::cfsetispeed(&options, speed);
|
||||
::cfsetospeed(&options, speed);
|
||||
if (!applyBaudRate(options, baudRate)) {
|
||||
emit errorOccurred(QStringLiteral("Bu sistemde desteklenmeyen baud rate: %1").arg(baudRate));
|
||||
return false;
|
||||
}
|
||||
|
||||
options.c_cflag |= CLOCAL | CREAD;
|
||||
options.c_cflag &= ~HUPCL;
|
||||
@@ -206,6 +234,14 @@ bool SerialReader::configurePort(int baudRate)
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef Q_OS_MACOS
|
||||
speed_t requestedSpeed = static_cast<speed_t>(baudRate);
|
||||
if (::ioctl(m_fd, IOSSIOSPEED, &requestedSpeed) != 0) {
|
||||
emitSystemError(QStringLiteral("macOS baud rate uygulanamadi"));
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
::tcflush(m_fd, TCIFLUSH);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user