Hardware Reference
In-Depth Information
cfsetspeed(3)
Most serial communication uses a common baud rate for transmitting and receiving. For
this reason, this is the preferred function to invoke for establishing both the input and
output baud rates:
int cfsetspeed(struct termios
*
termios_p, speed_t speed);
where
termios_p
is the pointer to the TTY configuration structure to be modified.
speed
is the input and output baud rate to apply.
Note that this function only updates the
termios
data structure with no direct effect
on the device being used.
struct termios term;
int rc;
rc = cfsetspeed(&term,9600);
if ( rc < 0 ) {
perror("cfsetsspeed(3)");
read(2)
The
read(2)
system call can be used for reading from the serial port, in addition to
normal Linux files and other devices:
#include <unistd.h>
ssize_t read(int fd, void
*
buf, size_t count);
where
fd
is the open file descriptor to read from.
buf
is the buffer to read the data into.
count
is the maximum number of bytes to read.
returns an
int
, where
-1
indicates an error, with the error code found in
errno
.
0
indicates that the serial port has been closed with the end-of-file
character.
>0 indicates the number of bytes read.