libgslutil  1.2.3
Functions
Serial port routines

Functions

int serial_port_open (const char *path, int flags, int baud))
 Open serial port for I/O. More...
 
int serial_port_setbaud (int fd, int baud)
 Set baud rate of serial port. More...
 

Detailed Description

Routines for opening and initialising serial ports.

Warning
The original version of serial_port_open had a problem with its flags parameter. This was defined as taking a bitmask including GSL_NONBLOCK or CLOCAL (undocumented, but widely used). However, these flags actually have the same bitmask! This has been resolved by defining a new GSL_SERIAL_PORT_CONTROL_LINES bit, with a different value and a different sense compared to CLOCAL, but this of course requires an audit of all users to ensure the serial port is in the correct mode.

Function Documentation

◆ serial_port_open()

int serial_port_open ( const char *  path,
int  flags,
int  baud 
)

Open serial port for I/O.

Parameters
pathPath to device node.
flagsFlags to open with.
baudBaud rate.
Returns
File descriptor.
Return values
-1on error

A convenience function that opens the serial port path O_RDWR | O_NOCTTY. flags may contain GSL_NONBLOCK to open for non-blocking I/O, and GSL_SERIAL_PORT_CONTROL_LINES to inhibit the CLOCAL bit (i.e. to honour the serial port control lines). If setting non-blocking mode or setting the requested baud rate fails, the file descriptor will be closed and -1 returned.

This automatically sets the port for raw serial mode, 8 data bits, no parity, 1 stop bit etc.

See also
set_non_blocking(), serial_port_setbaud()

◆ serial_port_setbaud()

int serial_port_setbaud ( int  fd,
int  baud 
)

Set baud rate of serial port.

Parameters
fdSerial port file descriptor.
baudNew baud rate.
Return values
0on success.
-1on error.

Sets the baud rate of a serial port fd to the value in baud. This value is the actual numerical baud rate, not a constant (as per cfsetspeed(3)). Passing a B... constant will cause incorrect results.

If the baud rate is not a standard rate, this function will try to set the non-standard rate using the divisor method. If the requested baud rate cannot be matched to within 5%, an error will be returned. If the requested baud rate is not an integral divisor of the crystal frequency, two stop bits will be turned on.

This will only change the baud rate and stop bits, not any of the other port settings.