qCNC 0.82-alpha
QextSerialPort Class Reference

#include <qextserialport.h>

Collaboration diagram for QextSerialPort:

List of all members.

Public Types

enum  QueryMode { Polling, EventDriven }

Signals

void dsrChanged (bool status)

Public Member Functions

 QextSerialPort (QueryMode mode=EventDriven)
 QextSerialPort (const QString &name, QueryMode mode=EventDriven)
 QextSerialPort (PortSettings const &s, QueryMode mode=EventDriven)
 QextSerialPort (const QString &name, PortSettings const &s, QueryMode mode=EventDriven)
 ~QextSerialPort ()
void setPortName (const QString &name)
QString portName () const
QueryMode queryMode () const
void setQueryMode (QueryMode mode)
void setBaudRate (BaudRateType)
BaudRateType baudRate () const
void setDataBits (DataBitsType)
DataBitsType dataBits () const
void setParity (ParityType)
ParityType parity () const
void setStopBits (StopBitsType)
StopBitsType stopBits () const
void setFlowControl (FlowType)
FlowType flowControl () const
void setTimeout (long)
bool open (OpenMode mode)
bool isSequential () const
void close ()
void flush ()
qint64 size () const
qint64 bytesAvailable () const
QByteArray readAll ()
void ungetChar (char c)
ulong lastError () const
void translateError (ulong error)
void setDtr (bool set=true)
void setRts (bool set=true)
ulong lineStatus ()
QString errorString ()

Protected Member Functions

void construct ()
void platformSpecificDestruct ()
void platformSpecificInit ()
qint64 readData (char *data, qint64 maxSize)
qint64 writeData (const char *data, qint64 maxSize)

Protected Attributes

QMutex * mutex
QString port
PortSettings Settings
ulong lastErr
QueryMode _queryMode

Detailed Description

Encapsulates a serial port on both POSIX and Windows systems.

Note:
Be sure to check the full list of members, as QIODevice provides quite a lot of functionality for QextSerialPort.

Usage

QextSerialPort offers both a polling and event driven API. Event driven is typically easier to use, since you never have to worry about checking for new data.

Example

QextSerialPort* port = new QextSerialPort("COM1", QextSerialPort::EventDriven);
connect(port, SIGNAL(readyRead()), myClass, SLOT(onDataAvailable()));
port->open();

void MyClass::onDataAvailable() {
    int avail = port->bytesAvailable();
    if( avail > 0 ) {
        QByteArray usbdata;
        usbdata.resize(avail);
        int read = port->read(usbdata.data(), usbdata.size());
        if( read > 0 ) {
            processNewData(usbdata);
        }
    }
}

Compatibility

The user will be notified of errors and possible portability conflicts at run-time by default - this behavior can be turned off by defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn off portability warnings) in the project.

On Windows NT/2000/XP this class uses Win32 serial port functions by default. The user may select POSIX behavior under NT, 2000, or XP ONLY by defining Q_OS_UNIX in the project. No guarantees are made as to the quality of POSIX support under NT/2000 however.

Author:
Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz

Definition at line 182 of file qextserialport.h.


Member Enumeration Documentation

Enumerator:
Polling 
EventDriven 

Definition at line 186 of file qextserialport.h.


Constructor & Destructor Documentation

QextSerialPort::QextSerialPort ( QextSerialPort::QueryMode  mode = EventDriven)

Default constructor. Note that the name of the device used by a QextSerialPort constructed with this constructor will be determined by #defined constants, or lack thereof - the default behavior is the same as _TTY_LINUX_. Possible naming conventions and their associated constants are:


Constant         Used By         Naming Convention
----------       -------------   ------------------------
Q_OS_WIN        Windows         COM1, COM2
_TTY_IRIX_       SGI/IRIX        /dev/ttyf1, /dev/ttyf2
_TTY_HPUX_       HP-UX           /dev/tty1p0, /dev/tty2p0
_TTY_SUN_        SunOS/Solaris   /dev/ttya, /dev/ttyb
_TTY_DIGITAL_    Digital UNIX    /dev/tty01, /dev/tty02
_TTY_FREEBSD_    FreeBSD         /dev/ttyd0, /dev/ttyd1
_TTY_OPENBSD_    OpenBSD         /dev/tty00, /dev/tty01
_TTY_LINUX_      Linux           /dev/ttyS0, /dev/ttyS1
<none>           Linux           /dev/ttyS0, /dev/ttyS1

This constructor assigns the device name to the name of the first port on the specified system. See the other constructors if you need to open a different port.

Definition at line 29 of file qextserialport.cpp.

Here is the call graph for this function:

QextSerialPort::QextSerialPort ( const QString &  name,
QextSerialPort::QueryMode  mode = EventDriven 
)

Constructs a serial port attached to the port specified by name. name is the name of the device, which is windowsystem-specific, e.g."COM1" or "/dev/ttyS0".

Definition at line 68 of file qextserialport.cpp.

Here is the call graph for this function:

QextSerialPort::QextSerialPort ( PortSettings const &  s,
QextSerialPort::QueryMode  mode = EventDriven 
)

Constructs a port with default name and specified settings.

Definition at line 80 of file qextserialport.cpp.

Here is the call graph for this function:

QextSerialPort::QextSerialPort ( const QString &  name,
PortSettings const &  s,
QextSerialPort::QueryMode  mode = EventDriven 
)

Constructs a port with specified name and settings.

Definition at line 97 of file qextserialport.cpp.

Here is the call graph for this function:

QextSerialPort::~QextSerialPort ( )

Standard destructor.

Definition at line 247 of file qextserialport.cpp.

Here is the call graph for this function:


Member Function Documentation

BaudRateType QextSerialPort::baudRate ( void  ) const

Returns the baud rate of the serial port. For a list of possible return values see the definition of the enum BaudRateType.

Definition at line 169 of file qextserialport.cpp.

Here is the caller graph for this function:

qint64 QextSerialPort::bytesAvailable ( ) const

Returns the number of bytes waiting in the port's receive queue. This function will return 0 if the port is not currently open, or -1 on error.

Definition at line 784 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::close ( )

Closes a serial port. This function has no effect if the serial port associated with the class is not currently open.

Definition at line 733 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void QextSerialPort::construct ( ) [protected]

Common constructor function for setting up default port settings. (115200 Baud, 8N1, Hardware flow control where supported, otherwise no flow control, and 0 ms timeout).

Definition at line 116 of file qextserialport.cpp.

Here is the caller graph for this function:

DataBitsType QextSerialPort::dataBits ( ) const

Returns the number of data bits used by the port. For a list of possible values returned by this function, see the definition of the enum DataBitsType.

Definition at line 178 of file qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::dsrChanged ( bool  status) [signal]

This signal is emitted whenever port settings are updated.

Parameters:
validtrue if settings are valid, false otherwise.
Todo:
implement.

This signal is emitted whenever dsr line has changed its state. You may use this signal to check if device is connected.

Parameters:
statustrue when DSR signal is on, false otherwise.
See also:
lineStatus().
QString QextSerialPort::errorString ( )

Definition at line 220 of file qextserialport.cpp.

Here is the call graph for this function:

FlowType QextSerialPort::flowControl ( ) const

Returns the type of flow control used by the port. For a list of possible values returned by this function, see the definition of the enum FlowType.

Definition at line 205 of file qextserialport.cpp.

void QextSerialPort::flush ( )

Flushes all pending I/O to the serial port. This function has no effect if the serial port associated with the class is not currently open.

Definition at line 758 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

bool QextSerialPort::isSequential ( ) const

Returns true if device is sequential, otherwise returns false. Serial port is sequential device so this function always returns true. Check QIODevice::isSequential() documentation for more information.

Definition at line 215 of file qextserialport.cpp.

ulong QextSerialPort::lastError ( ) const
ulong QextSerialPort::lineStatus ( void  )

Returns the line status as stored by the port function. This function will retrieve the states of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned long with specific bits indicating which lines are high. The following constants should be used to examine the states of individual lines:

Mask        Line
------      ----
LS_CTS      CTS
LS_DSR      DSR
LS_DCD      DCD
LS_RI       RI
LS_RTS      RTS (POSIX only)
LS_DTR      DTR (POSIX only)
LS_ST       Secondary TXD (POSIX only)
LS_SR       Secondary RXD (POSIX only)

This function will return 0 if the port associated with the class is not currently open.

Returns the line status as stored by the port function. This function will retrieve the states of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned long with specific bits indicating which lines are high. The following constants should be used to examine the states of individual lines:

Mask        Line
------      ----
LS_CTS      CTS
LS_DSR      DSR
LS_DCD      DCD
LS_RI       RI

This function will return 0 if the port associated with the class is not currently open.

Definition at line 891 of file posix_qextserialport.cpp.

bool QextSerialPort::open ( OpenMode  mode)

Opens the serial port associated to this class. This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure.

Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure.

Definition at line 677 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

ParityType QextSerialPort::parity ( ) const

Returns the type of parity used by the port. For a list of possible values returned by this function, see the definition of the enum ParityType.

Definition at line 187 of file qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::platformSpecificDestruct ( ) [protected]

Standard destructor.

Definition at line 18 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::platformSpecificInit ( ) [protected]

Definition at line 9 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

QString QextSerialPort::portName ( ) const

Returns the name set by setPortName().

Definition at line 149 of file qextserialport.cpp.

Here is the caller graph for this function:

QueryMode QextSerialPort::queryMode ( ) const [inline]

! Get query mode.

Returns:
query mode.

Definition at line 204 of file qextserialport.h.

Here is the caller graph for this function:

QByteArray QextSerialPort::readAll ( )

Reads all available data from the device, and returns it as a QByteArray. This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

Definition at line 159 of file qextserialport.cpp.

Here is the call graph for this function:

qint64 QextSerialPort::readData ( char *  data,
qint64  maxSize 
) [protected]

Reads a block of data from the serial port. This function will read at most maxSize bytes from the serial port and place them in the buffer pointed to by data. Return value is the number of bytes actually read, or -1 on error.

Warning:
before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).

Reads a block of data from the serial port. This function will read at most maxlen bytes from the serial port and place them in the buffer pointed to by data. Return value is the number of bytes actually read, or -1 on error.

Warning:
before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).

Definition at line 933 of file posix_qextserialport.cpp.

void QextSerialPort::setBaudRate ( BaudRateType  baudRate)

Sets the baud rate of the serial port. Note that not all rates are applicable on all platforms. The following table shows translations of the various baud rate constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * are speeds that are usable on both Windows and POSIX.

Note:
BAUD76800 may not be supported on all POSIX systems. SGI/IRIX systems do not support BAUD1800.

  RATE          Windows Speed   POSIX Speed
  -----------   -------------   -----------
   BAUD50                 110          50
   BAUD75                 110          75
  *BAUD110                110         110
   BAUD134                110         134.5
   BAUD150                110         150
   BAUD200                110         200
  *BAUD300                300         300
  *BAUD600                600         600
  *BAUD1200              1200        1200
   BAUD1800              1200        1800
  *BAUD2400              2400        2400
  *BAUD4800              4800        4800
  *BAUD9600              9600        9600
   BAUD14400            14400        9600
  *BAUD19200            19200       19200
  *BAUD38400            38400       38400
   BAUD56000            56000       38400
  *BAUD57600            57600       57600
   BAUD76800            57600       76800
  *BAUD115200          115200      115200
   BAUD128000          128000      115200
   BAUD256000          256000      115200

Sets the baud rate of the serial port. Note that not all rates are applicable on all platforms. The following table shows translations of the various baud rate constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * are speeds that are usable on both Windows and POSIX.


  RATE          Windows Speed   POSIX Speed
  -----------   -------------   -----------
   BAUD50                 110          50
   BAUD75                 110          75
  *BAUD110                110         110
   BAUD134                110         134.5
   BAUD150                110         150
   BAUD200                110         200
  *BAUD300                300         300
  *BAUD600                600         600
  *BAUD1200              1200        1200
   BAUD1800              1200        1800
  *BAUD2400              2400        2400
  *BAUD4800              4800        4800
  *BAUD9600              9600        9600
   BAUD14400            14400        9600
  *BAUD19200            19200       19200
  *BAUD38400            38400       38400
   BAUD56000            56000       38400
  *BAUD57600            57600       57600
   BAUD76800            57600       76800
  *BAUD115200          115200      115200
   BAUD128000          128000      115200
   BAUD256000          256000      115200

Definition at line 59 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void QextSerialPort::setDataBits ( DataBitsType  dataBits)

Sets the number of data bits used by the serial port. Possible values of dataBits are:

    DATA_5      5 data bits
    DATA_6      6 data bits
    DATA_7      7 data bits
    DATA_8      8 data bits
Note:
This function is subject to the following restrictions:
5 data bits cannot be used with 2 stop bits.
8 data bits cannot be used with space parity on POSIX systems.

Sets the number of data bits used by the serial port. Possible values of dataBits are:

    DATA_5      5 data bits
    DATA_6      6 data bits
    DATA_7      7 data bits
    DATA_8      8 data bits
Note:
This function is subject to the following restrictions:
5 data bits cannot be used with 2 stop bits.
1.5 stop bits can only be used with 5 data bits.
8 data bits cannot be used with space parity on POSIX systems.

Definition at line 379 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void QextSerialPort::setDtr ( bool  set = true)

Sets DTR line to the requested state (high by default). This function will have no effect if the port associated with the class is not currently open.

Definition at line 833 of file posix_qextserialport.cpp.

void QextSerialPort::setFlowControl ( FlowType  flow)

Sets the flow control used by the port. Possible values of flow are:

    FLOW_OFF            No flow control
    FLOW_HARDWARE       Hardware (RTS/CTS) flow control
    FLOW_XONXOFF        Software (XON/XOFF) flow control
Note:
FLOW_HARDWARE may not be supported on all versions of UNIX. In cases where it is unsupported, FLOW_HARDWARE is the same as FLOW_OFF.

Sets the flow control used by the port. Possible values of flow are:

    FLOW_OFF            No flow control
    FLOW_HARDWARE       Hardware (RTS/CTS) flow control
    FLOW_XONXOFF        Software (XON/XOFF) flow control

Definition at line 605 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::setParity ( ParityType  parity)

Sets the parity associated with the serial port. The possible values of parity are:

    PAR_SPACE       Space Parity
    PAR_MARK        Mark Parity
    PAR_NONE        No Parity
    PAR_EVEN        Even Parity
    PAR_ODD         Odd Parity
Note:
This function is subject to the following limitations:
POSIX systems do not support mark parity.
POSIX systems support space parity only if tricked into doing so, and only with fewer than 8 data bits. Use space parity very carefully with POSIX systems.

Sets the parity associated with the serial port. The possible values of parity are:

    PAR_SPACE       Space Parity
    PAR_MARK        Mark Parity
    PAR_NONE        No Parity
    PAR_EVEN        Even Parity
    PAR_ODD         Odd Parity

Definition at line 467 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void QextSerialPort::setPortName ( const QString &  name)

Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".

Definition at line 137 of file qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::setQueryMode ( QueryMode  mode)

Set desired serial communication handling style. You may choose from polling or event driven approach. This function does nothing when port is open; to apply changes port must be reopened.

In event driven approach read() and write() functions are acting asynchronously. They return immediately and the operation is performed in the background, so they doesn't freeze the calling thread. To determine when operation is finished, QextSerialPort runs separate thread and monitors serial port events. Whenever the event occurs, adequate signal is emitted.

When polling is set, read() and write() are acting synchronously. Signals are not working in this mode and some functions may not be available. The advantage of polling is that it generates less overhead due to lack of signals emissions and it doesn't start separate thread to monitor events.

Generally event driven approach is more capable and friendly, although some applications may need as low overhead as possible and then polling comes.

Parameters:
modequery mode.

Definition at line 129 of file qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::setRts ( bool  set = true)

Sets RTS line to the requested state (high by default). This function will have no effect if the port associated with the class is not currently open.

Definition at line 853 of file posix_qextserialport.cpp.

void QextSerialPort::setStopBits ( StopBitsType  stopBits)

Sets the number of stop bits used by the serial port. Possible values of stopBits are:

    STOP_1      1 stop bit
    STOP_1_5    1.5 stop bits
    STOP_2      2 stop bits
Note:
This function is subject to the following restrictions:
2 stop bits cannot be used with 5 data bits.
POSIX does not support 1.5 stop bits.

Sets the number of stop bits used by the serial port. Possible values of stopBits are:

    STOP_1      1 stop bit
    STOP_1_5    1.5 stop bits
    STOP_2      2 stop bits
Note:
This function is subject to the following restrictions:
2 stop bits cannot be used with 5 data bits.
1.5 stop bits cannot be used with 6 or more data bits.
POSIX does not support 1.5 stop bits.

Definition at line 554 of file posix_qextserialport.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void QextSerialPort::setTimeout ( long  millisec)

Sets the read and write timeouts for the port to millisec milliseconds. Note that this is a per-character timeout, i.e. the port will wait this long for each individual character, not for the whole read operation. This timeout also applies to the bytesWaiting() function.

Note:
POSIX does not support millisecond-level control for I/O timeout values. Any timeout set using this function will be set to the next lowest tenth of a second for the purposes of detecting read or write timeouts. For example a timeout of 550 milliseconds will be seen by the class as a timeout of 500 milliseconds for the purposes of reading and writing the port. However millisecond-level control is allowed by the select() system call, so for example a 550-millisecond timeout will be seen as 550 milliseconds on POSIX systems for the purpose of detecting available bytes in the read buffer.

Sets the read and write timeouts for the port to millisec milliseconds. Setting 0 indicates that timeouts are not used for read nor write operations; however read() and write() functions will still block. Set -1 to provide non-blocking behaviour (read() and write() will return immediately).

Note:
this function does nothing in event driven mode.

Definition at line 653 of file posix_qextserialport.cpp.

Here is the caller graph for this function:

qint64 QextSerialPort::size ( ) const

This function will return the number of bytes waiting in the receive queue of the serial port. It is included primarily to provide a complete QIODevice interface, and will not record errors in the lastErr member (because it is const). This function is also not thread-safe - in multithreading situations, use QextSerialPort::bytesWaiting() instead.

This function will return the number of bytes waiting in the receive queue of the serial port. It is included primarily to provide a complete QIODevice interface, and will not record errors in the lastErr member (because it is const). This function is also not thread-safe - in multithreading situations, use QextSerialPort::bytesAvailable() instead.

Definition at line 771 of file posix_qextserialport.cpp.

StopBitsType QextSerialPort::stopBits ( ) const

Returns the number of stop bits used by the port. For a list of possible return values, see the definition of the enum StopBitsType.

Definition at line 196 of file qextserialport.cpp.

Here is the caller graph for this function:

void QextSerialPort::translateError ( ulong  error)

Translates a system-specific error code to a QextSerialPort error code. Used internally.

Definition at line 811 of file posix_qextserialport.cpp.

void QextSerialPort::ungetChar ( char  c)

This function is included to implement the full QIODevice interface, and currently has no purpose within this class. This function is meaningless on an unbuffered device and currently only prints a warning message to that effect.

Definition at line 802 of file posix_qextserialport.cpp.

qint64 QextSerialPort::writeData ( const char *  data,
qint64  maxSize 
) [protected]

Writes a block of data to the serial port. This function will write maxSize bytes from the buffer pointed to by data to the serial port. Return value is the number of bytes actually written, or -1 on error.

Warning:
before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).

Writes a block of data to the serial port. This function will write len bytes from the buffer pointed to by data to the serial port. Return value is the number of bytes actually written, or -1 on error.

Warning:
before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).

Definition at line 951 of file posix_qextserialport.cpp.


Member Data Documentation

Definition at line 277 of file qextserialport.h.

ulong QextSerialPort::lastErr [protected]

Definition at line 276 of file qextserialport.h.

QMutex* QextSerialPort::mutex [protected]

Definition at line 273 of file qextserialport.h.

QString QextSerialPort::port [protected]

Definition at line 274 of file qextserialport.h.

Definition at line 275 of file qextserialport.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines