If on a POSIX system:
- Open a port using
open()
. - Pass the the file-descriptor from 1. to
tcgetattr()
to initialise astruct termios
. - Pass a reference to the
struct termios
from 2. tocfgetispeed()
/cfgetospeed()
to get the port's current inbound/outbound baud rate.
Example:
#include <termios.h>
#include <unistd.h>
[...]
struct termios t = {0};
speed_t baudrate_in = 0;
speed_t baudrate_out = 0;
int fd = open("/dev/ttyS0", O_RDWR);
if (-1 == fd)
{
perror("open() failed");
exit(1);
}
if (-1 == tcgetattr(fd, &t))
{
perror("tcgetattr() failed");
exit(1);
}
baudrate_in = cfgetispeed(&t);
baudrate_out = cfgetospeed(&t);