Skip to content

Instantly share code, notes, and snippets.

@lategoodbye
Forked from peterhurley/anybaud.c
Last active November 4, 2022 19:54
Show Gist options
  • Save lategoodbye/f2d76134aa6c404cd92c to your computer and use it in GitHub Desktop.
Save lategoodbye/f2d76134aa6c404cd92c to your computer and use it in GitHub Desktop.
/*
* Allows to set arbitrary speed for the serial device on Linux.
* stty allows to set only predefined values: 9600, 19200, 38400, 57600, 115200, 230400, 460800.
*/
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <asm/termbits.h>
#define error_exit(f, args...) \
printf_exit("%s: " f ": %s (code: %d)\n", __func__, ##args, \
strerror(errno), errno)
static void printf_exit(char *f, ...) {
va_list va;
va_start(va, f);
vfprintf(stderr, f, va);
va_end(va);
exit(EXIT_FAILURE);
}
static void usage(char* argv[]) {
printf("%s device speed\n\n"
"Set speed for a serial device.\n"
"For instance:\n"
" %s /dev/ttyUSB0 75000\n", argv[0], argv[0]);
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[]) {
int fd, speed;
struct termios2 tio;
setbuf(stdout, NULL);
if (argc != 3)
usage(argv);
fd = open(argv[1], O_RDONLY);
if (fd < 0)
error_exit("opening tty");
speed = atoi(argv[2]);
if (ioctl(fd, TCGETS2, &tio) < 0)
error_exit("TCGETS2 ioctl");
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = speed;
tio.c_ospeed = speed;
if (ioctl(fd, TCSETS2, &tio) < 0)
error_exit("TCSETS2 ioctl");
if (ioctl(fd, TCGETS2, &tio) < 0)
error_exit("TCGETS2 ioctl");
close(fd);
printf("%s speed changed to %d baud\n", argv[1], tio.c_ospeed);
return 0;
}
@geneshuman
Copy link

Thanks, this was incredibly helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment