LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
|
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>
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. | |
SerialStream & | operator= (const SerialStream &otherSerialStream)=delete |
Prevents copying of objects of this class by declaring the assignment operator private. This method is never defined. | |
SerialStream & | operator= (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. | |
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.
Definition at line 73 of file SerialStream.h.
|
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.
|
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.
fileName | The file name of the serial stream. |
baudRate | The communications baud rate. |
characterSize | The size of the character buffer for storing read/write streams. |
parityType | The parity type for the serial stream. |
stopBits | The number of stop bits for the serial stream. |
flowControlType | The flow control type for the serial stream. |
Definition at line 44 of file SerialStream.cpp.
|
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.
|
delete |
Prevents copying of objects of this class by declaring the copy constructor private. This method is never defined.
|
delete |
Move construction is disallowed.
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.
Definition at line 105 of file SerialStream.cpp.
void LibSerial::SerialStream::DrainWriteBuffer | ( | ) |
Waits until the write buffer is drained and then returns.
Definition at line 113 of file SerialStream.cpp.
void LibSerial::SerialStream::FlushInputBuffer | ( | ) |
Flushes the serial port input buffer.
Definition at line 143 of file SerialStream.cpp.
void LibSerial::SerialStream::FlushIOBuffers | ( | ) |
Flushes the serial port input and output buffers.
Definition at line 203 of file SerialStream.cpp.
void LibSerial::SerialStream::FlushOutputBuffer | ( | ) |
Flushes the serial port output buffer.
Definition at line 173 of file SerialStream.cpp.
std::vector< std::string > LibSerial::SerialStream::GetAvailableSerialPorts | ( | ) |
Gets a list of available serial ports.
BaudRate LibSerial::SerialStream::GetBaudRate | ( | ) |
Gets the current baud rate for the serial port.
Definition at line 309 of file SerialStream.cpp.
CharacterSize LibSerial::SerialStream::GetCharacterSize | ( | ) |
Gets the character size being used for serial communication.
Definition at line 368 of file SerialStream.cpp.
bool LibSerial::SerialStream::GetCTS | ( | ) |
Get the status of the CTS line.
Definition at line 801 of file SerialStream.cpp.
bool LibSerial::SerialStream::GetDSR | ( | ) |
Get the status of the DSR line.
Definition at line 828 of file SerialStream.cpp.
bool LibSerial::SerialStream::GetDTR | ( | ) |
Gets the status of the DTR line.
Definition at line 718 of file SerialStream.cpp.
int LibSerial::SerialStream::GetFileDescriptor | ( | ) |
Gets the serial port file descriptor.
Definition at line 855 of file SerialStream.cpp.
FlowControl LibSerial::SerialStream::GetFlowControl | ( | ) |
Gets the current flow control setting.
Definition at line 428 of file SerialStream.cpp.
int LibSerial::SerialStream::GetNumberOfBytesAvailable | ( | ) |
Gets the number of bytes available in the read buffer.
Definition at line 882 of file SerialStream.cpp.
Parity LibSerial::SerialStream::GetParity | ( | ) |
Gets the parity type for the serial port.
Definition at line 488 of file SerialStream.cpp.
bool LibSerial::SerialStream::GetRTS | ( | ) |
Get the status of the RTS line.
Definition at line 774 of file SerialStream.cpp.
StopBits LibSerial::SerialStream::GetStopBits | ( | ) |
Gets the number of stop bits currently being used by the serial.
Definition at line 548 of file SerialStream.cpp.
short LibSerial::SerialStream::GetVMin | ( | ) |
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.
Definition at line 606 of file SerialStream.cpp.
short LibSerial::SerialStream::GetVTime | ( | ) |
Gets the current timeout value for non-canonical reads in deciseconds.
Definition at line 662 of file SerialStream.cpp.
bool LibSerial::SerialStream::IsDataAvailable | ( | ) |
Checks if data is available at the input of the serial port.
Definition at line 233 of file SerialStream.cpp.
bool LibSerial::SerialStream::IsOpen | ( | ) |
Determines if the serial port is open for I/O.
Definition at line 261 of file SerialStream.cpp.
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.
fileName | The file name of the serial port. |
openMode | The communication mode status when the serial communication port is opened. |
Definition at line 83 of file SerialStream.cpp.
|
delete |
Move assignment is not allowed.
|
delete |
Prevents copying of objects of this class by declaring the assignment operator private. This method is never defined.
void LibSerial::SerialStream::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 279 of file SerialStream.cpp.
void LibSerial::SerialStream::SetCharacterSize | ( | const CharacterSize & | characterSize | ) |
Sets the character size for the serial port.
characterSize | The character size to be set. |
Definition at line 338 of file SerialStream.cpp.
void LibSerial::SerialStream::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 689 of file SerialStream.cpp.
void LibSerial::SerialStream::SetFlowControl | ( | const FlowControl & | flowControlType | ) |
Sets flow control for the serial port.
flowControlType | The flow control type to be set. |
Definition at line 397 of file SerialStream.cpp.
void LibSerial::SerialStream::SetParity | ( | const Parity & | parityType | ) |
Sets the parity type for the serial port.
parityType | The parity type to be set. |
Definition at line 457 of file SerialStream.cpp.
void LibSerial::SerialStream::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 745 of file SerialStream.cpp.
void LibSerial::SerialStream::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 517 of file SerialStream.cpp.
void LibSerial::SerialStream::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 577 of file SerialStream.cpp.
void LibSerial::SerialStream::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 633 of file SerialStream.cpp.