NAP
Classes | Public Member Functions | Public Attributes | List of all members
SerialPort Class Reference

#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 ()
 
- Public Member Functions inherited from Resource
 Resource ()
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (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...
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 

Description

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.

Inheritance diagram for SerialPort:
[legend]
Collaboration diagram for SerialPort:
[legend]

Constructor & Destructor Documentation

◆ SerialPort()

Default constructor

◆ ~SerialPort()

virtual ~SerialPort ( )
virtual

Member Function Documentation

◆ available()

uint32 available ( )
Returns
the number of characters in the buffer

◆ flush()

void flush ( )

Flush the input and output buffers

◆ flushInput()

void flushInput ( )

Flush the input buffers

◆ flushOutput()

void flushOutput ( )

Flush the output buffers

◆ getCD()

bool getCD ( )
Returns
the current status of the CD line.

◆ getCTS()

bool getCTS ( )
Returns
the current status of the CTS line.

◆ getDSR()

bool getDSR ( )
Returns
the current status of the DSR line.

◆ getRI()

bool getRI ( )
Returns
the current status of the RI line.

◆ init()

virtual bool init ( utility::ErrorState errorState)
overridevirtual

Initialize this object after de-serialization.

Parameters
errorStatecontains the error message when initialization fails

Reimplemented from Object.

◆ isOpen()

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.

Returns
if the port is open and ready to communicate with.

◆ read() [1/5]

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.

Parameters
bufferempty string to hold the read values.
countnumber of bytes to read.
errorcontains the error if the operation fails.
Returns
the total number of bytes read.

◆ read() [2/5]

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.

Parameters
bufferthe buffer that will hold the read values.
errorcontains the error if the operation fails.
Returns
the total number of bytes read.

◆ read() [3/5]

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.

Parameters
bufferthe buffer that will hold the read values.
countnumber of bytes to read. Buffer is resized to fit this number of bytes.
errorcontains the error if the operation fails.
Returns
the total number of bytes read.

◆ read() [4/5]

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.

Parameters
countthe total number of bytes to read.
errorcontains the error if the operation fails.
Returns
a string that contains the data read from the port.

◆ read() [5/5]

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.

Parameters
bufferthe buffer that will hold the read values, must be of size count.
countnumber of bytes to read.
errorcontains the error if the operation fails.
Returns
the total number of bytes read.

◆ readLine() [1/2]

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.

Parameters
bufferempty string that will contain the read data.
lengthmaximum character length of the line
eolend of line delimiter.
errorcontains the error if the operation fails.
Returns
number of bytes read.

◆ readLine() [2/2]

std::string readLine ( uint32  length,
const std::string &  eol,
SerialPort::Error error 
)

Reads in a line or until a given delimiter has been processed.

Parameters
lengthmaximum character length of the line.
eolend of line delimiter.
errorcontains the error if the operation fails.
Returns
string that contains the line.

◆ readLines()

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.

Parameters
lengthmaximum character length of all lines combined.
eolend of line delimiter that is used to separate individual strings.
errorcontains the error if the operation fails.
Returns
list of strings.

◆ sendBreak()

void sendBreak ( uint32  duration)

Sends the RS-232 break signal.

◆ setBreak()

void setBreak ( bool  level = true)

Set the break condition to the given level.

Parameters
levelnew break level.

◆ setDTR()

void setDTR ( bool  level = true)

Set the DTR handshaking line to the given level.

Parameters
levelthe new DTR level.

◆ setRTS()

void setRTS ( bool  level = true)

Set the RTS handshaking line to the given level.

Parameters
levelthe new RTS level

◆ start()

virtual bool start ( utility::ErrorState errorState)
overridevirtual

Opens the serial port using the associated port properties.

Parameters
errorStatecontains the error if the serial port can't be opened.
Returns
if the device started

Reimplemented from Device.

◆ stop()

virtual void stop ( )
overridevirtual

Closes the serial port.

Reimplemented from Device.

◆ waitByteTimes()

void waitByteTimes ( uint32  count)

◆ waitForChange()

bool waitForChange ( )

Blocks until CTS, DSR, RI, CD changes or something interrupts it.

Returns
true if one of the lines has changed, false if something else occurred.

◆ waitReadable()

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).

◆ write() [1/3]

uint32 write ( const std::string &  data,
SerialPort::Error error 
)

Write a string to the serial port.

Parameters
datastring that is written
errorcontains the error if the operation fails.
Returns
number of bytes actually written

◆ write() [2/3]

uint32 write ( const std::vector< uint8 > &  data,
SerialPort::Error error 
)

Write a buffer to the serial port.

Parameters
datadata that is written
errorcontains the error if the operation fails.
Returns
number of bytes actually written

◆ write() [3/3]

uint32 write ( const uint8 data,
uint32  count,
SerialPort::Error error 
)

Write a buffer to the serial port.

Parameters
datapointer to the data that is written, must be of size count.
countnumber of bytes to write
errorcontains the error if the operation fails.
Returns
number of bytes actually written

Member Data Documentation

◆ destructed

Signal<SerialPort*> destructed

Signal emitted when the serial port is destructed.

◆ mAllowFailure

bool mAllowFailure = false

Property: "AllowFailure" when set to true opening the port is allowed to fail on startup.

◆ mBaudRate

int mBaudRate = 9600

Property: "BaudRate" the baud rate of the serial port.

◆ mByteSize

Property: "ByteSize" size of each byte in the serial transmission of data.

◆ mFlowControl

Property: "FlowControl" type of flow control that is used.

◆ mInterByteTimeout

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.

◆ mParity

Property: "Parity" parity method.

◆ mPortName

std::string mPortName = "COM1"

Property: "PortName" the serial port to open and communicate with. Something like 'COM1' on windows and '/dev/ttyS0' on linux.

◆ mReadTimeout

int mReadTimeout = 0

Property: "ReadTimeout" the time in ms until a timeout occurs after a call to read is made. 0 = non blocking mode.

◆ mStopBits

Property: "StopBits" number of stop bits used.

◆ mWriteTimeout

int mWriteTimeout = 0

Property: "WriteTimeout" the time in ms until a timeout occurs after a call to write is made. 0 = non blocking mode.