C12Adapter Opensource C++ Interface
|
Serial port device. More...
Public Types | |
typedef HANDLE | PortHandleType |
Public Member Functions | |
MSerialPort () | |
Constructor that creates an uninitialized serial port. More... | |
~MSerialPort () | |
Destructor. More... | |
void | Open (const MStdString &portName) |
Open a serial port. More... | |
void | Close () |
Close the port. More... | |
unsigned | Read (char *buffer, unsigned size) |
Receive a number of bytes available in the serial port. More... | |
unsigned | Write (const char *buffer, unsigned size) |
Write the buffer into the port. More... | |
void | ClearInputBuffer () const |
Discard the contents of the input buffer of the port. More... | |
void | FlushOutputBuffer (unsigned numberOfCharsInBuffer=UINT_MAX) |
Ensure that the characters from the output buffer are sent. More... | |
unsigned | GetBytesReadyToRead () const |
Returns the number of bytes in the serial port input buffer, which are not read yet. | |
bool | IsOpen () const |
Whether the port is open. | |
bool | GetDCD () const |
Get the current state of the DCD signal of the port. More... | |
void | SetParameters (unsigned baud, int dataBits, char parity, int stopBits) |
Convenience method for setting related port parameters in a single call. More... | |
void | SetTimeouts (unsigned intercharacterTimeout, unsigned readTimeout, unsigned writeTimeout) |
Convenience method for setting port timeouts in a single call. More... | |
void | ConfigurePortParameters () const |
Force configuration of port parameters to properties of this class. More... | |
void | ConfigurePortTimeouts () const |
Force configuration of port timeouts to properties of this class. More... | |
void | UpdatePortParametersOrTimeoutsIfChanged () const |
If properties for parameters or timeouts are changed, change port. More... | |
const MStdString & | GetPortName () const |
void | SetPortName (const MStdString &port) |
unsigned | GetBaud () const |
void | SetBaud (unsigned) |
char | GetParity () const |
void | SetParity (char) |
int | GetStopBits () const |
void | SetStopBits (int) |
int | GetDataBits () const |
void | SetDataBits (int) |
bool | GetCtsFlow () const |
void | SetCtsFlow (bool) |
bool | GetDsrFlow () const |
void | SetDsrFlow (bool) |
bool | GetDsrSensitivity () const |
void | SetDsrSensitivity (bool) |
char | GetDtrControl () const |
void | SetDtrControl (char) |
char | GetRtsControl () const |
void | SetRtsControl (char rtsControl) |
unsigned | GetIntercharacterTimeout () const |
void | SetIntercharacterTimeout (unsigned timeout) |
unsigned | GetReadTimeout () const |
void | SetReadTimeout (unsigned timeout) |
unsigned | GetWriteTimeout () const |
void | SetWriteTimeout (unsigned timeout) |
PortHandleType | GetHandle () const |
Static Public Member Functions | |
static MStdStringVector | GetAvailablePortNames (bool addExtraInfo=false) |
Return a collection of serial port names available at this computer. More... | |
static MStdString | GetPortType (const MStdString &portName) |
Get type based on the port name. More... | |
static M_NORETURN_FUNC void | ThrowInvalidBaudRate (unsigned baud) |
Throw an error that says the baud is invalid, giving the baud value as parameter. More... | |
static void | CheckIsBaudValid (unsigned baud) |
Check if the given baud rate is valid. More... | |
static void | CheckIsParityValid (char parity) |
Check if the given parity is valid. More... | |
static const unsigned * | GetAcceptableBaudsArray () |
Get a zero-terminated list of acceptable baud rates. | |
static const char * | GetAcceptableParitiesArray () |
Get a zero-terminated list of acceptable parity values. | |
static void | CheckIsStopBitsValid (int stopBits) |
Check if the given number of stop bits is a valid value. More... | |
static void | CheckIsDataBitsValid (int dataBits) |
Check if the given number of data bits is a valid value. More... | |
static void | CheckIsDtrControlValid (char dtrControl) |
Check if the given character represents a valid value for DTR control. More... | |
static void | CheckIsRtsControlValid (char rtsControl) |
Check if the given character represents a valid value for RTS control. More... | |
Serial port device.
The services below can throw MException or MESystemError in the event of an erroneous serial port operation.
typedef HANDLE MSerialPort::PortHandleType |
Operating system dependent serial port handle type
MSerialPort::MSerialPort | ( | ) |
Constructor that creates an uninitialized serial port.
The serial is not open until Open is called.
MSerialPort::~MSerialPort | ( | ) |
Destructor.
No exceptions are thrown by this call.
|
static |
Check if the given baud rate is valid.
baud | Baud value to be checked. If the baud rate is not valid, an exception is thrown. |
|
static |
Check if the given number of data bits is a valid value.
dataBits | Data bits to check. If the number of data bits is not 5, 6, 7 or 8, an exception is thrown. |
|
static |
Check if the given character represents a valid value for DTR control.
dtrControl | DTR control parameter to check. It shall either be 'E', 'D' or 'H', or an exception is thrown. |
|
static |
Check if the given parity is valid.
parity | Parity value to be checked. If the parity is not valid, an exception is thrown. |
|
static |
Check if the given character represents a valid value for RTS control.
rtsControl | RTS control parameter to check. It shall either be 'E', 'D', 'H', or 'T', or an exception is thrown. |
|
static |
Check if the given number of stop bits is a valid value.
stopBits | Stop bits to test. If the number of stop bits is not 1 or 2, an exception is thrown. |
void MSerialPort::ClearInputBuffer | ( | ) | const |
Discard the contents of the input buffer of the port.
All characters in the receive buffer that are waiting to be read are lost.
void MSerialPort::Close | ( | ) |
Close the port.
No exceptions are thrown by this function. If the port was not open, this method does nothing.
void MSerialPort::ConfigurePortParameters | ( | ) | const |
Force configuration of port parameters to properties of this class.
No action is performed in case communication is not yet established.
void MSerialPort::ConfigurePortTimeouts | ( | ) | const |
Force configuration of port timeouts to properties of this class.
No action is performed in case communication is not yet established.
void MSerialPort::FlushOutputBuffer | ( | unsigned | numberOfCharsInBuffer = UINT_MAX | ) |
Ensure that the characters from the output buffer are sent.
The parameter, if specified, should match the number of characters written into the serial port right before FlushOutputBuffer is called. If the parameter is missing, the biggest possible number of characters will be ensured to go away.
|
static |
Return a collection of serial port names available at this computer.
Names returned can be given to Open. The returned names are operating system dependent.
addExtraInfo | whether to add any extra information about the port, useful for users to select the port. |
|
inline |
Baud rate of the port.
If the port is connected, the baud rate switch will happen immediately within the function. If the port is not connected, the baud rate will be used to configure the port when Open is called.
|
inline |
CTS flow used for port handling.
If the port is connected, CTS flow handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
|
inline |
Number of data bits.
If the port is connected, the number of data bits changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
bool MSerialPort::GetDCD | ( | ) | const |
Get the current state of the DCD signal of the port.
|
inline |
DSR flow used for port handling.
If the port is connected, CTS flow handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
|
inline |
Sensitivity of the communication port to DSR signal.
If the port is connected, DSR sensitivity handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
|
inline |
Data Terminal Ready (DTR) control mode of the communication port.
'E' means Enable, 'D' means Disable, and 'H' means Handshake.
If the port is connected, DTR control changes immediately at the assignment. If the port is not connected, the property is used to configure port when Open is called.
|
inline |
Access operating system handle of the port.
On Windows this is file HANDLE
type, while on Posix operating systems this is an integer file number.
|
inline |
Read intercharacter timeout in milliseconds.
Maximum amount of milliseconds between any two characters before timeout exception takes place.
|
inline |
Port parity.
If the port is connected, the parity change happens immediately at the assignment. If the port is not connected, the parity setting is used to configure the port when Open is called.
|
inline |
Port name, whatever was used during Open.
Setting port name explicitly outside Open can be used to keep the port name associated with the object that will open it later.
|
static |
Get type based on the port name.
Provides information about underlying port technology. When such information is not available, for example, the given port name is not present, an empty string is returned. Otherwise the string returned can be one of the following types:
All known port types will always appear in lowercase. Unknown port types will preserve the letter case defined by the operating system. The port will not be opened by the call.
This is a static method as it allows getting information without opening the port. A string is returned rather than an enumeration as it allows returning useful information even when the port type is not recognized and classified.
portName | Name of the port such as "COM11" on Windows or "/dev/ttyS0" on Linux |
|
inline |
Read timeout in milliseconds.
Maximum amount of milliseconds for completion of the whole read operation.
|
inline |
Request To Send (RTS) control mode of the communication port.
'E' means Enable, 'D' means Disable, 'H' means Handshake, and 'T' means Toggle.
If the port is connected, RTS control changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
|
inline |
Number of stop bits.
If the port is connected, the number of stop bits changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
|
inline |
Write timeout in milliseconds.
Timeout is the time for which the write operation should complete.
void MSerialPort::Open | ( | const MStdString & | portName | ) |
Open a serial port.
The valid name rules depend on the operating system. All available ports can be gotten using GetAvailablePortNames(bool addExtraInfo = false) call. When addExtraInfo is true, the port names returned have more human readable information that can help select the port name. The port name is operating system dependent:
portName | Port name to open. |
unsigned MSerialPort::Read | ( | char * | buffer, |
unsigned | size | ||
) |
Receive a number of bytes available in the serial port.
return the actual number of bytes read.
void MSerialPort::SetBaud | ( | unsigned | ) |
Baud rate of the port.
If the port is connected, the baud rate switch will happen immediately within the function. If the port is not connected, the baud rate will be used to configure the port when Open is called.
void MSerialPort::SetCtsFlow | ( | bool | ) |
CTS flow used for port handling.
If the port is connected, CTS flow handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetDataBits | ( | int | ) |
Number of data bits.
If the port is connected, the number of data bits changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetDsrFlow | ( | bool | ) |
DSR flow used for port handling.
If the port is connected, CTS flow handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetDsrSensitivity | ( | bool | ) |
Sensitivity of the communication port to DSR signal.
If the port is connected, DSR sensitivity handling changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetDtrControl | ( | char | ) |
Data Terminal Ready (DTR) control mode of the communication port.
'E' means Enable, 'D' means Disable, and 'H' means Handshake.
If the port is connected, DTR control changes immediately at the assignment. If the port is not connected, the property is used to configure port when Open is called.
void MSerialPort::SetIntercharacterTimeout | ( | unsigned | timeout | ) |
Read intercharacter timeout in milliseconds.
Maximum amount of milliseconds between any two characters before timeout exception takes place.
void MSerialPort::SetParameters | ( | unsigned | baud, |
int | dataBits, | ||
char | parity, | ||
int | stopBits | ||
) |
Convenience method for setting related port parameters in a single call.
baud | Valid baud rate |
dataBits | Valid data bits value |
parity | Valid parity value |
stopBits | Valid stop bits value |
void MSerialPort::SetParity | ( | char | ) |
Port parity.
If the port is connected, the parity change happens immediately at the assignment. If the port is not connected, the parity setting is used to configure the port when Open is called.
|
inline |
Port name, whatever was used during Open.
Setting port name explicitly outside Open can be used to keep the port name associated with the object that will open it later.
void MSerialPort::SetReadTimeout | ( | unsigned | timeout | ) |
Read timeout in milliseconds.
Maximum amount of milliseconds for completion of the whole read operation.
void MSerialPort::SetRtsControl | ( | char | rtsControl | ) |
Request To Send (RTS) control mode of the communication port.
'E' means Enable, 'D' means Disable, 'H' means Handshake, and 'T' means Toggle.
If the port is connected, RTS control changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetStopBits | ( | int | ) |
Number of stop bits.
If the port is connected, the number of stop bits changes immediately at the assignment. If the port is not connected, the property is used to configure the port when Open is called.
void MSerialPort::SetTimeouts | ( | unsigned | intercharacterTimeout, |
unsigned | readTimeout, | ||
unsigned | writeTimeout | ||
) |
Convenience method for setting port timeouts in a single call.
intercharacterTimeout | Intercharacter timeout in milliseconds |
readTimeout | Read timeout in milliseconds |
writeTimeout | Write timeout in milliseconds |
void MSerialPort::SetWriteTimeout | ( | unsigned | timeout | ) |
Write timeout in milliseconds.
Timeout is the time for which the write operation should complete.
|
static |
Throw an error that says the baud is invalid, giving the baud value as parameter.
baud | Reported bad baud |
void MSerialPort::UpdatePortParametersOrTimeoutsIfChanged | ( | ) | const |
If properties for parameters or timeouts are changed, change port.
No action is performed in case communication is not yet established.
unsigned MSerialPort::Write | ( | const char * | buffer, |
unsigned | size | ||
) |
Write the buffer into the port.