![]() |
libserial
1.6.2
RoadNarrows Roboitics Serial Library
|
RS-232 serial device communication definitions. More...
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/serdev.h"
Go to the source code of this file.
Classes | |
struct | SerDevAttrKvp_T |
Serial Device Attribute Key - Value Pair structure. More... | |
Macros | |
#define | NOTBITS(bits) ~((uint_t)(bits)) |
Bitwise complement. More... | |
#define | SERDEVLOG(buffer, size, bNewLine) {if(LOGABLE(LOG_LEVEL_DIAG4)) {SerDevLogBytes(buffer, size, bNewLine);}} |
Log read/written bytes to log stream. More... | |
#define | SERDEVLOGNL() {if(LOGABLE(LOG_LEVEL_DIAG4)) {fprintf(LOG_GET_LOGFP(), "\n");}} |
Write newline character to log stream. | |
#define | SERDEV_FIONREAD 0x541b |
number in input queue | |
#define | SERDEV_NO_ATTR 0xffffffff |
Special "No Attribute" value. | |
#define | SERDEV_ATTR_ENTRY(attr) { (void *)#attr , attr } |
Serial device attribute macros. More... | |
#define | SERDEV_GET_ATTR(attr) |
Get I/O defined attribute value. More... | |
#define | SERDEV_BAUDRATE_ENTRY(baudrate) { (void *)((unsigned long)baudrate), B##baudrate } |
Serial device supported baudrate macros. More... | |
#define | SERDEV_GET_BAUDRATE(baudrate) |
Get baudrate defined value. More... | |
Functions | |
static void | fdset_nowarn (int fd, fd_set *pset) |
FD_SET() wrapper with no annoying warnings. More... | |
static void | SerDevLogBytes (byte_t buffer[], size_t size, bool_t bNewLine) |
Log read/written bytes to log stream. More... | |
static void | timer_mark (struct timeval *pTvMark) |
Mark the current time. Resolution is microseconds. More... | |
static uint_t | timer_elapsed (struct timeval *pTvMark) |
Calculate the elapsed time between the given time mark and this call. More... | |
static int | SerDevOptAttrCmp (const void *pKey1, const void *pKey2) |
Serial option attribute comparator. More... | |
static int | SerDevBaudRateCmp (const void *pKey1, const void *pKey2) |
BaudRate attribute comparator. More... | |
static unsigned int | SerDevAttrLookup (SerDevAttrKvp_T tbl[], size_t nTblEntries, const void *pKey, int(*cmp)(const void *, const void *)) |
Lookup the attribute value associated with the key. More... | |
static int | SerDevConfigure (int fd, int nBaudRate, int nByteSize, int cParity, int nStopBits, bool_t bRtsCts, bool_t bXonXoff) |
(Re)configure a serial device attibutes. More... | |
int | SerDevOpen (const char *sSerDevName, int nBaudRate, int nByteSize, int cParity, int nStopBits, bool_t bRtsCts, bool_t bXonXoff) |
Open and configure serial device for communication. More... | |
int | SerDevClose (int fd) |
Close serial device port. More... | |
ssize_t | SerDevReadLine (int fd, char buffer[], size_t count, char *eol, uint_t usec) |
Read a ASCII character line from the serial device. More... | |
ssize_t | SerDevWriteLine (int fd, char *buffer, char *eol, uint_t usec) |
Write null-terminated ASCII character line to serial device. More... | |
int | SerDevGetc (int fd, uint_t usec) |
Get 1 character from the serial device. More... | |
int | SerDevPutc (int fd, byte_t byte, uint_t usec) |
Put one character to serial device. More... | |
ssize_t | SerDevRead (int fd, byte_t *buffer, size_t count, uint_t usec) |
Read from the serial device. More... | |
ssize_t | SerDevWrite (int fd, byte_t *buffer, size_t count, uint_t usec) |
Write to serial device. More... | |
ssize_t | SerDevFIFOInputCount (int fd) |
Determine the number of bytes in the input FIFO of the serial device. More... | |
ssize_t | SerDevFIFOInputFlush (int fd) |
Flush the input FIFO buffer, discarding all data in buffer. More... | |
void | SerDevFIFOOutputFlush (int fd) |
Flush output FIFO buffer, discarding all data in buffer. More... | |
void | SerDevFIFOOutputDrain (int fd) |
Transmit (drain) all data written to the output FIFO buffer. More... | |
int | SerDevSetBaudRate (int fd, int nBaudRate) |
Set the baudrate. More... | |
int | SerDevSetByteSize (int fd, int nByteSize) |
Set the byte size. More... | |
int | SerDevSetParity (int fd, int cParity) |
Set the parity. More... | |
int | SerDevSetStopBits (int fd, int nStopBits) |
Set the number of stop bits. More... | |
int | SerDevSetHwFlowControl (int fd, bool_t bRtsCts) |
Set hardware flow control state. More... | |
int | SerDevSetSwFlowControl (int fd, bool_t bXonXoff) |
Set software flow control state. More... | |
int | SerDevAssertRTS (int fd) |
Assert RTS (request to send). More... | |
int | SerDevDeassertRTS (int fd) |
De-assert RTS (request to send). More... | |
int | SerDevAssertCTS (int fd) |
Assert RTS (clear to send). More... | |
int | SerDevDeassertCTS (int fd) |
De-assert RTS (clear to send). More... | |
Variables | |
static SerDevAttrKvp_T | SerDevOptAttrTbl [] |
SerDevOptAttr table. Dynamic method to determine serial device optional defined attributes for each plaform architecture. More... | |
static SerDevAttrKvp_T | SerDevBaudRateTbl [] |
SerDevBaudRate table. Dynamic method to determine Serial device supported baud rates for each plaform architecture. | |
RS-232 serial device communication definitions.
Ported from ipserddev.c (RoadNarrows)
Definition in file serdev.c.
#define NOTBITS | ( | bits | ) | ~((uint_t)(bits)) |
Bitwise complement.
bits | Bits. |
Definition at line 86 of file serdev.c.
Referenced by SerDevConfigure(), SerDevSetByteSize(), SerDevSetHwFlowControl(), SerDevSetParity(), SerDevSetStopBits(), and SerDevSetSwFlowControl().
#define SERDEV_ATTR_ENTRY | ( | attr | ) | { (void *)#attr , attr } |
#define SERDEV_BAUDRATE_ENTRY | ( | baudrate | ) | { (void *)((unsigned long)baudrate), B##baudrate } |
#define SERDEV_GET_ATTR | ( | attr | ) |
Get I/O defined attribute value.
attr | Attribute define. |
Definition at line 249 of file serdev.c.
Referenced by SerDevConfigure(), SerDevSetHwFlowControl(), and SerDevSetSwFlowControl().
#define SERDEV_GET_BAUDRATE | ( | baudrate | ) |
Get baudrate defined value.
baudrate | Integer baudrate. |
Definition at line 305 of file serdev.c.
Referenced by SerDevConfigure(), and SerDevSetBaudRate().
#define SERDEVLOG | ( | buffer, | |
size, | |||
bNewLine | |||
) | {if(LOGABLE(LOG_LEVEL_DIAG4)) {SerDevLogBytes(buffer, size, bNewLine);}} |
Log read/written bytes to log stream.
buffer | Buffer of bytes. |
size | Number of bytes in buffer to log. |
bNewLine | Do [not] output trailing newline. |
Definition at line 113 of file serdev.c.
Referenced by SerDevFIFOInputCount(), SerDevRead(), and SerDevWrite().
|
inlinestatic |
FD_SET() wrapper with no annoying warnings.
fd | File descriptor to add to set. |
pset | Pointer to fd set. |
Definition at line 94 of file serdev.c.
Referenced by SerDevGetc(), SerDevPutc(), SerDevRead(), and SerDevWrite().
int SerDevAssertCTS | ( | int | fd | ) |
Assert RTS (clear to send).
fd | File descriptor to the opened serial device. |
Definition at line 1500 of file serdev.c.
Referenced by SerDevDeassertCTS(), and SerDevIsInputDataPresent().
int SerDevAssertRTS | ( | int | fd | ) |
Assert RTS (request to send).
fd | File descriptor to the opened serial device. |
Definition at line 1458 of file serdev.c.
Referenced by SerDevDeassertCTS(), and SerDevIsInputDataPresent().
|
static |
Lookup the attribute value associated with the key.
tbl[] | Attribute Key-Value Pair table. |
nTblEntries | Number of Key-Value Pair table entries. |
pKey | Key to lookup. |
cmp | Attribute key comparator function. |
Definition at line 433 of file serdev.c.
References SerDevAttrKvp_T::m_uVal, and SERDEV_NO_ATTR.
|
inlinestatic |
int SerDevClose | ( | int | fd | ) |
Close serial device port.
fd | File descriptor to the opened serial device. |
|
static |
(Re)configure a serial device attibutes.
fd | File descriptor to the opened serial device. |
nBaudRate | Baud rate. |
nByteSize | Bytes size in bits 5...8. |
cParity | Parity. One of: 'N', 'E', 'O' |
nStopBits | Number of stop bits 1, 2 |
bRtsCts | Do [not] use hardware flow control. |
bXonXoff | Do [not] use software flow control. |
Definition at line 463 of file serdev.c.
References NOTBITS, SERDEV_GET_ATTR, SERDEV_GET_BAUDRATE, and SERDEV_NO_ATTR.
Referenced by SerDevOpen().
int SerDevDeassertCTS | ( | int | fd | ) |
De-assert RTS (clear to send).
fd | File descriptor to the opened serial device. |
Definition at line 1521 of file serdev.c.
References SerDevAssertCTS(), SerDevAssertRTS(), and SerDevDeassertRTS().
Referenced by SerDevIsInputDataPresent().
int SerDevDeassertRTS | ( | int | fd | ) |
De-assert RTS (request to send).
fd | File descriptor to the opened serial device. |
Definition at line 1479 of file serdev.c.
Referenced by SerDevDeassertCTS(), and SerDevIsInputDataPresent().
ssize_t SerDevFIFOInputCount | ( | int | fd | ) |
Determine the number of bytes in the input FIFO of the serial device.
The input FIFO holds data received but not read.
fd | File descriptor to the opened serial device. |
Definition at line 1146 of file serdev.c.
References SERDEV_FIONREAD, SerDevFIFOInputFlush(), SERDEVLOG, SERDEVLOGNL, and SerDevRead().
Referenced by SerDevFIFOInputFlush(), and SerDevIsInputDataPresent().
ssize_t SerDevFIFOInputFlush | ( | int | fd | ) |
Flush the input FIFO buffer, discarding all data in buffer.
The input FIFO holds data received but not read.
This function does not block.
fd | File descriptor to the opened serial device. |
Definition at line 1202 of file serdev.c.
References SerDevFIFOInputCount().
Referenced by SerDevFIFOInputCount().
void SerDevFIFOOutputDrain | ( | int | fd | ) |
Transmit (drain) all data written to the output FIFO buffer.
The output FIFO holds data written to the device but not transmitted.
This function blocks until all of the FIFO data has been transmitted.
fd | File descriptor to the opened serial device. |
void SerDevFIFOOutputFlush | ( | int | fd | ) |
Flush output FIFO buffer, discarding all data in buffer.
The output FIFO holds data written to the device but not transmitted.
This function does not block.
fd | File descriptor to the opened serial device. |
int SerDevGetc | ( | int | fd, |
uint_t | usec | ||
) |
Get 1 character from the serial device.
Read 1 character from the serial device input. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the read can block indefinitely.
fd | File descriptor to the opened serial device. |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the read. If usec == 0, then the read will block indefinitely until count bytes are read or an I/O error occurred. |
Definition at line 843 of file serdev.c.
References fdset_nowarn().
Referenced by SerDevReadLine().
|
static |
Log read/written bytes to log stream.
buffer | Buffer of bytes. |
size | Number of bytes in buffer to log. |
bNewLine | Do [not] output trailing newline. |
int SerDevOpen | ( | const char * | sSerDevName, |
int | nBaudRate, | ||
int | nByteSize, | ||
int | cParity, | ||
int | nStopBits, | ||
bool_t | bRtsCts, | ||
bool_t | bXonXoff | ||
) |
Open and configure serial device for communication.
sSerDevName | Serial device port name (e.g. /dev/ttyS0). |
nBaudRate | Baud rate. |
nByteSize | Bytes size in bits 5...8. |
cParity | Parity. One of: 'N', 'E', 'O' |
nStopBits | Number of stop bits 1, 2 |
bRtsCts | Do [not] use hardware flow control. |
bXonXoff | Do [not] use software flow control. |
Definition at line 663 of file serdev.c.
References SerDevConfigure().
|
inlinestatic |
int SerDevPutc | ( | int | fd, |
byte_t | byte, | ||
uint_t | usec | ||
) |
Put one character to serial device.
Write 1 character to the serial device output. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the write can block indefinitely.
fd | File descriptor to the opened serial device. |
byte | Character to write. |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the write. If usec == 0, then the write will block indefinitely until count bytes are written or an I/O error occurred. |
Definition at line 910 of file serdev.c.
References fdset_nowarn().
ssize_t SerDevRead | ( | int | fd, |
byte_t * | buffer, | ||
size_t | count, | ||
uint_t | usec | ||
) |
Read from the serial device.
Read up to count bytes into buffer from the serial device input. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the read can block indefinitely.
Note the the bytes read can be less than the count.
fd | File descriptor to the opened serial device. |
buffer | Output read buffer. |
count | Number of bytes to read. |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the read. If usec == 0, then the read will block indefinitely until count bytes are read or an I/O error occurred. |
Definition at line 976 of file serdev.c.
References fdset_nowarn(), SERDEVLOG, timer_elapsed(), and timer_mark().
Referenced by SerDevFIFOInputCount().
ssize_t SerDevReadLine | ( | int | fd, |
char | buffer[], | ||
size_t | count, | ||
char * | eol, | ||
uint_t | usec | ||
) |
Read a ASCII character line from the serial device.
Read up to count bytes into buffer from the serial device input. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the read can block indefinitely.
The end-of-line sequence defines the end of the line. It is stripped from the return buffer.
fd | File descriptor to the opened serial device. | |
[out] | buffer | Output read buffer. |
count | Number of bytes to read. | |
eol | End-of-line character sequence > 0. | |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the read. If usec == 0, then the read will block indefinitely until count bytes are read or an I/O error occurred. |
Definition at line 720 of file serdev.c.
References SerDevGetc(), timer_elapsed(), and timer_mark().
int SerDevSetBaudRate | ( | int | fd, |
int | nBaudRate | ||
) |
Set the baudrate.
fd | File descriptor to the opened serial device. |
nBaudRate | Baud rate. |
Definition at line 1230 of file serdev.c.
References SERDEV_GET_BAUDRATE, and SERDEV_NO_ATTR.
Referenced by SerDevIsInputDataPresent().
int SerDevSetByteSize | ( | int | fd, |
int | nByteSize | ||
) |
Set the byte size.
fd | File descriptor to the opened serial device. |
nByteSize | Bytes size in bits 5...8. |
Definition at line 1266 of file serdev.c.
References NOTBITS.
Referenced by SerDevIsInputDataPresent().
int SerDevSetHwFlowControl | ( | int | fd, |
bool_t | bRtsCts | ||
) |
Set hardware flow control state.
fd | File descriptor to the opened serial device. |
bRtsCts | Do [not] use hardware flow control. |
Definition at line 1350 of file serdev.c.
References NOTBITS, SERDEV_GET_ATTR, and SERDEV_NO_ATTR.
Referenced by SerDevIsInputDataPresent().
int SerDevSetParity | ( | int | fd, |
int | cParity | ||
) |
Set the parity.
fd | File descriptor to the opened serial device. |
cParity | Parity. One of: 'N', 'E', 'O' |
Definition at line 1297 of file serdev.c.
References NOTBITS.
Referenced by SerDevIsInputDataPresent().
int SerDevSetStopBits | ( | int | fd, |
int | nStopBitgs | ||
) |
Set the number of stop bits.
fd | File descriptor to the opened serial device. |
nStopBits | Number of stop bits 1, 2 |
Definition at line 1326 of file serdev.c.
References NOTBITS.
Referenced by SerDevIsInputDataPresent().
int SerDevSetSwFlowControl | ( | int | fd, |
bool_t | bXonXoff | ||
) |
Set software flow control state.
fd | File descriptor to the opened serial device. |
bXonXoff | Do [not] use software flow control. |
Definition at line 1410 of file serdev.c.
References NOTBITS, SERDEV_GET_ATTR, and SERDEV_NO_ATTR.
Referenced by SerDevIsInputDataPresent().
ssize_t SerDevWrite | ( | int | fd, |
byte_t * | buffer, | ||
size_t | count, | ||
uint_t | usec | ||
) |
Write to serial device.
Write up to count bytes from the buffer to the serial device output. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the write can block indefinitely.
Note that the number of bytes written can be less than the count.
fd | File descriptor to the opened serial device. |
buffer | Input write buffer. |
count | Number of of bytes to write. |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the write. If usec == 0, then the write will block indefinitely until count bytes are written or an I/O error occurred. |
Definition at line 1061 of file serdev.c.
References fdset_nowarn(), SERDEVLOG, timer_elapsed(), and timer_mark().
Referenced by SerDevWriteLine().
ssize_t SerDevWriteLine | ( | int | fd, |
char * | buffer, | ||
char * | eol, | ||
uint_t | usec | ||
) |
Write null-terminated ASCII character line to serial device.
Write line plus end-of-line sequence. This call is non-blocking if the timeout value usec is greater than zero. Otherwise the write can block indefinitely.
fd | File descriptor to the opened serial device. | |
[in] | buffer | Buffer to write. |
eol | End-of-line character sequence to append to buffer output. | |
usec | Timeout in microseconds. If usec > 0, an upper timeout limit is placed on the write. If usec == 0, then the write will block indefinitely until count bytes are written or an I/O error occurred. |
Definition at line 812 of file serdev.c.
References SerDevWrite().
|
static |
Calculate the elapsed time between the given time mark and this call.
pTvMark | Pointer to timeval holding time mark. |
Definition at line 192 of file serdev.c.
References timer_mark().
Referenced by SerDevRead(), SerDevReadLine(), and SerDevWrite().
|
inlinestatic |
Mark the current time. Resolution is microseconds.
pTvMark | Pointer to timeval structure to be populated with the current system seconds and useconds. |
Definition at line 174 of file serdev.c.
Referenced by SerDevRead(), SerDevReadLine(), SerDevWrite(), and timer_elapsed().
|
static |