#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/termios.h>
int ioctl(int d, int request, ...);
int main(int argc, char *argv[]) {
struct termios2 t;
int fd,baud;
if (argc != 3) {
fprintf(stderr,"usage: %s <device> <baud>\n", argv[0]);
return 1;
}
fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
fprintf(stderr, "error opening %s: %s", argv[1], strerror(errno));
return 2;
}
baud = atoi(argv[2]);
if (ioctl(fd, TCGETS2, &t)) {
perror("TCGETS2");
return 3;
}
t.c_cflag &= ~CBAUD;
t.c_cflag |= BOTHER;
t.c_ispeed = baud;
t.c_ospeed = baud;
if (ioctl(fd, TCSETS2, &t)) {
perror("TCSETS2");
return 4;
}
if (ioctl(fd, TCGETS2, &t)) {
perror("TCGETS2");
return 5;
}
printf("actual speed reported %d\n", t.c_ospeed);
return 0;
}
The example is working on a USB serial adapter but not on the UART of the iMX6SX:
# stty -F /dev/ttymxc4
NUM: 0x29
DENOM: 0xC34
speed 9600 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel
# ./test_arm /dev/ttymxc4 115200
NUM: 0x29
DENOM: 0xC34
NUM: 0x1F7
DENOM: 0xC34
actual speed reported 115200
# stty -F /dev/ttymxc4
NUM: 0x1F7
DENOM: 0xC34
speed 115200 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel
# ./test_arm /dev/ttymxc4 1000000
NUM: 0x1F7
DENOM: 0xC34
NUM: 0x00
DENOM: 0x00
actual speed reported 1000000
# stty -F /dev/ttymxc4
NUM: 0x00
DENOM: 0x00
speed 0 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel