Home · All Classes ·
  • Modules
  • QextSerialPort
  • Contents

    QextSerialPort Class Reference

    The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems. More...

        #include <QextSerialPort>

    Inherits: QIODevice.

    Public Types

    enum QueryMode { Polling, EventDriven }

    Public Functions

    QextSerialPort ( QueryMode mode = EventDriven, QObject * parent = 0 )
    QextSerialPort ( const QString & name, QueryMode mode = EventDriven, QObject * parent = 0 )
    QextSerialPort ( const PortSettings & settings, QueryMode mode = EventDriven, QObject * parent = 0 )
    QextSerialPort ( const QString & name, const PortSettings & settings, QueryMode mode = EventDriven, QObject * parent = 0 )
    ~QextSerialPort ()
    BaudRateType baudRate () const
    DataBitsType dataBits () const
    QString errorString ()
    FlowType flowControl () const
    void flush ()
    ulong lastError () const
    ulong lineStatus ()
    bool open ( OpenMode mode )
    ParityType parity () const
    QString portName () const
    QueryMode queryMode () const
    QByteArray readAll ()
    StopBitsType stopBits () const

    Reimplemented Public Functions

    virtual qint64 bytesAvailable () const
    virtual bool canReadLine () const
    virtual void close ()
    virtual bool isSequential () const

    Public Slots

    void setBaudRate ( BaudRateType baudRate )
    void setDataBits ( DataBitsType dataBits )
    void setDtr ( bool set = true )
    void setFlowControl ( FlowType flow )
    void setParity ( ParityType parity )
    void setPortName ( const QString & name )
    void setQueryMode ( QueryMode mode )
    void setRts ( bool set = true )
    void setStopBits ( StopBitsType stopBits )
    void setTimeout ( long millisec )

    Signals

    void dsrChanged ( bool status )

    Reimplemented Protected Functions

    virtual qint64 readData ( char * data, qint64 maxSize )
    virtual qint64 writeData ( const char * data, qint64 maxSize )

    Additional Inherited Members

    Detailed Description

    The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems.

    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");
        connect(port, SIGNAL(readyRead()), myClass, SLOT(onDataAvailable()));
        port->open();
    
        void MyClass::onDataAvailable()
        {
            QByteArray data = port->readAll();
            processNewData(usbdata);
        }

    Compatibility

    The user will be notified of errors and possible portability conflicts at run-time by default.

    For example, if a application has used BAUD1800, when it is runing under unix, you will get following message.

        QextSerialPort Portability Warning: Windows does not support baudRate:1800

    This behavior can be turned off by defining macro QESP_NO_WARN (to turn off all warnings) or QESP_NO_PORTABILITY_WARN (to turn off portability warnings) in the project.

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

    Member Type Documentation

    enum QextSerialPort::QueryMode

    This enum type specifies query mode used in a serial port:

    ConstantValueDescription
    QextSerialPort::Polling0asynchronously read and write
    QextSerialPort::EventDriven1synchronously read and write

    Member Function Documentation

    QextSerialPort::QextSerialPort ( QueryMode mode = EventDriven, QObject * parent = 0 )

    Default constructor. Note that the name of the device used by a QextSerialPort is dependent on your OS. Possible naming conventions and their associated OS are:

        OS Constant       Used By         Naming Convention
        -------------     -------------   ------------------------
        Q_OS_WIN          Windows         COM1, COM2
        Q_OS_IRIX         SGI/IRIX        /dev/ttyf1, /dev/ttyf2
        Q_OS_HPUX         HP-UX           /dev/tty1p0, /dev/tty2p0
        Q_OS_SOLARIS      SunOS/Slaris    /dev/ttya, /dev/ttyb
        Q_OS_OSF          Digital UNIX    /dev/tty01, /dev/tty02
        Q_OS_FREEBSD      FreeBSD         /dev/ttyd0, /dev/ttyd1
        Q_OS_OPENBSD      OpenBSD         /dev/tty00, /dev/tty01
        Q_OS_LINUX        Linux           /dev/ttyS0, /dev/ttyS1
        <none>                            /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. Default mode is EventDriven. As a subclass of QObject, parent can be specified.

    QextSerialPort::QextSerialPort ( const QString & name, QueryMode mode = EventDriven, QObject * parent = 0 )

    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". mode

    QextSerialPort::QextSerialPort ( const PortSettings & settings, QueryMode mode = EventDriven, QObject * parent = 0 )

    Constructs a port with default name and specified settings.

    QextSerialPort::QextSerialPort ( const QString & name, const PortSettings & settings, QueryMode mode = EventDriven, QObject * parent = 0 )

    Constructs a port with specified name , mode and settings.

    QextSerialPort::~QextSerialPort ()

    Destructs the QextSerialPort object.

    BaudRateType QextSerialPort::baudRate () const

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

    See also setBaudRate().

    qint64 QextSerialPort::bytesAvailable () const [virtual]

    Reimplemented from QIODevice::bytesAvailable().

    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.

    bool QextSerialPort::canReadLine () const [virtual]

    Reimplemented from QIODevice::canReadLine().

    void QextSerialPort::close () [virtual]

    Reimplemented from QIODevice::close().

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

    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.

    See also setDataBits().

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

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

    status true when DSR signal is on, false otherwise.

    QString QextSerialPort::errorString ()

    Returns a human-readable description of the last device error that occurred.

    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.

    See also setFlowControl().

    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.

    bool QextSerialPort::isSequential () const [virtual]

    Reimplemented from QIODevice::isSequential().

    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.

    ulong QextSerialPort::lastError () const

    Return the error number, or 0 if no error occurred.

    ulong QextSerialPort::lineStatus ()

    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.

    bool QextSerialPort::open ( OpenMode mode )

    Opens a serial port and sets its OpenMode to mode. Note that this function does not specify which device to open. Returns true if successful; otherwise returns false.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.

    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.

    See also setParity().

    QString QextSerialPort::portName () const

    Returns the name set by setPortName().

    See also setPortName().

    QueryMode QextSerialPort::queryMode () const

    Get query mode.

    See also setQueryMode().

    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.

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

    Reimplemented from QIODevice::readData().

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

    void QextSerialPort::setBaudRate ( BaudRateType baudRate ) [slot]

    Sets the baud rate of the serial port to baudRate. 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                   X          50
         BAUD75                   X          75
        *BAUD110                110         110
         BAUD134                  X         134.5
         BAUD150                  X         150
         BAUD200                  X         200
        *BAUD300                300         300
        *BAUD600                600         600
        *BAUD1200              1200        1200
         BAUD1800                 X        1800
        *BAUD2400              2400        2400
        *BAUD4800              4800        4800
        *BAUD9600              9600        9600
         BAUD14400            14400           X
        *BAUD19200            19200       19200
        *BAUD38400            38400       38400
         BAUD56000            56000           X
        *BAUD57600            57600       57600
         BAUD76800                X       76800
        *BAUD115200          115200      115200
         BAUD128000          128000           X
         BAUD230400               X      230400
         BAUD256000          256000           X
         BAUD460800               X      460800
         BAUD500000               X      500000
         BAUD576000               X      576000
         BAUD921600               X      921600
         BAUD1000000              X     1000000
         BAUD1152000              X     1152000
         BAUD1500000              X     1500000
         BAUD2000000              X     2000000
         BAUD2500000              X     2500000
         BAUD3000000              X     3000000
         BAUD3500000              X     3500000
         BAUD4000000              X     4000000

    See also baudRate().

    void QextSerialPort::setDataBits ( DataBitsType dataBits ) [slot]

    Sets the number of data bits used by the serial port to dataBits. 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:

    See also dataBits().

    void QextSerialPort::setDtr ( bool set = true ) [slot]

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

    void QextSerialPort::setFlowControl ( FlowType flow ) [slot]

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

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

    See also flowControl().

    void QextSerialPort::setParity ( ParityType parity ) [slot]

    Sets the parity associated with the serial port to parity. 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

    See also parity().

    void QextSerialPort::setPortName ( const QString & name ) [slot]

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

    See also portName().

    void QextSerialPort::setQueryMode ( QueryMode mode ) [slot]

    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.

    mode query mode.

    See also queryMode().

    void QextSerialPort::setRts ( bool set = true ) [slot]

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

    void QextSerialPort::setStopBits ( StopBitsType stopBits ) [slot]

    Sets the number of stop bits used by the serial port to stopBits. 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:

    See also stopBits().

    void QextSerialPort::setTimeout ( long millisec ) [slot]

    For Unix:

    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.

    For Windows:

    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.

    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.

    See also setStopBits().

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

    Reimplemented from QIODevice::writeData().

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


    Copyright © 2000-2012 QextSerialPort Project
    QextSerialPort Manual