Pregunta

By default what is the baud rate during serial communication. That is if a write a program where I do not mention any baud rate, then what baud rate will be taken into account?

¿Fue útil?

Solución

If on a POSIX system:

  1. Open a port using open().
  2. Pass the the file-descriptor from 1. to tcgetattr() to initialise a struct termios.
  3. Pass a reference to the struct termios from 2. to cfgetispeed()/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);

Otros consejos

You can use setserial to find out http://linux.die.net/man/8/setserial

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top