![]() |
PIDUINO
|
Provides functions to access serial ports. More...
#include <serialport.h>
Classes | |
class | Info |
Provides information about existing serial ports. More... | |
class | Settings |
Provides settings information about serial ports. More... | |
Public Types | |
enum | BaudRate { Baud1200 = 1200 , Baud2400 = 2400 , Baud4800 = 4800 , Baud9600 = 9600 , Baud19200 = 19200 , Baud38400 = 38400 , Baud57600 = 57600 , Baud115200 = 115200 , UnknownBaud = -1 } |
enum | DataBits { Data5 = 5 , Data6 = 6 , Data7 = 7 , Data8 = 8 , UnknownDataBits = -1 } |
enum | StopBits { OneStop = 1 , TwoStop = 2 , UnknownStopBits = -1 } |
enum | Parity { NoParity = 'N' , EvenParity = 'E' , OddParity = 'O' , SpaceParity = 'S' , MarkParity = 'M' , UnknownParity = -1 } |
enum | FlowControl { NoFlowControl = ' ' , HardwareControl = 'H' , SoftwareControl = 'S' , Rs485AfterSendControl = 'R' , Rs485OnSendControl = 'r' , Rs485RtsUpControl = Rs485AfterSendControl , Rs485RtsDownControl = Rs485OnSendControl , UnknownFlowControl = -1 } |
enum | Direction { NoDirection = 0 , Input = 1 , Output = 2 , AllDirections = Input | Output } |
enum | PinoutSignal { NoSignal = 0x00 , TransmittedDataSignal = 0x01 , ReceivedDataSignal = 0x02 , DataTerminalReadySignal = 0x04 , DataCarrierDetectSignal = 0x08 , DataSetReadySignal = 0x10 , RingIndicatorSignal = 0x20 , RequestToSendSignal = 0x40 , ClearToSendSignal = 0x80 , SecondaryTransmittedDataSignal = 0x100 , SecondaryReceivedDataSignal = 0x200 } |
typedef Flags< Direction > | Directions |
typedef Flags< PinoutSignal > | PinoutSignals |
enum | OpenModeFlag { NotOpen = 0x0000 , Append = std::ios_base::app , AtEnd = std::ios_base::ate , Binary = std::ios_base::binary , ReadOnly = std::ios_base::in , WriteOnly = std::ios_base::out , ReadWrite = ReadOnly | WriteOnly , Truncate = std::ios_base::trunc , IosModes = Append | AtEnd | Binary | ReadOnly | WriteOnly | Truncate , Unbuffered = (Truncate << 1) } |
Flags that specify how the device is to be opened. More... | |
typedef Flags< OpenModeFlag > | OpenMode |
Type representing a combination of OpenModeFlag values. | |
Public Member Functions | |
SerialPort () | |
SerialPort (const Info &serialPortInfo) | |
SerialPort (const std::string &path) | |
virtual | ~SerialPort () |
Closes the serial port, if necessary, and then destroys object. | |
void | discard (Directions directions=AllDirections) |
void | setPort (const Info &info) |
void | setPortName (const std::string &name) |
std::string | portName () const |
bool | setSettings (const Settings &settings) |
Settings | settings () const |
settings information | |
bool | setBaudRate (int32_t baudRate, Directions directions=AllDirections) |
int32_t | baudRate (Directions directions=AllDirections) const |
the data baud rate for the desired direction | |
bool | setDataBits (DataBits dataBits) |
DataBits | dataBits () const |
the data bits in a frame | |
bool | setParity (Parity parity) |
Parity | parity () const |
the parity checking mode | |
bool | setStopBits (StopBits stopBits) |
StopBits | stopBits () const |
the number of stop bits in a frame | |
bool | setFlowControl (FlowControl flowControl) |
FlowControl | flowControl () const |
the desired flow control mode | |
bool | setDataTerminalReady (bool set) |
bool | isDataTerminalReady () |
bool | setRequestToSend (bool set) |
bool | isRequestToSend () |
PinoutSignals | pinoutSignals () |
bool | sendBreak (int duration=0) |
bool | setBreakEnabled (bool set=true) |
bool | isBreakEnabled () const |
virtual ssize_t | write (const char *data, size_t maxSize) |
virtual ssize_t | write (const char *data, size_t maxSize, bool endl) |
ssize_t | available () const |
virtual ssize_t | read (char *data, size_t maxSize) |
ssize_t | read (char *buf, size_t maxSize, long msTimeout) |
ssize_t | read (std::string &str, long msTimeout=0) |
ssize_t | read (char &c, long msTimeout=0) |
ssize_t | peek (char *buf, size_t len, long msTimeout=0) |
ssize_t | peek (std::string &str, long msTimeout=0) |
ssize_t | peek (char &c, long msTimeout=0) |
Piduino::TerminalNotifier & | notifier () |
const Piduino::TerminalNotifier & | notifier () const |
ssize_t | write (const char *str) |
ssize_t | write (const std::string &str) |
ssize_t | write (char c) |
virtual bool | open (OpenMode mode=ReadWrite) |
virtual void | close () |
virtual void | setPath (const std::string &path) |
virtual std::string | path () const |
bool | setFd (int fd) |
int | fd () const |
bool | isOurFile () const |
OpenMode | openMode () const |
Returns the current open mode of the device. | |
virtual bool | isOpen () const |
Returns true if the device is currently open. | |
bool | isReadable () const |
Returns true if the device is readable. | |
bool | isWritable () const |
Returns true if the device is writable. | |
bool | isBuffered () const |
Returns true if the device is buffered. | |
virtual bool | isSequential () const |
Returns true if this device is sequential; otherwise returns false. | |
void | setTextModeEnabled (bool enabled) |
Enables or disables text mode for the device. | |
bool | isTextModeEnabled () const |
Returns true if text mode is enabled. | |
void | setDebug (bool enabled) |
Enables or disables debug mode for the device. | |
bool | isDebug () const |
Returns true if debug mode is enabled. | |
virtual std::string | errorString () const |
Returns a human-readable description of the last error. | |
virtual int | error () const |
Returns the code of the last error. | |
Protected Member Functions | |
SerialPort (Private &dd) | |
Protected Attributes | |
std::unique_ptr< Private > | d_ptr |
Pointer to the private implementation. | |
Provides functions to access serial ports.
You can get information about the available serial ports using the SerialPort::Info helper class, which allows an enumeration of all the serial ports in the system. This is useful to obtain the correct name of the serial port you want to use. You can pass an object of the helper class as an argument to the setPort() or setPortName() methods to assign the desired serial device.
After setting the port, you can open it in read-only (r/o), write-only (w/o), or read-write (r/w) mode using the open() method.
Use the close() method to close the port and cancel the I/O operations.
Having successfully opened, SerialPort tries to determine the current configuration of the port and initializes itself. You can reconfigure the port to the desired setting using the setBaudRate(), setDataBits(), setParity(), setStopBits(), and setFlowControl() methods.
There are a couple of properties to work with the pinout signals namely: SerialPort::dataTerminalReady, SerialPort::requestToSend. It is also possible to use the pinoutSignals() method to query the current pinout signals set.
Once you know that the ports are ready to read or write, you can use the read() or write() methods.
SerialPort provides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking serial ports:
@list
@endlist
Definition at line 77 of file serialport.h.
Definition at line 243 of file serialport.h.
|
inherited |
Type representing a combination of OpenModeFlag values.
Definition at line 70 of file iodevice.h.
Definition at line 284 of file serialport.h.
This enum describes the baud rate which the communication device operates with.
@value Baud1200 1200 baud. @value Baud2400 2400 baud. @value Baud4800 4800 baud. @value Baud9600 9600 baud. @value Baud19200 19200 baud. @value Baud38400 38400 baud. @value Baud57600 57600 baud. @value Baud115200 115200 baud. @value UnknownBaud Unknown baud. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Enumerator | |
---|---|
Baud1200 | |
Baud2400 | |
Baud4800 | |
Baud9600 | |
Baud19200 | |
Baud38400 | |
Baud57600 | |
Baud115200 | |
UnknownBaud |
Definition at line 101 of file serialport.h.
This enum describes the number of data bits used.
@value Data5 The number of data bits in each character is 5. It is used for Baudot code. It generally only makes sense with older equipment such as teleprinters. @value Data6 The number of data bits in each character is 6. It is rarely used. @value Data7 The number of data bits in each character is 7. It is used for true ASCII. It generally only makes sense with older equipment such as teleprinters. @value Data8 The number of data bits in each character is 8. It is used for most kinds of data, as this size matches the size of a byte. It is almost universally used in newer applications. @value UnknownDataBits Unknown number of bits. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Enumerator | |
---|---|
Data5 | |
Data6 | |
Data7 | |
Data8 | |
UnknownDataBits |
Definition at line 136 of file serialport.h.
This enum describes the possible directions of the data transmission.
@value Input Input direction. @value Output Output direction. @value AllDirections Simultaneously in two directions.
Enumerator | |
---|---|
NoDirection | |
Input | |
Output | |
AllDirections |
Definition at line 237 of file serialport.h.
Enumerator | |
---|---|
NoFlowControl | |
HardwareControl | |
SoftwareControl | |
Rs485AfterSendControl | |
Rs485OnSendControl | |
Rs485RtsUpControl | |
Rs485RtsDownControl | |
UnknownFlowControl |
Definition at line 213 of file serialport.h.
|
inherited |
Flags that specify how the device is to be opened.
These flags control the behavior of the device when it is opened. They can be combined using bitwise OR.
Definition at line 48 of file iodevice.h.
This enum describes the parity scheme used.
@value NoParity No parity bit it sent. This is the most common parity setting. Error detection is handled by the communication protocol. @value EvenParity The number of 1 bits in each character, including the parity bit, is always even. @value OddParity The number of 1 bits in each character, including the parity bit, is always odd. It ensures that at least one state transition occurs in each character. @value SpaceParity Space parity. The parity bit is sent in the space signal condition. It does not provide error detection information. @value MarkParity Mark parity. The parity bit is always set to the mark signal condition (logical 1). It does not provide error detection information. @value UnknownParity Unknown parity. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Enumerator | |
---|---|
NoParity | |
EvenParity | |
OddParity | |
SpaceParity | |
MarkParity | |
UnknownParity |
Definition at line 188 of file serialport.h.
Definition at line 271 of file serialport.h.
Enumerator | |
---|---|
OneStop | |
TwoStop | |
UnknownStopBits |
Definition at line 157 of file serialport.h.
Piduino::SerialPort::SerialPort | ( | ) |
Constructs a new serial port object
Piduino::SerialPort::SerialPort | ( | const Info & | serialPortInfo | ) |
Constructs a new serial port object to represent the serial port with the specified helper class serialPortInfo.
Piduino::SerialPort::SerialPort | ( | const std::string & | path | ) |
Constructs a new serial port object to represent the serial port with the specified path.
The path should have a specific format; see the FileDevice::setPath() method
|
virtual |
Closes the serial port, if necessary, and then destroys object.
|
protected |
|
inherited |
Returns the number of incoming bytes that are waiting to be read.
Piduino::SerialPort::baudRate | ( | Directions | directions = AllDirections | ) | const |
the data baud rate for the desired direction
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the SerialPort::error property. To set the baud rate, use the enumeration SerialPort::BaudRate or any positive int32_t value.
The default value is Baud9600, i.e. 9600 bits per second.
|
virtualinherited |
Closes the device and sets its OpenMode to NotOpen
Reimplemented from Piduino::IoDevice.
Piduino::SerialPort::dataBits | ( | ) | const |
the data bits in a frame
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the SerialPort::error property.
The default value is Data8, i.e. 8 data bits.
void Piduino::SerialPort::discard | ( | Directions | directions = AllDirections | ) |
Discards all characters from the output or input buffer, depending on given directions directions. This includes clearing the internal class buffers and the UART (driver) buffers. Also terminate pending read or write operations. If successful, returns true
; otherwise returns false
.
false
and sets the NotOpenError error code.
|
virtualinherited |
Returns the code of the last error.
|
virtualinherited |
Returns a human-readable description of the last error.
|
inherited |
Returns the file descriptor of the file, if it is open, else returns -1.
Do not use the close() function with this descriptor !
Piduino::SerialPort::flowControl | ( | ) | const |
the desired flow control mode
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the SerialPort::error property.
The default value is NoFlowControl, i.e. no flow control.
bool Piduino::SerialPort::isBreakEnabled | ( | ) | const |
|
inherited |
Returns true if the device is buffered.
bool Piduino::SerialPort::isDataTerminalReady | ( | ) |
|
inherited |
Returns true if debug mode is enabled.
|
virtualinherited |
Returns true if the device is currently open.
|
inherited |
|
inherited |
Returns true if the device is readable.
bool Piduino::SerialPort::isRequestToSend | ( | ) |
|
virtualinherited |
Returns true if this device is sequential; otherwise returns false.
Sequential devices, as opposed to random-access devices, have no concept of a start, an end, a size, or a current position, and do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential. Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.
The IoDevice implementation returns false.
|
inherited |
Returns true if text mode is enabled.
|
inherited |
Returns true if the device is writable.
|
inherited |
|
inherited |
Opens the file device using OpenMode mode, and then returns true
if successful; otherwise returns false
and sets an error code which can be obtained by calling the error() method. The error string is also reset.
Reimplemented from Piduino::IoDevice.
|
inherited |
Returns the current open mode of the device.
Piduino::SerialPort::parity | ( | ) | const |
the parity checking mode
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the SerialPort::error property.
The default value is NoParity, i.e. no parity.
|
virtualinherited |
Returns the path set by setPath() or to the FileDevice constructors.
|
inherited |
|
inherited |
|
inherited |
PinoutSignals Piduino::SerialPort::pinoutSignals | ( | ) |
Returns the state of the line signals in a bitmap format.
From this result, it is possible to allocate the state of the desired signal by applying a mask "AND", where the mask is the desired enumeration value from SerialPort::PinoutSignals.
std::string Piduino::SerialPort::portName | ( | ) | const |
Returns the name set by setPort() or passed to the SerialPort constructor. This name is short, i.e. it is extracted and converted from the internal variable system location of the device.
|
inherited |
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads at most maxSize bytes from the terminal into data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a terminal opened in WriteOnly mode, this function returns -1.
0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases.
|
virtualinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads at most maxSize bytes from the terminal into data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a terminal opened in WriteOnly mode, this function returns -1.
0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases.
Reimplemented from Piduino::FileStream.
|
inherited |
bool Piduino::SerialPort::sendBreak | ( | int | duration = 0 | ) |
Sends a continuous stream of zero bits during a specified period of time duration in msec if the terminal is using asynchronous serial data. If successful, returns true
; otherwise returns false
.
If the duration is zero then zero bits are transmitted by at least 0.25
seconds, but no more than 0.5
seconds.
If the duration is non zero then zero bits are transmitted within a certain period of time depending on the implementation.
false
and sets the NotOpenError error code.bool Piduino::SerialPort::setBaudRate | ( | int32_t | baudRate, |
Directions | directions = AllDirections |
||
) |
bool Piduino::SerialPort::setBreakEnabled | ( | bool | set = true | ) |
bool Piduino::SerialPort::setDataBits | ( | DataBits | dataBits | ) |
bool Piduino::SerialPort::setDataTerminalReady | ( | bool | set | ) |
|
inherited |
Enables or disables debug mode for the device.
enabled | True to enable debug mode, false to disable. |
|
inherited |
bool Piduino::SerialPort::setFlowControl | ( | FlowControl | flowControl | ) |
bool Piduino::SerialPort::setParity | ( | Parity | parity | ) |
|
virtualinherited |
Sets the path of the file. The path can have no prefix, a relative path, or an absolute path.
Do not call this function if the file has already been opened, if so, the file is closed before the path is modified.
If the file name has no path or a relative path, the path used will be the application's current directory path at the time of the open() call.
Note that the directory separator is "/".
void Piduino::SerialPort::setPort | ( | const Info & | info | ) |
Sets the port stored in the serial port info instance serialPortInfo.
void Piduino::SerialPort::setPortName | ( | const std::string & | name | ) |
bool Piduino::SerialPort::setRequestToSend | ( | bool | set | ) |
bool Piduino::SerialPort::setSettings | ( | const Settings & | settings | ) |
bool Piduino::SerialPort::setStopBits | ( | StopBits | stopBits | ) |
|
inherited |
Enables or disables text mode for the device.
enabled | True to enable text mode, false to disable. |
Piduino::SerialPort::settings | ( | ) | const |
settings information
Piduino::SerialPort::stopBits | ( | ) | const |
the number of stop bits in a frame
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the SerialPort::error property.
The default value is OneStop, i.e. 1 stop bit.
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes the character c to the stream. Returns the number of bytes that were actually written, or -1 if an error occurred.
|
virtual |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes at most maxSize bytes of data from data to the stream. Returns the number of bytes that were actually written, or -1 if an error occurred.
Reimplemented from Piduino::FileStream.
|
virtual |
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes data from a zero-terminated string of 8-bit characters to the stream. Returns the number of bytes that were actually written, or -1 if an error occurred.
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes the content of string to the stream. Returns the number of bytes that were actually written, or -1 if an error occurred.
|
protectedinherited |
Pointer to the private implementation.
Definition at line 190 of file iodevice.h.