Skip to content

Instantly share code, notes, and snippets.

@vifino
Last active June 1, 2025 14:03
Show Gist options
  • Select an option

  • Save vifino/b96fe0ba8adcb7bca205c5172b3a12fe to your computer and use it in GitHub Desktop.

Select an option

Save vifino/b96fe0ba8adcb7bca205c5172b3a12fe to your computer and use it in GitHub Desktop.
Tiny helper to set ASYNC_LOW_LATENCY on Linux Serial Ports, since setserial is dead.
// Tiny helper to set ASYNC_LOW_LATENCY on Linux Serial Ports, since setserial is dead.
// ISC (c) 2025 Adrian "vifino" Pistol
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/serial.h>
static void usage(const char *prog) {
fprintf(stderr,
"Usage: %s [ -o | --on ] [ -f | --off ] <device>\n"
"\t-e, --enable\t\tenable ASYNC_LOW_LATENCY\n"
"\t-f, --disable\t\tdisable ASYNC_LOW_LATENCY\n",
prog);
exit(1);
}
int main(int argc, char *argv[]) {
int enable = -1;
int c;
static struct option long_opts[] = {
{"enable", no_argument, 0, 'e'},
{"disable", no_argument, 0, 'd'},
{0, 0, 0, 0 }
};
while ((c = getopt_long(argc, argv, "ed", long_opts, NULL)) != -1) {
switch (c) {
case 'e':
enable = 1;
break;
case 'd':
enable = 0;
break;
default:
usage(argv[0]);
}
}
if (enable < 0 || optind >= argc) {
usage(argv[0]);
}
const char *dev = argv[optind];
int fd = open(dev, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror("open");
return 1;
}
struct serial_struct ser;
if (ioctl(fd, TIOCGSERIAL, &ser) < 0) {
perror("TIOCGSERIAL");
close(fd);
return 1;
}
if (enable)
ser.flags |= ASYNC_LOW_LATENCY;
else
ser.flags &= ~ASYNC_LOW_LATENCY;
if (ioctl(fd, TIOCSSERIAL, &ser) < 0) {
perror("TIOCSSERIAL");
close(fd);
return 1;
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment