LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
LibSerial::SerialStream Class Reference

SerialStream is a stream class for accessing serial ports on POSIX operating systems. A lot of the functionality of this class has been obtained by looking at the code of libserial package by Linas Vepstas, (linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g) and the excellent document on serial programming by Michael R. Sweet. This document can be found at <ahref="http://www.easysw.com/~mike/serial/serial.html"> http://www.easysw.com/~mike/serial/serial.html. The libserial package can be found at <ahref="http://www.linas.org/serial/"> http://www.linas.org/serial/. This class allows one to set various parameters of a serial port and then access it like a simple fstream. (In fact, that is exactly what it does!) It sets the parameters of the serial port by maintaining a file descriptor for the port and uses the basic_fstream functions for the IO. We have not implemented any file locking yet but it will be added soon. More...

#include <SerialStream.h>

Inheritance diagram for LibSerial::SerialStream:
Collaboration diagram for LibSerial::SerialStream:

Public Member Functions

 SerialStream ()
 Default Contructor. Creates a new SerialStream object but does not open it. The Open() method will need to be called explicitly on the object to communicate with the serial port.
 
 SerialStream (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)
 Constructor that allows a SerialStream instance to be created and opened, initializing the corresponding serial port with the specified parameters. Suggested by Witek Adamus (wit3k): https://sourceforge.net/tracker/index.php?func=detail&aid=2137885&group_id=9432&atid=359432.
 
virtual ~SerialStream ()
 Default Destructor for a SerialStream object Closes the stream associated with mFileDescriptor, and also closes the serial port if open. Remaining actions are accomplished by the fstream destructor.
 
 SerialStream (const SerialStream &otherSerialStream)=delete
 Prevents copying of objects of this class by declaring the copy constructor private. This method is never defined.
 
 SerialStream (const SerialStream &&otherSerialStream)=delete
 Move construction is disallowed.
 
SerialStreamoperator= (const SerialStream &otherSerialStream)=delete
 Prevents copying of objects of this class by declaring the assignment operator private. This method is never defined.
 
SerialStreamoperator= (const SerialStream &&otherSerialStream)=delete
 Move assignment is not allowed.
 
void Open (const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out)
 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 ()
 Determines if the serial port is open for I/O.
 
void SetBaudRate (const BaudRate &baudRate)
 Sets the baud rate for the serial port to the specified value.
 
BaudRate GetBaudRate ()
 Gets the current baud rate for the serial port.
 
void SetCharacterSize (const CharacterSize &characterSize)
 Sets the character size for the serial port.
 
CharacterSize GetCharacterSize ()
 Gets the character size being used for serial communication.
 
void SetFlowControl (const FlowControl &flowControlType)
 Sets flow control for the serial port.
 
FlowControl GetFlowControl ()
 Gets the current flow control setting.
 
void SetParity (const Parity &parityType)
 Sets the parity type for the serial port.
 
Parity GetParity ()
 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 ()
 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 ()
 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 ()
 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 ()
 Gets the status of the DTR line.
 
void SetRTS (const bool rtsState=true)
 Set the RTS line to the specified value.
 
bool GetRTS ()
 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 ()
 Gets the serial port file descriptor.
 
int GetNumberOfBytesAvailable ()
 Gets the number of bytes available in the read buffer.
 
std::vector< std::string > GetAvailableSerialPorts ()
 Gets a list of available serial ports.
 

Detailed Description

SerialStream is a stream class for accessing serial ports on POSIX operating systems. A lot of the functionality of this class has been obtained by looking at the code of libserial package by Linas Vepstas, (linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g) and the excellent document on serial programming by Michael R. Sweet. This document can be found at <ahref="http://www.easysw.com/~mike/serial/serial.html"> http://www.easysw.com/~mike/serial/serial.html. The libserial package can be found at <ahref="http://www.linas.org/serial/"> http://www.linas.org/serial/. This class allows one to set various parameters of a serial port and then access it like a simple fstream. (In fact, that is exactly what it does!) It sets the parameters of the serial port by maintaining a file descriptor for the port and uses the basic_fstream functions for the IO. We have not implemented any file locking yet but it will be added soon.

Make sure you read the documentation of the standard fstream template before using this class because most of the functionality is inherited from fstream. Also, a lot of information about the various system calls used in the implementation can also be found in the Single Unix Specification (Version 2). A copy of this document can be obtained from http://www.UNIX-systems.org/. We will refer to this document as SUS-2.

Examples
main_page_example.cpp, serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 73 of file SerialStream.h.

Constructor & Destructor Documentation

◆ SerialStream() [1/4]

LibSerial::SerialStream::SerialStream ( )
explicit

Default Contructor. Creates a new SerialStream object but does not open it. The Open() method will need to be called explicitly on the object to communicate with the serial port.

Definition at line 39 of file SerialStream.cpp.

◆ SerialStream() [2/4]

LibSerial::SerialStream::SerialStream ( 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 
)
explicit

Constructor that allows a SerialStream instance to be created and opened, initializing the corresponding serial port with the specified parameters. Suggested by Witek Adamus (wit3k): https://sourceforge.net/tracker/index.php?func=detail&aid=2137885&group_id=9432&atid=359432.

Parameters
fileNameThe file name of the serial stream.
baudRateThe communications baud rate.
characterSizeThe size of the character buffer for storing read/write streams.
parityTypeThe parity type for the serial stream.
stopBitsThe number of stop bits for the serial stream.
flowControlTypeThe flow control type for the serial stream.

Definition at line 44 of file SerialStream.cpp.

◆ ~SerialStream()

LibSerial::SerialStream::~SerialStream ( )
virtual

Default Destructor for a SerialStream object Closes the stream associated with mFileDescriptor, and also closes the serial port if open. Remaining actions are accomplished by the fstream destructor.

Definition at line 61 of file SerialStream.cpp.

◆ SerialStream() [3/4]

LibSerial::SerialStream::SerialStream ( const SerialStream otherSerialStream)
delete

Prevents copying of objects of this class by declaring the copy constructor private. This method is never defined.

◆ SerialStream() [4/4]

LibSerial::SerialStream::SerialStream ( const SerialStream &&  otherSerialStream)
delete

Move construction is disallowed.

Member Function Documentation

◆ Close()

void LibSerial::SerialStream::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.

Examples
serial_stream_read_write.cpp.

Definition at line 105 of file SerialStream.cpp.

◆ DrainWriteBuffer()

void LibSerial::SerialStream::DrainWriteBuffer ( )

Waits until the write buffer is drained and then returns.

Examples
serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 113 of file SerialStream.cpp.

◆ FlushInputBuffer()

void LibSerial::SerialStream::FlushInputBuffer ( )

Flushes the serial port input buffer.

Definition at line 143 of file SerialStream.cpp.

◆ FlushIOBuffers()

void LibSerial::SerialStream::FlushIOBuffers ( )

Flushes the serial port input and output buffers.

Definition at line 203 of file SerialStream.cpp.

◆ FlushOutputBuffer()

void LibSerial::SerialStream::FlushOutputBuffer ( )

Flushes the serial port output buffer.

Definition at line 173 of file SerialStream.cpp.

◆ GetAvailableSerialPorts()

std::vector< std::string > LibSerial::SerialStream::GetAvailableSerialPorts ( )

Gets a list of available serial ports.

Returns
Returns a std::vector of std::strings with the name of each available serial port.
Todo:
Consider making this a static member function.

◆ GetBaudRate()

BaudRate LibSerial::SerialStream::GetBaudRate ( )

Gets the current baud rate for the serial port.

Returns
Returns the baud rate.

Definition at line 309 of file SerialStream.cpp.

◆ GetCharacterSize()

CharacterSize LibSerial::SerialStream::GetCharacterSize ( )

Gets the character size being used for serial communication.

Returns
Returns the current character size.

Definition at line 368 of file SerialStream.cpp.

◆ GetCTS()

bool LibSerial::SerialStream::GetCTS ( )

Get the status of the CTS line.

Returns
Returns true iff the status of the CTS line is high.

Definition at line 801 of file SerialStream.cpp.

◆ GetDSR()

bool LibSerial::SerialStream::GetDSR ( )

Get the status of the DSR line.

Returns
Returns true iff the status of the DSR line is high.

Definition at line 828 of file SerialStream.cpp.

◆ GetDTR()

bool LibSerial::SerialStream::GetDTR ( )

Gets the status of the DTR line.

Returns
Returns true iff the status of the DTR line is high.

Definition at line 718 of file SerialStream.cpp.

◆ GetFileDescriptor()

int LibSerial::SerialStream::GetFileDescriptor ( )

Gets the serial port file descriptor.

Returns
Returns the serial port file descriptor.

Definition at line 855 of file SerialStream.cpp.

◆ GetFlowControl()

FlowControl LibSerial::SerialStream::GetFlowControl ( )

Gets the current flow control setting.

Returns
Returns the flow control type of the serial port.

Definition at line 428 of file SerialStream.cpp.

◆ GetNumberOfBytesAvailable()

int LibSerial::SerialStream::GetNumberOfBytesAvailable ( )

Gets the number of bytes available in the read buffer.

Returns
Returns the number of bytes avilable in the read buffer.

Definition at line 882 of file SerialStream.cpp.

◆ GetParity()

Parity LibSerial::SerialStream::GetParity ( )

Gets the parity type for the serial port.

Returns
Returns the parity type.

Definition at line 488 of file SerialStream.cpp.

◆ GetRTS()

bool LibSerial::SerialStream::GetRTS ( )

Get the status of the RTS line.

Returns
Returns true iff the status of the RTS line is high.

Definition at line 774 of file SerialStream.cpp.

◆ GetStopBits()

StopBits LibSerial::SerialStream::GetStopBits ( )

Gets the number of stop bits currently being used by the serial.

Returns
Returns the number of stop bits.

Definition at line 548 of file SerialStream.cpp.

◆ GetVMin()

short LibSerial::SerialStream::GetVMin ( )

Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.

Returns
Returns the minimum number of characters for non-canonical reads.

Definition at line 606 of file SerialStream.cpp.

◆ GetVTime()

short LibSerial::SerialStream::GetVTime ( )

Gets the current timeout value for non-canonical reads in deciseconds.

Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 662 of file SerialStream.cpp.

◆ IsDataAvailable()

bool LibSerial::SerialStream::IsDataAvailable ( )

Checks if data is available at the input of the serial port.

Returns
Returns true iff data is available to read.
Examples
serial_stream_read.cpp.

Definition at line 233 of file SerialStream.cpp.

◆ IsOpen()

bool LibSerial::SerialStream::IsOpen ( )

Determines if the serial port is open for I/O.

Returns
Returns true iff the serial port is open.

Definition at line 261 of file SerialStream.cpp.

◆ Open()

void LibSerial::SerialStream::Open ( const std::string &  fileName,
const std::ios_base::openmode &  openMode = std::ios_base::in | std::ios_base::out 
)

Opens the serial port associated with the specified file name and the specified mode.

Parameters
fileNameThe file name of the serial port.
openModeThe communication mode status when the serial communication port is opened.
Examples
main_page_example.cpp, serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 83 of file SerialStream.cpp.

◆ operator=() [1/2]

SerialStream & LibSerial::SerialStream::operator= ( const SerialStream &&  otherSerialStream)
delete

Move assignment is not allowed.

◆ operator=() [2/2]

SerialStream & LibSerial::SerialStream::operator= ( const SerialStream otherSerialStream)
delete

Prevents copying of objects of this class by declaring the assignment operator private. This method is never defined.

◆ SetBaudRate()

void LibSerial::SerialStream::SetBaudRate ( const BaudRate &  baudRate)

Sets the baud rate for the serial port to the specified value.

Parameters
baudRateThe baud rate to be set for the serial port.
Examples
serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 279 of file SerialStream.cpp.

◆ SetCharacterSize()

void LibSerial::SerialStream::SetCharacterSize ( const CharacterSize &  characterSize)

Sets the character size for the serial port.

Parameters
characterSizeThe character size to be set.
Examples
serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 338 of file SerialStream.cpp.

◆ SetDTR()

void LibSerial::SerialStream::SetDTR ( const bool  dtrState = true)

Sets the DTR line to the specified value.

Parameters
dtrStateThe line voltage state to be set, (true = high, false = low).

Definition at line 689 of file SerialStream.cpp.

◆ SetFlowControl()

void LibSerial::SerialStream::SetFlowControl ( const FlowControl &  flowControlType)

Sets flow control for the serial port.

Parameters
flowControlTypeThe flow control type to be set.
Examples
serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 397 of file SerialStream.cpp.

◆ SetParity()

void LibSerial::SerialStream::SetParity ( const Parity &  parityType)

Sets the parity type for the serial port.

Parameters
parityTypeThe parity type to be set.
Examples
serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 457 of file SerialStream.cpp.

◆ SetRTS()

void LibSerial::SerialStream::SetRTS ( const bool  rtsState = true)

Set the RTS line to the specified value.

Parameters
rtsStateThe line voltage state to be set, (true = high, false = low).

Definition at line 745 of file SerialStream.cpp.

◆ SetStopBits()

void LibSerial::SerialStream::SetStopBits ( const StopBits &  stopBits)

Sets the number of stop bits to be used with the serial port.

Parameters
stopBitsThe number of stop bits to set.
Examples
serial_stream_read.cpp, serial_stream_read_write.cpp, and serial_stream_write.cpp.

Definition at line 517 of file SerialStream.cpp.

◆ SetVMin()

void LibSerial::SerialStream::SetVMin ( const short  vmin)

Sets the minimum number of characters for non-canonical reads.

Note
See VMIN in man termios(3).
Parameters
vminthe number of minimum characters to be set.

Definition at line 577 of file SerialStream.cpp.

◆ SetVTime()

void LibSerial::SerialStream::SetVTime ( const short  vtime)

Sets character buffer timeout for non-canonical reads in deciseconds.

Parameters
vtimeThe timeout value in deciseconds to be set.
Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 633 of file SerialStream.cpp.


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