Home · All Classes · |
The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems. More...
#include <QextSerialPort>
Inherits: QIODevice.
enum | QueryMode { Polling, EventDriven } |
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 |
virtual qint64 | bytesAvailable () const |
virtual bool | canReadLine () const |
virtual void | close () |
virtual bool | isSequential () const |
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 ) |
void | dsrChanged ( bool status ) |
virtual qint64 | readData ( char * data, qint64 maxSize ) |
virtual qint64 | writeData ( const char * data, qint64 maxSize ) |
The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems.
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); }
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
This enum type specifies query mode used in a serial port:
Constant | Value | Description |
---|---|---|
QextSerialPort::Polling | 0 | asynchronously read and write |
QextSerialPort::EventDriven | 1 | synchronously read and write |
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.
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
Constructs a port with default name and specified settings.
Constructs a port with specified name , mode and settings.
Destructs the QextSerialPort object.
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().
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.
Reimplemented from QIODevice::canReadLine().
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.
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().
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.
Returns a human-readable description of the last device error that occurred.
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().
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.
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.
Return the error number, or 0 if no error occurred.
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.
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.
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().
Returns the name set by setPortName().
See also setPortName().
Get query mode.
See also setQueryMode().
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.
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).
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().
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().
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.
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().
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().
Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".
See also portName().
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().
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.
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().
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.
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().
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 |