libserial
0.6.0rc3
|
A stream class for accessing serial ports on POSIX operating systems. More...
#include <SerialStream.h>
Public Member Functions | |
SerialStream (const std::string fileName, std::ios_base::openmode openMode=std::ios::in|std::ios::out) | |
This constructor takes a filename and an openmode to construct a SerialStream object. More... | |
SerialStream (const std::string fileName, const SerialStreamBuf::BaudRateEnum baudRate=SerialStreamBuf::DEFAULT_BAUD, const SerialStreamBuf::CharSizeEnum charSize=SerialStreamBuf::DEFAULT_CHAR_SIZE, const SerialStreamBuf::ParityEnum parityType=SerialStreamBuf::DEFAULT_PARITY, const short numOfStopBits=SerialStreamBuf::DEFAULT_NO_OF_STOP_BITS, const SerialStreamBuf::FlowControlEnum flowControlType=SerialStreamBuf::DEFAULT_FLOW_CONTROL) | |
Constructor that allows one to create a SerialStream instance and also initialize the corresponding serial port with the specified parameters. More... | |
SerialStream () | |
Create a new SerialStream object but do not open it. More... | |
virtual | ~SerialStream () |
The destructor. More... | |
void | Open (const std::string fileName, std::ios_base::openmode openMode=std::ios_base::in|std::ios_base::out) |
Open the serial port associated with the specified filename, and the specified mode, mode. More... | |
void | Close () |
Close the serial port. More... | |
const bool | IsOpen () const |
Returns true if the Stream is in a good open state, false otherwise. More... | |
void | SetBaudRate (SerialStreamBuf::BaudRateEnum baudRate) |
Set the baud rate for serial communications. More... | |
const SerialStreamBuf::BaudRateEnum | BaudRate () |
Get the current baud rate being used for serial communication. More... | |
void | SetCharSize (const SerialStreamBuf::CharSizeEnum charSize) |
Set the character size associated with the serial port. More... | |
const SerialStreamBuf::CharSizeEnum | CharSize () |
Get the character size being used for serial communication. More... | |
void | SetNumOfStopBits (short numOfStopBits) |
Set the number of stop bits used during serial communication. More... | |
const short | NumOfStopBits () |
Get the number of stop bits being used during serial communication. More... | |
void | SetParity (const SerialStreamBuf::ParityEnum parityType) |
Set the parity for serial communication. More... | |
const SerialStreamBuf::ParityEnum | Parity () |
Get the current parity setting for the serial port. More... | |
void | SetFlowControl (const SerialStreamBuf::FlowControlEnum flowControlType) |
Use the specified flow control. More... | |
const SerialStreamBuf::FlowControlEnum | FlowControl () |
Return the current flow control setting. More... | |
const short | SetVMin (short vtime) |
Set character buffer size. More... | |
const short | VMin () |
Get current size of character buffer. More... | |
const short | SetVTime (short vtime) |
Set character buffer timing in 10th of a second. More... | |
const short | VTime () |
Get current timing of character buffer in 10th of a second. More... | |
Private Member Functions | |
SerialStream (const SerialStream &) | |
SerialStream & | operator= (const SerialStream &) |
Private Attributes | |
SerialStreamBuf * | mIOBuffer |
The SerialStreamBuf object that will be used by the stream to communicate with the serial port. More... | |
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) and the excellent document on serial programming by Michael R. Sweet. This document can be found at @lin as.or ghttp://www.easysw.com/~mike/serial/serial.html. The libserial package can be found at 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 52 of file SerialStream.h.
|
explicit |
This constructor takes a filename and an openmode to construct a SerialStream object.
This results in a call to basic_fstream::open(s,mode). This is the only way to contruct an object of this class. We have to enforce this instead of providing a default constructor because we want to get a file descriptor whenever the basic_fstream::open() function is called. However, this function is not made virtual in the STL hence it is probably not very safe to overload it. We may decide to overload it later but the users of this class will have to make sure that this class is not used as an fstream class. The SerialStream will be in the "open" state (same state as after calling the Open() method) after calling this constructor.
If the constructor has problems opening the serial port or getting the file-descriptor for the port, it will set the failbit for the stream. So, one must make sure that the stream is in a good state before using it for any further I/O operations.
fileName | The filename of the serial port. |
openMode | The openmode for the serial port file. |
LibSerial::SerialStream::SerialStream | ( | const std::string | fileName, |
const SerialStreamBuf::BaudRateEnum | baudRate = SerialStreamBuf::DEFAULT_BAUD , |
||
const SerialStreamBuf::CharSizeEnum | charSize = SerialStreamBuf::DEFAULT_CHAR_SIZE , |
||
const SerialStreamBuf::ParityEnum | parityType = SerialStreamBuf::DEFAULT_PARITY , |
||
const short | numOfStopBits = SerialStreamBuf::DEFAULT_NO_OF_STOP_BITS , |
||
const SerialStreamBuf::FlowControlEnum | flowControlType = SerialStreamBuf::DEFAULT_FLOW_CONTROL |
||
) |
Constructor that allows one to create a SerialStream instance and also initialize the corresponding serial port with the specified parameters.
This was suggested by Witek Adamus (wit3k).
See https://sourceforge.net/tracker/index.php?func=detail&aid=2137885&group_id=9432&atid=359432
:TODO: Add documentation for all parameters here.
|
explicit |
Create a new SerialStream object but do not open it.
The Open() method will need to be called explicitly on the object to communicate with the serial port.
|
virtual |
The destructor.
It closes the stream associated with mFileDescriptor. The rest is done by the fstream destructor.
|
private |
const SerialStreamBuf::BaudRateEnum LibSerial::SerialStream::BaudRate | ( | ) |
Get the current baud rate being used for serial communication.
This routine queries the serial port for its current settings and returns the baud rate that is being used by the serial port.
const SerialStreamBuf::CharSizeEnum LibSerial::SerialStream::CharSize | ( | ) |
Get the character size being used for serial communication.
void LibSerial::SerialStream::Close | ( | ) |
Close the serial port.
No communications can occur with the serial port after calling this routine.
const SerialStreamBuf::FlowControlEnum LibSerial::SerialStream::FlowControl | ( | ) |
Return the current flow control setting.
const bool LibSerial::SerialStream::IsOpen | ( | ) | const |
Returns true if the Stream is in a good open state, false otherwise.
const short LibSerial::SerialStream::NumOfStopBits | ( | ) |
Get the number of stop bits being used during serial communication.
void LibSerial::SerialStream::Open | ( | const std::string | fileName, |
std::ios_base::openmode | openMode = std::ios_base::in|std::ios_base::out |
||
) |
Open the serial port associated with the specified filename, and the specified mode, mode.
|
private |
const SerialStreamBuf::ParityEnum LibSerial::SerialStream::Parity | ( | ) |
Get the current parity setting for the serial port.
void LibSerial::SerialStream::SetBaudRate | ( | SerialStreamBuf::BaudRateEnum | baudRate | ) |
Set the baud rate for serial communications.
void LibSerial::SerialStream::SetCharSize | ( | const SerialStreamBuf::CharSizeEnum | charSize | ) |
Set the character size associated with the serial port.
size | The character size will be set to this value. |
void LibSerial::SerialStream::SetFlowControl | ( | const SerialStreamBuf::FlowControlEnum | flowControlType | ) |
Use the specified flow control.
void LibSerial::SerialStream::SetNumOfStopBits | ( | short | numOfStopBits | ) |
Set the number of stop bits used during serial communication.
The only valid values are 1 and 2.
stop_bits | The number of stop bits. (1 or 2). |
void LibSerial::SerialStream::SetParity | ( | const SerialStreamBuf::ParityEnum | parityType | ) |
Set the parity for serial communication.
parity | The parity value. |
const short LibSerial::SerialStream::SetVMin | ( | short | vtime | ) |
Set character buffer size.
const short LibSerial::SerialStream::SetVTime | ( | short | vtime | ) |
Set character buffer timing in 10th of a second.
const short LibSerial::SerialStream::VMin | ( | ) |
Get current size of character buffer.
Look here for more documentation about VTIME and VMIN.
const short LibSerial::SerialStream::VTime | ( | ) |
Get current timing of character buffer in 10th of a second.
Look here for more documentation about VTIME and VMIN.
|
private |
The SerialStreamBuf object that will be used by the stream to communicate with the serial port.
Definition at line 266 of file SerialStream.h.