LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
|
SerialPort allows an object oriented approach to serial port communication. A serial port object can be created to allow opening the port with specified modes and settings. The SerialPort class also provides Get/Set methods to access the most commonly utilized parameters associated with serial port communication. More...
#include <SerialPort.h>
Classes | |
class | Implementation |
SerialPort::Implementation is the SerialPort implementation class. More... | |
Public Member Functions | |
SerialPort () | |
Default Constructor. | |
SerialPort (const std::string &fileName, const BaudRate &baudRate=BaudRate::BAUD_DEFAULT, const CharacterSize &characterSize=CharacterSize::CHAR_SIZE_DEFAULT, const FlowControl &flowControlType=FlowControl::FLOW_CONTROL_DEFAULT, const Parity &parityType=Parity::PARITY_DEFAULT, const StopBits &stopBits=StopBits::STOP_BITS_DEFAULT, bool exclusive=true) | |
Constructor that allows a SerialPort instance to be created and opened, initializing the corresponding serial port with the specified parameters. | |
virtual | ~SerialPort () noexcept |
Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if open. | |
SerialPort (const SerialPort &otherSerialPort)=delete | |
Copy construction is disallowed. | |
SerialPort (SerialPort &&otherSerialPort) | |
Move construction is allowed. | |
SerialPort & | operator= (const SerialPort &otherSerialPort)=delete |
Copy assignment is disallowed. | |
SerialPort & | operator= (SerialPort &&otherSerialPort) |
Move assignment is allowed. | |
void | Open (const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true) |
Opens the serial port associated with the specified file name and the specified mode. | |
void | Close () |
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port. | |
void | DrainWriteBuffer () |
Waits until the write buffer is drained and then returns. | |
void | FlushInputBuffer () |
Flushes the serial port input buffer. | |
void | FlushOutputBuffer () |
Flushes the serial port output buffer. | |
void | FlushIOBuffers () |
Flushes the serial port input and output buffers. | |
bool | IsDataAvailable () |
Checks if data is available at the input of the serial port. | |
bool | IsOpen () const |
Determines if the serial port is open for I/O. | |
void | SetDefaultSerialPortParameters () |
Sets all serial port paramters to their default values. | |
void | SetBaudRate (const BaudRate &baudRate) |
Sets the baud rate for the serial port to the specified value. | |
BaudRate | GetBaudRate () const |
Gets the current baud rate for the serial port. | |
void | SetCharacterSize (const CharacterSize &characterSize) |
Sets the character size for the serial port. | |
CharacterSize | GetCharacterSize () const |
Gets the character size being used for serial communication. | |
void | SetFlowControl (const FlowControl &flowControlType) |
Sets flow control for the serial port. | |
FlowControl | GetFlowControl () const |
Gets the current flow control setting. | |
void | SetParity (const Parity &parityType) |
Sets the parity type for the serial port. | |
Parity | GetParity () const |
Gets the parity type for the serial port. | |
void | SetStopBits (const StopBits &stopBits) |
Sets the number of stop bits to be used with the serial port. | |
StopBits | GetStopBits () const |
Gets the number of stop bits currently being used by the serial. | |
void | SetVMin (const short vmin) |
Sets the minimum number of characters for non-canonical reads. | |
short | GetVMin () const |
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads. | |
void | SetVTime (const short vtime) |
Sets character buffer timeout for non-canonical reads in deciseconds. | |
short | GetVTime () const |
Gets the current timeout value for non-canonical reads in deciseconds. | |
void | SetDTR (const bool dtrState=true) |
Sets the DTR line to the specified value. | |
bool | GetDTR () const |
Gets the status of the DTR line. | |
void | SetRTS (const bool rtsState=true) |
Set the RTS line to the specified value. | |
bool | GetRTS () const |
Get the status of the RTS line. | |
bool | GetCTS () |
Get the status of the CTS line. | |
bool | GetDSR () |
Get the status of the DSR line. | |
int | GetFileDescriptor () const |
Gets the serial port file descriptor. | |
int | GetNumberOfBytesAvailable () |
Gets the number of bytes available in the read buffer. | |
void | Read (DataBuffer &dataBuffer, size_t numberOfBytes=0, size_t msTimeout=0) |
Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataBuffer on return from this method. | |
void | Read (std::string &dataString, size_t numberOfBytes=0, size_t msTimeout=0) |
Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataString on return from this method. | |
void | ReadByte (char &charBuffer, size_t msTimeout=0) |
Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available. | |
void | ReadByte (unsigned char &charBuffer, size_t msTimeout=0) |
Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available. | |
void | ReadLine (std::string &dataString, char lineTerminator='\n', size_t msTimeout=0) |
Reads a line of characters from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is 0, then this method will block until a line terminator is received. In all cases, any data received remains available in the string on return from this method. | |
void | Write (const DataBuffer &dataBuffer) |
Writes a DataBuffer to the serial port. | |
void | Write (const std::string &dataString) |
Writes a std::string to the serial port. | |
void | WriteByte (char charbuffer) |
Writes a single byte to the serial port. | |
void | WriteByte (unsigned char charbuffer) |
Writes a single byte to the serial port. | |
void | SetSerialPortBlockingStatus (bool blockingStatus) |
Sets the current state of the serial port blocking status. | |
bool | GetSerialPortBlockingStatus () const |
Gets the current state of the serial port blocking status. | |
void | SetModemControlLine (int modemLine, bool lineState) |
Set the specified modem control line to the specified value. | |
bool | GetModemControlLine (int modemLine) |
Get the current state of the specified modem control line. | |
SerialPort allows an object oriented approach to serial port communication. A serial port object can be created to allow opening the port with specified modes and settings. The SerialPort class also provides Get/Set methods to access the most commonly utilized parameters associated with serial port communication.
Definition at line 55 of file SerialPort.h.
|
explicit |
Default Constructor.
Definition at line 487 of file SerialPort.cpp.
|
explicit |
Constructor that allows a SerialPort instance to be created and opened, initializing the corresponding serial port with the specified parameters.
fileName | The file name of the serial port. |
baudRate | The communications baud rate. |
characterSize | The size of the character buffer for storing read/write streams. |
parityType | The parity type for the serial port. |
stopBits | The number of stop bits for the serial port. |
flowControlType | The flow control type for the serial port. |
exclusive | Set exclusive access for the serial port. |
Definition at line 493 of file SerialPort.cpp.
|
virtualdefaultnoexcept |
Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if open.
|
delete |
Copy construction is disallowed.
LibSerial::SerialPort::SerialPort | ( | SerialPort && | otherSerialPort | ) |
Move construction is allowed.
Definition at line 511 of file SerialPort.cpp.
void LibSerial::SerialPort::Close | ( | ) |
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port.
Definition at line 536 of file SerialPort.cpp.
void LibSerial::SerialPort::DrainWriteBuffer | ( | ) |
Waits until the write buffer is drained and then returns.
Definition at line 542 of file SerialPort.cpp.
void LibSerial::SerialPort::FlushInputBuffer | ( | ) |
Flushes the serial port input buffer.
Definition at line 548 of file SerialPort.cpp.
void LibSerial::SerialPort::FlushIOBuffers | ( | ) |
Flushes the serial port input and output buffers.
Definition at line 560 of file SerialPort.cpp.
void LibSerial::SerialPort::FlushOutputBuffer | ( | ) |
Flushes the serial port output buffer.
Definition at line 554 of file SerialPort.cpp.
BaudRate LibSerial::SerialPort::GetBaudRate | ( | ) | const |
Gets the current baud rate for the serial port.
Definition at line 590 of file SerialPort.cpp.
CharacterSize LibSerial::SerialPort::GetCharacterSize | ( | ) | const |
Gets the character size being used for serial communication.
Definition at line 602 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetCTS | ( | ) |
Get the status of the CTS line.
Definition at line 692 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetDSR | ( | ) |
Get the status of the DSR line.
Definition at line 698 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetDTR | ( | ) | const |
Gets the status of the DTR line.
Definition at line 674 of file SerialPort.cpp.
int LibSerial::SerialPort::GetFileDescriptor | ( | ) | const |
Gets the serial port file descriptor.
Definition at line 704 of file SerialPort.cpp.
FlowControl LibSerial::SerialPort::GetFlowControl | ( | ) | const |
Gets the current flow control setting.
Definition at line 614 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetModemControlLine | ( | int | modemLine | ) |
Get the current state of the specified modem control line.
modemLine | One of the following four values: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, or TIOCM_DSR. |
Definition at line 813 of file SerialPort.cpp.
int LibSerial::SerialPort::GetNumberOfBytesAvailable | ( | ) |
Gets the number of bytes available in the read buffer.
Definition at line 710 of file SerialPort.cpp.
Parity LibSerial::SerialPort::GetParity | ( | ) | const |
Gets the parity type for the serial port.
Definition at line 626 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetRTS | ( | ) | const |
Get the status of the RTS line.
Definition at line 686 of file SerialPort.cpp.
bool LibSerial::SerialPort::GetSerialPortBlockingStatus | ( | ) | const |
Gets the current state of the serial port blocking status.
Definition at line 800 of file SerialPort.cpp.
StopBits LibSerial::SerialPort::GetStopBits | ( | ) | const |
Gets the number of stop bits currently being used by the serial.
Definition at line 638 of file SerialPort.cpp.
short LibSerial::SerialPort::GetVMin | ( | ) | const |
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.
Definition at line 650 of file SerialPort.cpp.
short LibSerial::SerialPort::GetVTime | ( | ) | const |
Gets the current timeout value for non-canonical reads in deciseconds.
Definition at line 662 of file SerialPort.cpp.
bool LibSerial::SerialPort::IsDataAvailable | ( | ) |
Checks if data is available at the input of the serial port.
Definition at line 566 of file SerialPort.cpp.
bool LibSerial::SerialPort::IsOpen | ( | ) | const |
Determines if the serial port is open for I/O.
Definition at line 572 of file SerialPort.cpp.
void LibSerial::SerialPort::Open | ( | const std::string & | fileName, |
const std::ios_base::openmode & | openMode = std::ios_base::in | std::ios_base::out , |
||
bool | exclusive = true |
||
) |
Opens the serial port associated with the specified file name and the specified mode.
fileName | The file name of the serial port. |
openMode | The communication mode status when the serial communication port is opened. |
exclusive | Set exclusive access for the serial port. |
Definition at line 526 of file SerialPort.cpp.
|
delete |
Copy assignment is disallowed.
SerialPort & LibSerial::SerialPort::operator= | ( | SerialPort && | otherSerialPort | ) |
Move assignment is allowed.
Definition at line 517 of file SerialPort.cpp.
void LibSerial::SerialPort::Read | ( | DataBuffer & | dataBuffer, |
size_t | numberOfBytes = 0 , |
||
size_t | msTimeout = 0 |
||
) |
Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataBuffer on return from this method.
dataBuffer | The data buffer to place data into. |
numberOfBytes | The number of bytes to read before returning. |
msTimeout | The timeout period in milliseconds. |
Definition at line 724 of file SerialPort.cpp.
void LibSerial::SerialPort::Read | ( | std::string & | dataString, |
size_t | numberOfBytes = 0 , |
||
size_t | msTimeout = 0 |
||
) |
Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataString on return from this method.
dataString | The string to place data into. |
numberOfBytes | The number of bytes to read before returning. |
msTimeout | The timeout period in milliseconds. |
Definition at line 734 of file SerialPort.cpp.
void LibSerial::SerialPort::ReadByte | ( | char & | charBuffer, |
size_t | msTimeout = 0 |
||
) |
Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.
charBuffer | The character read from the serial port. |
msTimeout | The timeout period in milliseconds. |
Definition at line 744 of file SerialPort.cpp.
void LibSerial::SerialPort::ReadByte | ( | unsigned char & | charBuffer, |
size_t | msTimeout = 0 |
||
) |
Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.
charBuffer | The character read from the serial port. |
msTimeout | The timeout period in milliseconds. |
Definition at line 752 of file SerialPort.cpp.
void LibSerial::SerialPort::ReadLine | ( | std::string & | dataString, |
char | lineTerminator = '\n' , |
||
size_t | msTimeout = 0 |
||
) |
Reads a line of characters from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is 0, then this method will block until a line terminator is received. In all cases, any data received remains available in the string on return from this method.
dataString | The data string read from the serial port. |
lineTerminator | The line termination character to specify the end of a line. |
msTimeout | The timeout value to return if a line termination character is not read. |
Definition at line 760 of file SerialPort.cpp.
void LibSerial::SerialPort::SetBaudRate | ( | const BaudRate & | baudRate | ) |
Sets the baud rate for the serial port to the specified value.
baudRate | The baud rate to be set for the serial port. |
Definition at line 584 of file SerialPort.cpp.
void LibSerial::SerialPort::SetCharacterSize | ( | const CharacterSize & | characterSize | ) |
Sets the character size for the serial port.
characterSize | The character size to be set. |
Definition at line 596 of file SerialPort.cpp.
void LibSerial::SerialPort::SetDefaultSerialPortParameters | ( | ) |
Sets all serial port paramters to their default values.
Definition at line 578 of file SerialPort.cpp.
void LibSerial::SerialPort::SetDTR | ( | const bool | dtrState = true | ) |
Sets the DTR line to the specified value.
dtrState | The line voltage state to be set, (true = high, false = low). |
Definition at line 668 of file SerialPort.cpp.
void LibSerial::SerialPort::SetFlowControl | ( | const FlowControl & | flowControlType | ) |
Sets flow control for the serial port.
flowControlType | The flow control type to be set. |
Definition at line 608 of file SerialPort.cpp.
void LibSerial::SerialPort::SetModemControlLine | ( | int | modemLine, |
bool | lineState | ||
) |
Set the specified modem control line to the specified value.
modemLine | One of the following four values: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, or TIOCM_DSR. |
lineState | State of the modem line after successful call to this method. |
Definition at line 806 of file SerialPort.cpp.
void LibSerial::SerialPort::SetParity | ( | const Parity & | parityType | ) |
Sets the parity type for the serial port.
parityType | The parity type to be set. |
Definition at line 620 of file SerialPort.cpp.
void LibSerial::SerialPort::SetRTS | ( | const bool | rtsState = true | ) |
Set the RTS line to the specified value.
rtsState | The line voltage state to be set, (true = high, false = low). |
Definition at line 680 of file SerialPort.cpp.
void LibSerial::SerialPort::SetSerialPortBlockingStatus | ( | bool | blockingStatus | ) |
Sets the current state of the serial port blocking status.
blockingStatus | The serial port blocking status to be set, true if to be set blocking, false if to be set non-blocking. |
Definition at line 794 of file SerialPort.cpp.
void LibSerial::SerialPort::SetStopBits | ( | const StopBits & | stopBits | ) |
Sets the number of stop bits to be used with the serial port.
stopBits | The number of stop bits to set. |
Definition at line 632 of file SerialPort.cpp.
void LibSerial::SerialPort::SetVMin | ( | const short | vmin | ) |
Sets the minimum number of characters for non-canonical reads.
vmin | the number of minimum characters to be set. |
Definition at line 644 of file SerialPort.cpp.
void LibSerial::SerialPort::SetVTime | ( | const short | vtime | ) |
Sets character buffer timeout for non-canonical reads in deciseconds.
vtime | The timeout value in deciseconds to be set. |
Definition at line 656 of file SerialPort.cpp.
void LibSerial::SerialPort::Write | ( | const DataBuffer & | dataBuffer | ) |
Writes a DataBuffer to the serial port.
dataBuffer | The DataBuffer to write to the serial port. |
Definition at line 770 of file SerialPort.cpp.
void LibSerial::SerialPort::Write | ( | const std::string & | dataString | ) |
Writes a std::string to the serial port.
dataString | The data string to write to the serial port. |
Definition at line 776 of file SerialPort.cpp.
void LibSerial::SerialPort::WriteByte | ( | char | charbuffer | ) |
Writes a single byte to the serial port.
charbuffer | The byte to write to the serial port. |
Definition at line 782 of file SerialPort.cpp.
void LibSerial::SerialPort::WriteByte | ( | unsigned char | charbuffer | ) |
Writes a single byte to the serial port.
charbuffer | The byte to write to the serial port. |
Definition at line 788 of file SerialPort.cpp.