Skip to content

Commit

Permalink
set low latency serial port
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxxzer committed Oct 29, 2019
1 parent 64f63d4 commit b38f746
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions com-handle.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
#include <com-handle.h>

#include <QDebug>

#include <sys/ioctl.h>
#include <termios.h>
#include <linux/serial.h>

bool ComHandle::setLowLatency()
{
auto handle = serialPort->handle();

if(!handle) {
qWarning() << "Failed to get serial handle from OS.";
return false;
}

// Our first attempt is with termios2
struct termios2 {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[19]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
} tio2;
if (::ioctl(handle, TCGETS2, &tio2) != -1) {
// If it's already in low latency, no further configuration is necessary
if (!(tio2.c_cflag & ASYNC_LOW_LATENCY)) {
tio2.c_cflag |= ASYNC_LOW_LATENCY;
::ioctl(handle, TCSETS2, &tio2);
} else {
qDebug() << "Low latency mode is already in termios2.";
}
} else {
qWarning() << "Failed to get termios2 struct from system.";
}

// Check again same configuration with serial_struct
serial_struct serial;
::memset(&serial, 0, sizeof(serial));
if (::ioctl(handle, TIOCGSERIAL, &serial) == -1) {
qWarning() << "Failed to get serial_struct from system.";
return false;
}

// If it's already in low latency, no further configuration is necessary
if (serial.flags & ASYNC_LOW_LATENCY) {
qDebug() << "Low latency mode is already in serial_struct.";
return true;
}

// It's not possible to check for errors since the driver may not support it
serial.flags |= ASYNC_LOW_LATENCY;
::ioctl(handle, TIOCSSERIAL, &serial);

//light at the end of the tunnel
return true;
}
ComHandle::ComHandle(QSerialPortInfo p)
{
serialPort = new QSerialPort(p);
setLowLatency();
}

bool ComHandle::open()
Expand Down
1 change: 1 addition & 0 deletions com-handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ComHandle : public QObject
void close();

void write(uint8_t* data, uint16_t length);
bool setLowLatency();

QSerialPort* serialPort;

Expand Down

0 comments on commit b38f746

Please sign in to comment.