The QextSerialPort class encapsulates a serial port on both POSIX and Windows systems. Plus de détails...
#include <qextserialport.h>
Types publics | |
enum | QueryMode { Polling, EventDriven } |
Connecteurs publics | |
void | setPortName (const QString &name) |
void | setQueryMode (QueryMode mode) |
void | setBaudRate (BaudRateType) |
void | setDataBits (DataBitsType) |
void | setParity (ParityType) |
void | setStopBits (StopBitsType) |
void | setFlowControl (FlowType) |
void | setTimeout (long) |
void | setDtr (bool set=true) |
void | setRts (bool set=true) |
Signaux | |
void | dsrChanged (bool status) |
Fonctions membres publiques | |
QextSerialPort (QueryMode mode=EventDriven, QObject *parent=0) | |
QextSerialPort (const QString &name, QueryMode mode=EventDriven, QObject *parent=0) | |
QextSerialPort (const PortSettings &s, QueryMode mode=EventDriven, QObject *parent=0) | |
QextSerialPort (const QString &name, const PortSettings &s, QueryMode mode=EventDriven, QObject *parent=0) | |
~QextSerialPort () | |
QString | portName () const |
QueryMode | queryMode () const |
BaudRateType | baudRate () const |
DataBitsType | dataBits () const |
ParityType | parity () const |
StopBitsType | stopBits () const |
FlowType | flowControl () const |
bool | open (OpenMode mode) |
bool | isSequential () const |
void | close () |
void | flush () |
qint64 | bytesAvailable () const |
bool | canReadLine () const |
QByteArray | readAll () |
ulong | lastError () const |
ulong | lineStatus () |
QString | errorString () |
Fonctions membres protégées | |
qint64 | readData (char *data, qint64 maxSize) |
qint64 | writeData (const char *data, qint64 maxSize) |
Propriétés | |
QString | portName |
QueryMode | queryMode |
Fonctions membres privées | |
Q_PRIVATE_SLOT (d_func(), void _q_canRead()) QextSerialPortPrivate *const d_ptr |
Description détaillée
Documentation des énumérations membres
This enum type specifies query mode used in a serial port:
Polling asynchronously read and write EventDriven synchronously read and write
{ Polling, EventDriven };
Documentation des constructeurs et destructeur
QextSerialPort::QextSerialPort | ( | QextSerialPort::QueryMode | mode = EventDriven , |
QObject * | parent = 0 |
||
) | [explicit] |
- Paramètres:
-
mode parent 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.
Références setPortName(), et setQueryMode().
: QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) { #ifdef Q_OS_WIN setPortName(QLatin1String("COM1")); #elif defined(Q_OS_IRIX) setPortName(QLatin1String("/dev/ttyf1")); #elif defined(Q_OS_HPUX) setPortName(QLatin1String("/dev/tty1p0")); #elif defined(Q_OS_SOLARIS) setPortName(QLatin1String("/dev/ttya")); #elif defined(Q_OS_OSF) //formally DIGITAL UNIX setPortName(QLatin1String("/dev/tty01")); #elif defined(Q_OS_FREEBSD) setPortName(QLatin1String("/dev/ttyd1")); #elif defined(Q_OS_OPENBSD) setPortName(QLatin1String("/dev/tty00")); #else setPortName(QLatin1String("/dev/ttyS0")); #endif setQueryMode(mode); }
QextSerialPort::QextSerialPort | ( | const QString & | name, |
QextSerialPort::QueryMode | mode = EventDriven , |
||
QObject * | parent = 0 |
||
) | [explicit] |
- Paramètres:
-
name mode parent 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
Références setPortName(), et setQueryMode().
: QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) { setQueryMode(mode); setPortName(name); }
QextSerialPort::QextSerialPort | ( | const PortSettings & | settings, |
QextSerialPort::QueryMode | mode = EventDriven , |
||
QObject * | parent = 0 |
||
) | [explicit] |
- Paramètres:
-
s mode parent Constructs a port with default name and specified settings.
Références setQueryMode().
: QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) { Q_D(QextSerialPort); setQueryMode(mode); d->setPortSettings(settings); }
QextSerialPort::QextSerialPort | ( | const QString & | name, |
const PortSettings & | settings, | ||
QextSerialPort::QueryMode | mode = EventDriven , |
||
QObject * | parent = 0 |
||
) |
- Paramètres:
-
name s mode parent Constructs a port with specified name , mode and settings.
Références setPortName(), et setQueryMode().
: QIODevice(parent), d_ptr(new QextSerialPortPrivate(this)) { Q_D(QextSerialPort); setPortName(name); setQueryMode(mode); d->setPortSettings(settings); }
Destructs the QextSerialPort object.
Références close().
{ if (isOpen()) { close(); } delete d_ptr; }
Documentation des fonctions membres
BaudRateType QextSerialPort::baudRate | ( | ) | const |
- Renvoie:
- BaudRateType
Returns the baud rate of the serial port. For a list of possible return values see the definition of the enum BaudRateType.
{
QReadLocker locker(&d_func()->lock);
return d_func()->settings.BaudRate;
}
qint64 QextSerialPort::bytesAvailable | ( | ) | const |
- Renvoie:
- qint64
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.
Référencé par Xbee::LireTrame(), et readAll().
{ QWriteLocker locker(&d_func()->lock); if (isOpen()) { qint64 bytes = d_func()->bytesAvailable_sys(); if (bytes != -1) { return bytes + d_func()->readBuffer.size() + QIODevice::bytesAvailable(); } else { return -1; } } return 0; }
bool QextSerialPort::canReadLine | ( | ) | const |
- Renvoie:
- bool
{ QReadLocker locker(&d_func()->lock); return QIODevice::canReadLine() || d_func()->readBuffer.canReadLine(); }
void QextSerialPort::close | ( | ) |
Closes a serial port. This function has no effect if the serial port associated with the class is not currently open.
Référencé par ~QextSerialPort(), et Xbee::~Xbee().
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (isOpen()) { // Be a good QIODevice and call QIODevice::close() before really close() // so the aboutToClose() signal is emitted at the proper time QIODevice::close(); // mark ourselves as closed d->close_sys(); d->readBuffer.clear(); } }
DataBitsType QextSerialPort::dataBits | ( | ) | const |
- Renvoie:
- DataBitsType
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.
{
QReadLocker locker(&d_func()->lock);
return d_func()->settings.DataBits;
}
void QextSerialPort::dsrChanged | ( | bool | status | ) | [signal] |
- Paramètres:
-
status 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 | ( | ) |
- Renvoie:
- QString
Returns a human-readable description of the last device error that occurred.
Références E_AGAIN, E_BREAK_CONDITION, E_BUFFER_OVERRUN, E_CAUGHT_NON_BLOCKED_SIGNAL, E_FILE_NOT_FOUND, E_FRAMING_ERROR, E_INVALID_DEVICE, E_INVALID_FD, E_IO_ERROR, E_NO_ERROR, E_NO_MEMORY, E_PERMISSION_DENIED, E_PORT_TIMEOUT, E_READ_FAILED, E_RECEIVE_OVERFLOW, E_RECEIVE_PARITY_ERROR, E_TRANSMIT_OVERFLOW, E_WRITE_FAILED, et portName().
{ Q_D(QextSerialPort); QReadLocker locker(&d->lock); switch(d->lastErr) { case E_NO_ERROR: return tr("No Error has occurred"); case E_INVALID_FD: return tr("Invalid file descriptor (port was not opened correctly)"); case E_NO_MEMORY: return tr("Unable to allocate memory tables (POSIX)"); case E_CAUGHT_NON_BLOCKED_SIGNAL: return tr("Caught a non-blocked signal (POSIX)"); case E_PORT_TIMEOUT: return tr("Operation timed out (POSIX)"); case E_INVALID_DEVICE: return tr("The file opened by the port is not a valid device"); case E_BREAK_CONDITION: return tr("The port detected a break condition"); case E_FRAMING_ERROR: return tr("The port detected a framing error (usually caused by incorrect baud rate settings)"); case E_IO_ERROR: return tr("There was an I/O error while communicating with the port"); case E_BUFFER_OVERRUN: return tr("Character buffer overrun"); case E_RECEIVE_OVERFLOW: return tr("Receive buffer overflow"); case E_RECEIVE_PARITY_ERROR: return tr("The port detected a parity error in the received data"); case E_TRANSMIT_OVERFLOW: return tr("Transmit buffer overflow"); case E_READ_FAILED: return tr("General read operation failure"); case E_WRITE_FAILED: return tr("General write operation failure"); case E_FILE_NOT_FOUND: return tr("The %1 file doesn't exists").arg(this->portName()); case E_PERMISSION_DENIED: return tr("Permission denied"); case E_AGAIN: return tr("Device is already locked"); default: return tr("Unknown error: %1").arg(d->lastErr); } }
FlowType QextSerialPort::flowControl | ( | ) | const |
- Renvoie:
- FlowType
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.
{
QReadLocker locker(&d_func()->lock);
return d_func()->settings.FlowControl;
}
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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (isOpen()) d->flush_sys(); }
bool QextSerialPort::isSequential | ( | ) | const |
- Renvoie:
- bool
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 true; }
ulong QextSerialPort::lastError | ( | ) | const |
- Renvoie:
- ulong
Return the error number, or 0 if no error occurred.
{
QReadLocker locker(&d_func()->lock);
return d_func()->lastErr;
}
unsigned long QextSerialPort::lineStatus | ( | ) |
- Renvoie:
- ulong
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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (isOpen()) return d->lineStatus_sys(); return 0; }
bool QextSerialPort::open | ( | OpenMode | mode | ) |
- Paramètres:
-
mode
- Renvoie:
- bool
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.
Référencé par Xbee::Xbee().
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (mode != QIODevice::NotOpen && !isOpen()) d->open_sys(mode); return isOpen(); }
ParityType QextSerialPort::parity | ( | ) | const |
- Renvoie:
- ParityType
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.
{
QReadLocker locker(&d_func()->lock);
return d_func()->settings.Parity;
}
QString QextSerialPort::portName | ( | ) | const |
- Renvoie:
- QString
Référencé par errorString().
QextSerialPort::Q_PRIVATE_SLOT | ( | d_func() | , |
void | _q_canRead() | ||
) | const [private] |
TODO
QueryMode QextSerialPort::queryMode | ( | ) | const |
- Renvoie:
- QueryMode
QByteArray QextSerialPort::readAll | ( | ) |
- Renvoie:
- QByteArray
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.
Références bytesAvailable().
Référencé par Xbee::LireTrame().
{ int avail = this->bytesAvailable(); return (avail > 0) ? this->read(avail) : QByteArray(); }
qint64 QextSerialPort::readData | ( | char * | data, |
qint64 | maxSize | ||
) | [protected] |
- Paramètres:
-
data maxSize
- Renvoie:
- qint64
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.
- Avertissement:
- before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); qint64 bytesFromBuffer = 0; if (!d->readBuffer.isEmpty()) { bytesFromBuffer = d->readBuffer.read(data, maxSize); if (bytesFromBuffer == maxSize) return bytesFromBuffer; } qint64 bytesFromDevice = d->readData_sys(data+bytesFromBuffer, maxSize-bytesFromBuffer); if (bytesFromDevice < 0) { return -1; } return bytesFromBuffer + bytesFromDevice; }
void QextSerialPort::setBaudRate | ( | BaudRateType | baudRate | ) | [slot] |
- Paramètres:
-
BaudRateType 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
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.BaudRate != baudRate) d->setBaudRate(baudRate, true); }
void QextSerialPort::setDataBits | ( | DataBitsType | dataBits | ) | [slot] |
- Paramètres:
-
DataBitsType Sets the number of data bits used by the serial port to dataBits. Possible values of dataBits are:
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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.DataBits != dataBits) d->setDataBits(dataBits, true); }
void QextSerialPort::setDtr | ( | bool | set = true | ) | [slot] |
- Paramètres:
-
set 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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (isOpen()) d->setDtr_sys(set); }
void QextSerialPort::setFlowControl | ( | FlowType | flow | ) | [slot] |
- Paramètres:
-
FlowType 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
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.FlowControl != flow) d->setFlowControl(flow, true); }
void QextSerialPort::setParity | ( | ParityType | parity | ) | [slot] |
- Paramètres:
-
ParityType Sets the parity associated with the serial port to parity. The possible values of parity are:
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.Parity != parity) d->setParity(parity, true); }
void QextSerialPort::setPortName | ( | const QString & | name | ) | [slot] |
- Paramètres:
-
name Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".
Référencé par QextSerialPort().
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); d->port = name; }
void QextSerialPort::setQueryMode | ( | QueryMode | mode | ) | [slot] |
- Paramètres:
-
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.
mode query mode.
Référencé par QextSerialPort().
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (mode != d->queryMode) { d->queryMode = mode; } }
void QextSerialPort::setRts | ( | bool | set = true | ) | [slot] |
- Paramètres:
-
set 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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (isOpen()) d->setRts_sys(set); }
void QextSerialPort::setStopBits | ( | StopBitsType | stopBits | ) | [slot] |
- Paramètres:
-
StopBitsType Sets the number of stop bits used by the serial port to stopBits. Possible values of stopBits are:
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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.StopBits != stopBits) d->setStopBits(stopBits, true); }
void QextSerialPort::setTimeout | ( | long | millisec | ) | [slot] |
- Paramètres:
-
long 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.
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); if (d->settings.Timeout_Millisec != millisec) d->setTimeout(millisec, true); }
StopBitsType QextSerialPort::stopBits | ( | ) | const |
- Renvoie:
- StopBitsType
Returns the number of stop bits used by the port. For a list of possible return values, see the definition of the enum StopBitsType.
{
QReadLocker locker(&d_func()->lock);
return d_func()->settings.StopBits;
}
qint64 QextSerialPort::writeData | ( | const char * | data, |
qint64 | maxSize | ||
) | [protected] |
- Paramètres:
-
data maxSize
- Renvoie:
- qint64
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.
- Avertissement:
- before calling this function ensure that serial port associated with this class is currently open (use isOpen() function to check if port is open).
{ Q_D(QextSerialPort); QWriteLocker locker(&d->lock); return d->writeData_sys(data, maxSize); }
Documentation des propriétés
QString QextSerialPort::portName [read, write] |
Returns the name set by setPortName().
QextSerialPort::QueryMode QextSerialPort::queryMode [read, write] |
Get query mode.
La documentation de cette classe a été générée à partir des fichiers suivants :