Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I regularly use cat to view debugging information in the console from my FPGA development board over the serial connection, but I never have had to tell linux what the baud rate is. How does cat know what the baud rate of the serial connection is?

share|improve this question
    
You did not set up the port e.g. with minicom before? It doesn't work here. Only after I set up the serial port parameters I can use cat. –  Marco Apr 19 '13 at 9:11
    
It doesn't set or knows the baudrate, it just reads from the device. –  Ulrich Dangel Apr 19 '13 at 9:12
    
@Marco, I don't know if Debian has some default baud rate setting, but I haven't set it anywhere. –  Stacey Anne Apr 19 '13 at 9:18
add comment

2 Answers

up vote 8 down vote accepted

The stty utility sets or reports on terminal I/O characteristics for the device that is its standard input. These characteristics are used when establishing a connection over that particular medium. cat doesn't know the baud rate as such, it rather prints on the screen information received from the particular connection.

As an example stty -F /dev/ttyACM0 gives the current baud rate for the ttyACM0 device.

share|improve this answer
add comment

cat just uses whatever settings the port is already configured for. With this little C snippet you can see the baud rate currently set for a particular serial port:

get-baud-rate.c

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  struct termios tios;
  tcgetattr(0, &tios);
  speed_t ispeed = cfgetispeed(&tios);
  speed_t ospeed = cfgetospeed(&tios);
  printf("baud rate in: 0%o\n", ispeed);
  printf("baud rate out: 0%o\n", ospeed);
  return 0;
}

Run it:

./get-baud-rate < /dev/ttyS0 # or whatever your serial port is

The numbers you get can be looked up in /usr/include/asm-generic/termios.h, where there are #defines such as B9600 etc. Note that the numbers in the header file and in the get-baud-rate output are in octal.

Maybe you can experiment and see what these numbers are like on a fresh boot and whether they change later.

share|improve this answer
    
I just found the stty command which does just this. For example, stty -F /dev/ttyACM0 gives me the current baud rate, which is correct for my device. –  Stacey Anne Apr 19 '13 at 9:33
    
Of course that's a much better idea. –  clacke Apr 19 '13 at 10:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.