#include <serialport.h>
Classes | |
struct | Error |
Public Member Functions | |
SerialPort () | |
virtual | ~SerialPort () |
virtual bool | init (utility::ErrorState &errorState) override |
virtual bool | start (utility::ErrorState &errorState) override |
virtual void | stop () override |
bool | isOpen () const |
uint32 | read (std::vector< uint8 > &buffer, uint32 count, SerialPort::Error &error) |
uint32 | read (std::vector< uint8 > &buffer, SerialPort::Error &error) |
uint32 | read (uint8 *buffer, uint32 count, SerialPort::Error &error) |
uint32 | read (std::string &buffer, uint32 count, SerialPort::Error &error) |
std::string | read (uint32 count, SerialPort::Error &error) |
uint32 | readLine (std::string &buffer, uint32 length, const std::string &eol, SerialPort::Error &error) |
std::string | readLine (uint32 length, const std::string &eol, SerialPort::Error &error) |
std::vector< std::string > | readLines (uint32 length, const std::string &eol, SerialPort::Error &error) |
uint32 | write (const uint8 *data, uint32 count, SerialPort::Error &error) |
uint32 | write (const std::vector< uint8 > &data, SerialPort::Error &error) |
uint32 | write (const std::string &data, SerialPort::Error &error) |
uint32 | available () |
bool | waitReadable () |
void | waitByteTimes (uint32 count) |
void | flush () |
void | flushInput () |
void | flushOutput () |
void | sendBreak (uint32 duration) |
bool | waitForChange () |
void | setBreak (bool level=true) |
void | setRTS (bool level=true) |
void | setDTR (bool level=true) |
bool | getCTS () |
bool | getDSR () |
bool | getRI () |
bool | getCD () |
![]() | |
Resource () | |
![]() | |
Object () | |
virtual | ~Object () |
virtual void | onDestroy () |
Object (Object &)=delete | |
Object & | operator= (const Object &)=delete |
Object (Object &&)=delete | |
Object & | operator= (Object &&)=delete |
Public Attributes | |
Signal< SerialPort * > | destructed |
std::string | mPortName = "COM1" |
Property: "PortName" the serial port to open and communicate with. Something like 'COM1' on windows and '/dev/ttyS0' on linux. More... | |
int | mBaudRate = 9600 |
Property: "BaudRate" the baud rate of the serial port. More... | |
ESerialByteSize | mByteSize = ESerialByteSize::Eight |
Property: "ByteSize" size of each byte in the serial transmission of data. More... | |
ESerialFlowControl | mFlowControl = ESerialFlowControl::None |
Property: "FlowControl" type of flow control that is used. More... | |
ESerialParity | mParity = ESerialParity::None |
Property: "Parity" parity method. More... | |
ESerialStopBits | mStopBits = ESerialStopBits::One |
Property: "StopBits" number of stop bits used. More... | |
int | mReadTimeout = 0 |
Property: "ReadTimeout" the time in ms until a timeout occurs after a call to read is made. 0 = non blocking mode. More... | |
int | mWriteTimeout = 0 |
Property: "WriteTimeout" the time in ms until a timeout occurs after a call to write is made. 0 = non blocking mode. More... | |
int | mInterByteTimeout = 0 |
Property: "InterByteTimeout" the max amount of time in ms between receiving bytes that can pass before a timeout occurs. Setting this to 0 will prevent inter byte timeouts. More... | |
bool | mAllowFailure = false |
Property: "AllowFailure" when set to true opening the port is allowed to fail on startup. More... | |
![]() | |
std::string | mID |
Property: 'mID' unique name of the object. Used as an identifier by the system. More... | |
Additional Inherited Members | |
![]() | |
static bool | isIDProperty (rtti::Instance &object, const rtti::Property &property) |
Serial port device interface. Creates and opens a serial communication port. The port is opened using the specified properties when the device is started. The serial connection is closed when the device is stopped. Valid address names of the serial port would be something like 'COM1' on Windows an '/dev/ttyS0' on Linux. By default all serial communication (reading / writing) is non-blocking.
SerialPort | ( | ) |
Default constructor
|
virtual |
uint32 available | ( | ) |
void flush | ( | ) |
Flush the input and output buffers
void flushInput | ( | ) |
Flush the input buffers
void flushOutput | ( | ) |
Flush the output buffers
bool getCD | ( | ) |
bool getCTS | ( | ) |
bool getDSR | ( | ) |
bool getRI | ( | ) |
|
overridevirtual |
Initialize this object after de-serialization.
errorState | contains the error message when initialization fails |
Reimplemented from Object.
bool isOpen | ( | ) | const |
Returns if the port has been opened successfully and is ready to be communicated with. The port won't be open when startup failed and 'AllowFailure' is turned on.
uint32 read | ( | std::string & | buffer, |
uint32 | count, | ||
SerialPort::Error & | error | ||
) |
Read a given amount of bytes from the serial port into the given buffer. The read function will return when the number of requested bytes was read or when a timeout occurs. A timeout occurs when the inter-byte timeout or when the read timeout has expired. An error is generated when an exception is thrown.
buffer | empty string to hold the read values. |
count | number of bytes to read. |
error | contains the error if the operation fails. |
uint32 read | ( | std::vector< uint8 > & | buffer, |
SerialPort::Error & | error | ||
) |
Read a given amount of bytes from the serial port into the given buffer. The read function will return when the number of requested bytes was read or when a timeout occurs. A timeout occurs when the inter-byte timeout or when the read timeout has expired. An error is generated when an exception is thrown. The size of the buffer is used to determine the number of bytes to read.
buffer | the buffer that will hold the read values. |
error | contains the error if the operation fails. |
uint32 read | ( | std::vector< uint8 > & | buffer, |
uint32 | count, | ||
SerialPort::Error & | error | ||
) |
Read a given amount of bytes from the serial port into the given buffer. The read function will return when the number of requested bytes was read or when a timeout occurs. A timeout occurs when the inter-byte timeout or when the read timeout has expired. An error is generated when an exception is thrown. The buffer is automatically resized to hold the requested number of bytes.
buffer | the buffer that will hold the read values. |
count | number of bytes to read. Buffer is resized to fit this number of bytes. |
error | contains the error if the operation fails. |
std::string read | ( | uint32 | count, |
SerialPort::Error & | error | ||
) |
Read a given amount of bytes from the serial port and return a string containing the data.
count | the total number of bytes to read. |
error | contains the error if the operation fails. |
uint32 read | ( | uint8 * | buffer, |
uint32 | count, | ||
SerialPort::Error & | error | ||
) |
Read a given amount of bytes from the serial port into the given buffer. The read function will return when the number of requested bytes was read or when a timeout occurs. A timeout occurs when the inter-byte timeout or when the read timeout has expired. An error is generated when an exception is thrown.
buffer | the buffer that will hold the read values, must be of size count. |
count | number of bytes to read. |
error | contains the error if the operation fails. |
uint32 readLine | ( | std::string & | buffer, |
uint32 | length, | ||
const std::string & | eol, | ||
SerialPort::Error & | error | ||
) |
Reads in a line or until a given delimiter has been processed.
buffer | empty string that will contain the read data. |
length | maximum character length of the line |
eol | end of line delimiter. |
error | contains the error if the operation fails. |
std::string readLine | ( | uint32 | length, |
const std::string & | eol, | ||
SerialPort::Error & | error | ||
) |
Reads in a line or until a given delimiter has been processed.
length | maximum character length of the line. |
eol | end of line delimiter. |
error | contains the error if the operation fails. |
std::vector<std::string> readLines | ( | uint32 | length, |
const std::string & | eol, | ||
SerialPort::Error & | error | ||
) |
Reads multiple lines until the serial port times out. This requires a timeout > 0 before it can be run. It will read until a timeout occurs and return a list of strings.
length | maximum character length of all lines combined. |
eol | end of line delimiter that is used to separate individual strings. |
error | contains the error if the operation fails. |
void sendBreak | ( | uint32 | duration | ) |
Sends the RS-232 break signal.
void setBreak | ( | bool | level = true | ) |
Set the break condition to the given level.
level | new break level. |
void setDTR | ( | bool | level = true | ) |
Set the DTR handshaking line to the given level.
level | the new DTR level. |
void setRTS | ( | bool | level = true | ) |
Set the RTS handshaking line to the given level.
level | the new RTS level |
|
overridevirtual |
Opens the serial port using the associated port properties.
errorState | contains the error if the serial port can't be opened. |
Reimplemented from Device.
|
overridevirtual |
Closes the serial port.
Reimplemented from Device.
void waitByteTimes | ( | uint32 | count | ) |
bool waitForChange | ( | ) |
Blocks until CTS, DSR, RI, CD changes or something interrupts it.
bool waitReadable | ( | ) |
Block until serial data can be read or number of ms have elapsed. The return value is true when the function exits with the port in a readable state, false otherwise (due to timeout or select interruption).
uint32 write | ( | const std::string & | data, |
SerialPort::Error & | error | ||
) |
Write a string to the serial port.
data | string that is written |
error | contains the error if the operation fails. |
uint32 write | ( | const std::vector< uint8 > & | data, |
SerialPort::Error & | error | ||
) |
Write a buffer to the serial port.
data | data that is written |
error | contains the error if the operation fails. |
uint32 write | ( | const uint8 * | data, |
uint32 | count, | ||
SerialPort::Error & | error | ||
) |
Write a buffer to the serial port.
data | pointer to the data that is written, must be of size count. |
count | number of bytes to write |
error | contains the error if the operation fails. |
Signal<SerialPort*> destructed |
Signal emitted when the serial port is destructed.
bool mAllowFailure = false |
Property: "AllowFailure" when set to true opening the port is allowed to fail on startup.
int mBaudRate = 9600 |
Property: "BaudRate" the baud rate of the serial port.
ESerialByteSize mByteSize = ESerialByteSize::Eight |
Property: "ByteSize" size of each byte in the serial transmission of data.
ESerialFlowControl mFlowControl = ESerialFlowControl::None |
Property: "FlowControl" type of flow control that is used.
int mInterByteTimeout = 0 |
Property: "InterByteTimeout" the max amount of time in ms between receiving bytes that can pass before a timeout occurs. Setting this to 0 will prevent inter byte timeouts.
ESerialParity mParity = ESerialParity::None |
Property: "Parity" parity method.
std::string mPortName = "COM1" |
Property: "PortName" the serial port to open and communicate with. Something like 'COM1' on windows and '/dev/ttyS0' on linux.
int mReadTimeout = 0 |
Property: "ReadTimeout" the time in ms until a timeout occurs after a call to read is made. 0 = non blocking mode.
ESerialStopBits mStopBits = ESerialStopBits::One |
Property: "StopBits" number of stop bits used.
int mWriteTimeout = 0 |
Property: "WriteTimeout" the time in ms until a timeout occurs after a call to write is made. 0 = non blocking mode.