Serialib  2.0
Multi plateform serial library
serialib Class Reference

This class is used for communication over a serial device. More...

#include <serialib.h>

Public Member Functions

 serialib ()
 Constructor of the class serialib.
 
 ~serialib ()
 Destructor of the class serialib. It close the connection.
 
char openDevice (const char *Device, const unsigned int Bauds, SerialDataBits Databits=SERIAL_DATABITS_8, SerialParity Parity=SERIAL_PARITY_NONE, SerialStopBits Stopbits=SERIAL_STOPBITS_1)
 Open the serial port. More...
 
void closeDevice ()
 Close the connection with the current device.
 
char writeChar (char)
 Write a char on the current serial port. More...
 
char readChar (char *pByte, const unsigned int timeOut_ms=0)
 Wait for a byte from the serial device and return the data read. More...
 
char writeString (const char *String)
 Write a string on the current serial port. More...
 
int readString (char *receivedString, char finalChar, unsigned int maxNbBytes, const unsigned int timeOut_ms=0)
 Read a string from the serial device (with timeout) More...
 
char writeBytes (const void *Buffer, const unsigned int NbBytes)
 Write an array of data on the current serial port. More...
 
int readBytes (void *buffer, unsigned int maxNbBytes, const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100)
 Read an array of bytes from the serial device (with timeout) More...
 
char flushReceiver ()
 Empty receiver buffer. More...
 
int available ()
 Return the number of bytes in the received buffer (UNIX only) More...
 
bool DTR (bool status)
 Set or unset the bit DTR (pin 4) DTR stands for Data Terminal Ready Convenience method :This method calls setDTR and clearDTR. More...
 
bool setDTR ()
 Set the bit DTR (pin 4) DTR stands for Data Terminal Ready. More...
 
bool clearDTR ()
 Clear the bit DTR (pin 4) DTR stands for Data Terminal Ready. More...
 
bool RTS (bool status)
 Set or unset the bit RTS (pin 7) RTS stands for Data Termina Ready Convenience method :This method calls setDTR and clearDTR. More...
 
bool setRTS ()
 Set the bit RTS (pin 7) RTS stands for Data Terminal Ready. More...
 
bool clearRTS ()
 Clear the bit RTS (pin 7) RTS stands for Data Terminal Ready. More...
 
bool isRI ()
 Get the RING's status (pin 9) Ring Indicator. More...
 
bool isDCD ()
 Get the DCD's status (pin 1) CDC stands for Data Carrier Detect. More...
 
bool isCTS ()
 Get the CTS's status (pin 8) CTS stands for Clear To Send. More...
 
bool isDSR ()
 Get the DSR's status (pin 6) DSR stands for Data Set Ready. More...
 
bool isRTS ()
 Get the RTS's status (pin 7) RTS stands for Request To Send May behave abnormally on Windows. More...
 
bool isDTR ()
 Get the DTR's status (pin 4) DTR stands for Data Terminal Ready May behave abnormally on Windows. More...
 

Detailed Description

This class is used for communication over a serial device.

Member Function Documentation

◆ available()

int serialib::available ( )

Return the number of bytes in the received buffer (UNIX only)

Returns
The number of bytes received by the serial provider but not yet read.

◆ clearDTR()

bool serialib::clearDTR ( )

Clear the bit DTR (pin 4) DTR stands for Data Terminal Ready.

Returns
If the function fails, the return value is false If the function succeeds, the return value is true.

◆ clearRTS()

bool serialib::clearRTS ( )

Clear the bit RTS (pin 7) RTS stands for Data Terminal Ready.

Returns
If the function fails, the return value is false If the function succeeds, the return value is true.

◆ DTR()

bool serialib::DTR ( bool  status)

Set or unset the bit DTR (pin 4) DTR stands for Data Terminal Ready Convenience method :This method calls setDTR and clearDTR.

Parameters
status= true set DTR status = false unset DTR
Returns
If the function fails, the return value is false If the function succeeds, the return value is true.

◆ flushReceiver()

char serialib::flushReceiver ( )

Empty receiver buffer.

Returns
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

◆ isCTS()

bool serialib::isCTS ( )

Get the CTS's status (pin 8) CTS stands for Clear To Send.

Returns
Return true if CTS is set otherwise false

◆ isDCD()

bool serialib::isDCD ( )

Get the DCD's status (pin 1) CDC stands for Data Carrier Detect.

Returns
true if DCD is set
false otherwise

◆ isDSR()

bool serialib::isDSR ( )

Get the DSR's status (pin 6) DSR stands for Data Set Ready.

Returns
Return true if DTR is set otherwise false

◆ isDTR()

bool serialib::isDTR ( )

Get the DTR's status (pin 4) DTR stands for Data Terminal Ready May behave abnormally on Windows.

Returns
Return true if CTS is set otherwise false

◆ isRI()

bool serialib::isRI ( )

Get the RING's status (pin 9) Ring Indicator.

Returns
Return true if RING is set otherwise false

◆ isRTS()

bool serialib::isRTS ( )

Get the RTS's status (pin 7) RTS stands for Request To Send May behave abnormally on Windows.

Returns
Return true if RTS is set otherwise false

◆ openDevice()

char serialib::openDevice ( const char *  Device,
const unsigned int  Bauds,
SerialDataBits  Databits = SERIAL_DATABITS_8,
SerialParity  Parity = SERIAL_PARITY_NONE,
SerialStopBits  Stopbits = SERIAL_STOPBITS_1 
)

Open the serial port.

Parameters
Device: Port name (COM1, COM2, ... for Windows ) or (/dev/ttyS0, /dev/ttyACM0, /dev/ttyUSB0 ... for linux)
Bauds: Baud rate of the serial port.
       \n Supported baud rate for Windows :
               - 110
               - 300
               - 600
               - 1200
               - 2400
               - 4800
               - 9600
               - 14400
               - 19200
               - 38400
               - 56000
               - 57600
               - 115200
               - 128000
               - 256000

      \n Supported baud rate for Linux :\n
               - 110
               - 300
               - 600
               - 1200
               - 2400
               - 4800
               - 9600
               - 19200
               - 38400
               - 57600
               - 115200
Databits: Number of data bits in one UART transmission.
   \n Supported values: \n
       - SERIAL_DATABITS_5 (5)
       - SERIAL_DATABITS_6 (6)
       - SERIAL_DATABITS_7 (7)
       - SERIAL_DATABITS_8 (8)
       - SERIAL_DATABITS_16 (16) (not supported on Unix)
ParityParity type
   \n Supported values: \n
       - SERIAL_PARITY_NONE (N)
       - SERIAL_PARITY_EVEN (E)
       - SERIAL_PARITY_ODD (O)
       - SERIAL_PARITY_MARK (MARK) (not supported on Unix)
       - SERIAL_PARITY_SPACE (SPACE) (not supported on Unix)
StopbitNumber of stop bits
    \n Supported values:
        - SERIAL_STOPBITS_1 (1)
        - SERIAL_STOPBITS_1_5 (1.5) (not supported on Unix)
        - SERIAL_STOPBITS_2 (2)
Returns
1 success
-1 device not found
-2 error while opening the device
-3 error while getting port parameters
-4 Speed (Bauds) not recognized
-5 error while writing port parameters
-6 error while writing timeout parameters
-7 Databits not recognized
-8 Stopbits not recognized
-9 Parity not recognized

◆ readBytes()

int serialib::readBytes ( void *  buffer,
unsigned int  maxNbBytes,
const unsigned int  timeOut_ms = 0,
unsigned int  sleepDuration_us = 100 
)

Read an array of bytes from the serial device (with timeout)

Parameters
buffer: array of bytes read from the serial device
maxNbBytes: maximum allowed number of bytes read
timeOut_ms: delay of timeout before giving up the reading
sleepDuration_us: delay of CPU relaxing in microseconds (Linux only) In the reading loop, a sleep can be performed after each reading This allows CPU to perform other tasks
Returns
>=0 return the number of bytes read before timeout or requested data is completed
-1 error while setting the Timeout
-2 error while reading the byte

◆ readChar()

char serialib::readChar ( char *  pByte,
const unsigned int  timeOut_ms = 0 
)

Wait for a byte from the serial device and return the data read.

Parameters
pByte: data read on the serial device
timeOut_ms: delay of timeout before giving up the reading If set to zero, timeout is disable (Optional)
Returns
1 success
0 Timeout reached
-1 error while setting the Timeout
-2 error while reading the byte

◆ readString()

int serialib::readString ( char *  receivedString,
char  finalChar,
unsigned int  maxNbBytes,
const unsigned int  timeOut_ms = 0 
)

Read a string from the serial device (with timeout)

Parameters
receivedString: string read on the serial device
finalChar: final char of the string
maxNbBytes: maximum allowed number of bytes read
timeOut_ms: delay of timeout before giving up the reading (optional)
Returns
>0 success, return the number of bytes read
0 timeout is reached
-1 error while setting the Timeout
-2 error while reading the byte
-3 MaxNbBytes is reached

◆ RTS()

bool serialib::RTS ( bool  status)

Set or unset the bit RTS (pin 7) RTS stands for Data Termina Ready Convenience method :This method calls setDTR and clearDTR.

Parameters
status= true set DTR status = false unset DTR
Returns
false if the function fails
true if the function succeeds

◆ setDTR()

bool serialib::setDTR ( )

Set the bit DTR (pin 4) DTR stands for Data Terminal Ready.

Returns
If the function fails, the return value is false If the function succeeds, the return value is true.

◆ setRTS()

bool serialib::setRTS ( )

Set the bit RTS (pin 7) RTS stands for Data Terminal Ready.

Returns
If the function fails, the return value is false If the function succeeds, the return value is true.

◆ writeBytes()

char serialib::writeBytes ( const void *  Buffer,
const unsigned int  NbBytes 
)

Write an array of data on the current serial port.

Parameters
Buffer: array of bytes to send on the port
NbBytes: number of byte to send
Returns
1 success
-1 error while writting data

◆ writeChar()

char serialib::writeChar ( char  Byte)

Write a char on the current serial port.

Parameters
Byte: char to send on the port (must be terminated by '\0')
Returns
1 success
-1 error while writting data

◆ writeString()

char serialib::writeString ( const char *  receivedString)

Write a string on the current serial port.

Parameters
receivedString: string to send on the port (must be terminated by '\0')
Returns
1 success
-1 error while writting data

The documentation for this class was generated from the following files: