C++ (Qt) void exception(); void ctsChanged(bool value); void dsrChanged(bool value); void ringChanged(bool value);
C++ (Qt)enum BaudRate {/* Baud75 = 75, Baud110 = 110, Baud134 = 134, Baud150 = 150, Baud300 = 300, Baud600 = 600, */ Baud1200 = 1200, Baud1800 = 1800, Baud2400 = 2400, Baud4800 = 4800, Baud9600 = 9600, Baud19200 = 19200, Baud38400 = 38400, Baud57600 = 57600, Baud115200 = 115200};
C++ (Qt) #ifndef SERIALDEVICE_H#define SERIALDEVICE_H #include <QtCore/QIODevice> #ifndef QT_NO_DEBUG_STREAM#include <QtCore/QDebug>#endif #include "../qserialdevice_global.h" #ifndef SERIALDEVICE_BUFFERSIZE#define SERIALDEVICE_BUFFERSIZE Q_INT64_C(16384)#endif class SerialDevicePrivate;#if defined(QSERIALDEVICE_EXPORT)class QSERIALDEVICE_EXPORT SerialDevice : public QIODevice#elseclass SerialDevice : public QIODevice#endif{ Q_OBJECT Q_SIGNALS: void ctsChanged(bool value); void dsrChanged(bool value); void ringChanged(bool value); void error(SerialDevice::SerialError); public: /*! \~english \enum StreamDirectionFlag Directions baud rate that supports the class SerialDevice. */ enum StreamDirectionFlag { Input = 1, /*!< \~english Input baud rate. */ Output = 2 /*!< \~english Output baud rate. */ }; Q_DECLARE_FLAGS(StreamDirection, StreamDirectionFlag) /*! \~english \enum BaudRate Standard types of speed serial device that supports the class SerialDevice. */ enum BaudRate { Baud9600 = 9600, /*!< \~english Speed 9600 bauds. */ Baud19200 = 19200, /*!< \~english Speed 19200 bauds. */ Baud38400 = 38400, /*!< \~english Speed 38400 bauds. */ Baud57600 = 57600, /*!< \~english Speed 57600 bauds. */ Baud115200 = 115200, /*!< \~english Speed 115200 bauds. */ UnknownBaudRate = -1 /*!< \~english Unknown speed. */ }; /*! \~english \enum DataBits Standard types of data bits serial device that supports the class SerialDevice. */ enum DataBits { Data5 = 5, /*!< \~english 5 data bits. */ Data6 = 6, /*!< \~english 6 data bits. */ Data7 = 7, /*!< \~english 7 data bits. */ Data8 = 8, /*!< \~english 8 data bits. */ UnknownDataBits = -1 /*!< \~english Unknown data bits. */ }; /*! \~english \enum Parity Standard types of parity serial device that supports the class SerialDevice. */ enum Parity { None = 0, /*!< \~english No parity. */ Odd = 2, /*!< \~english Odd parity. */ Even = 3, /*!< \~english Even parity. */ Space = 4, /*!< \~english Space parity. */ Mark = 5, /*!< \~english Mark parity. */ UnknownParity = -1 /*!< \~english Parity unknown. */ }; /*! \~english \enum StopBits Standard types of stop bits serial device that supports the class SerialDevice. */ enum StopBits { One = 1, /*!< \~english One stop bit. */ Half = 3, /*!< \~english Half stop bits. */ Two = 2, /*!< \~english Two stop bits. */ UnknownStopBits = -1 /*!< \~english Unknown stop bits number. */ }; /*! \~english \enum Flow Standard types of flow control serial device that supports the class SerialDevice. */ enum Flow { Off, /*!< \~english Flow control "Off". */ Hardware, /*!< \~english Flow control "Hardware". */ Software, /*!< \~english Flow control "Xon/Xoff". */ UnknownFlow = -1 /*!< \~english Flow control unknown. */ }; /*! \~english \enum LineFlag Flags of states of the lines: CTS, DSR, DCD, RI, RTS, DTR, ST, SR interface serial device (see RS-232 standard, etc.).\n To determine the state of the desired line is necessary to impose a mask "and" of the flag line at the result of the method: quint16 SerialDevice::lines(). */ enum LineFlag { Le = 0x01, /*!< \~english Line DSR (data set ready/line enable). */ Dtr = 0x02, /*!< \~english Line DTR (data terminal ready). */ Rts = 0x04, /*!< \~english Line RTS (request to send). */ St = 0x08, /*!< \~english Line Secondary TXD (transmit). */ Sr = 0x10, /*!< \~english Line Secondary RXD (receive.) */ Cts = 0x20, /*!< \~english Line CTS (clear to send). */ Dcd = 0x40, /*!< \~english Line DCD (data carrier detect). */ Ri = 0x80, /*!< \~english Line RNG (ring). */ Dsr = Le /*!< \~english Line DSR (data set ready). */ }; Q_DECLARE_FLAGS(Line, LineFlag) /*! \~english \enum BrokenDataControl */ enum BrokenDataControl { Skip, PassZero, Ignore, BreakRecv }; /*! \~english \enum SerialError */ enum SerialError { /* Что тут писать? */ }; explicit SerialDevice(QObject *parent = 0); virtual ~SerialDevice(); void setDeviceName(const QString &deviceName); QString deviceName() const; bool open(OpenMode mode); void close(); //baud rate bool setBaudRate(qint32 baud, StreamDirection dir = Input | Output); qint32 baudRate(StreamDirection dir = Input | Output) const; //data bits bool setDataBits(DataBits dataBits); DataBits dataBits() const; //parity bool setParity(Parity parity); Parity parity() const; //stop bits bool setStopBits(StopBits stopBits); StopBits stopBits() const; //flow bool setFlowControl(Flow flow); Flow flowControl() const; // Timeouts void setCharIntervalTimeout(int usecs = 0); int charIntervalTimeout() const; void setTotalReadConstantTimeout(int msecs = 0); int totalReadConstantTimeout() const; //Lines statuses bool setDtr(bool set); bool setRts(bool set); quint16 lines(); //Break bool sendBreak(int duration); bool setBreak(bool set); // bool flush(); bool reset(); //+++ void setBrokenDataControl(BrokenDataControl control = Ignore); SerialError error() const; bool isSequential() const; qint64 bytesAvailable() const; qint64 bytesToWrite() const; bool waitForReadyRead(int msecs); bool waitForBytesWritten(int msecs); qint64 readBufferSize() const; void setReadBufferSize(qint64 size); protected: qint64 readData(char *data, qint64 maxSize); qint64 writeData(const char *data, qint64 maxSize); void setSerialError(SerialError serialError); SerialDevicePrivate * const d_ptr; private: Q_DECLARE_PRIVATE(SerialDevice) Q_DISABLE_COPY(SerialDevice)}; Q_DECLARE_OPERATORS_FOR_FLAGS(SerialDevice::StreamDirection)
void ctsChanged(bool value); void dsrChanged(bool value); void ringChanged(bool value);
C++ (Qt)/* <license header> */ #ifndef SERIALPORT_H#define SERIALPORT_H #include <QtCore/qiodevice.h> #ifndef SERIALPORT_EXPORT#define SERIALPORT_EXPORT#endif class SerialPortPrivate; class SERIALPORT_EXPORT SerialPort : public QIODevice{ Q_OBJECT Q_PROPERTY(BaudRate baudRate READ baudRate WRITE setBaudRate) Q_PROPERTY(DataBits dataBits READ dataBits WRITE setDataBits) Q_PROPERTY(Parity parity READ parity WRITE setParity) Q_PROPERTY(StopBits stopBits READ stopBits WRITE setStopBits) Q_PROPERTY(FlowControl flowControl READ flowControl WRITE setFlowControl) Q_PROPERTY(DataErrorPolicy dataErrorPolicy READ dataErrorPolicy WRITE setDataErrorPolicy) Q_PROPERTY(bool dtr READ dtr WRITE setDtr) Q_PROPERTY(bool rts READ rts WRITE setRts) Q_PROPERTY(SerialError error READ error RESET unsetError) Q_ENUMS( Direction BaudRate DataBits Parity StopBits FlowControl Line DataErrorPolicy SerialError ) public: /*! \~english \enum DirectionFlag Directions baud rate that supports the class SerialPort. */ enum Direction { Input = 1, /*!< \~english Input baud rate. */ Output = 2 /*!< \~english Output baud rate. */ }; Q_DECLARE_FLAGS(Directions, Direction) /*! \~english \enum BaudRate Standard types of speed serial device that supports the class SerialPort. */ enum BaudRate { Baud1200 = 1200, /*!< \~english Speed 1200 bauds. */ Baud1800 = 1800, /*!< \~english Speed 1800 bauds. */ Baud2400 = 2400, /*!< \~english Speed 2400 bauds. */ Baud4800 = 4800, /*!< \~english Speed 4800 bauds. */ Baud9600 = 9600, /*!< \~english Speed 9600 bauds. */ Baud19200 = 19200, /*!< \~english Speed 19200 bauds. */ Baud38400 = 38400, /*!< \~english Speed 38400 bauds. */ Baud57600 = 57600, /*!< \~english Speed 57600 bauds. */ Baud115200 = 115200, /*!< \~english Speed 115200 bauds. */ UnknownBaudRate = -1 /*!< \~english Unknown speed. */ }; /*! \~english \enum DataBits Standard types of data bits serial device that supports the class SerialPort. */ enum DataBits { Data5 = 5, /*!< \~english 5 data bits. */ Data6 = 6, /*!< \~english 6 data bits. */ Data7 = 7, /*!< \~english 7 data bits. */ Data8 = 8, /*!< \~english 8 data bits. */ UnknownDataBits = -1 /*!< \~english Unknown data bits. */ }; /*! \~english \enum Parity Standard types of parity serial device that supports the class SerialPort. */ enum Parity { NoParity = 0, /*!< \~english No parity. */ Even = 2, /*!< \~english Even parity. */ Odd = 3, /*!< \~english Odd parity. */ Space = 4, /*!< \~english Space parity. */ Mark = 5, /*!< \~english Mark parity. */ UnknownParity = -1 /*!< \~english Parity unknown. */ }; /*! \~english \enum StopBits Standard types of stop bits serial device that supports the class SerialPort. */ enum StopBits { One = 2, /*!< \~english One stop bit. */ OneAndHalf = 3, /*!< \~english One and half stop bits. */ Two = 4, /*!< \~english Two stop bits. */ UnknownStopBits = -1 /*!< \~english Unknown stop bits number. */ }; /*! \~english \enum FlowControl Standard types of flow control serial device that supports the class SerialPort. */ enum FlowControl { NoFlowControl, /*!< \~english Flow control "Off". */ Hardware, /*!< \~english Flow control "Hardware". */ Software, /*!< \~english Flow control "Xon/Xoff". */ UnknownFlow = -1 /*!< \~english Flow control unknown. */ }; /*! \~english \enum LineFlag Flags of states of the lines: CTS, DSR, DCD, RI, RTS, DTR, ST, SR interface serial device (see RS-232 standard, etc.).\n To determine the state of the desired line is necessary to impose a mask "and" of the flag line at the result of the method: quint16 SerialPort::lines(). */ enum Line { Le = 0x01, /*!< \~english Line DSR (data set ready/line enable). */ Dtr = 0x02, /*!< \~english Line DTR (data terminal ready). */ Rts = 0x04, /*!< \~english Line RTS (request to send). */ St = 0x08, /*!< \~english Line Secondary TXD (transmit). */ Sr = 0x10, /*!< \~english Line Secondary RXD (receive.) */ Cts = 0x20, /*!< \~english Line CTS (clear to send). */ Dcd = 0x40, /*!< \~english Line DCD (data carrier detect). */ Ri = 0x80, /*!< \~english Line RNG (ring). */ Dsr = Le /*!< \~english Line DSR (data set ready). */ }; Q_DECLARE_FLAGS(Lines, LineFlag) /*! \~english \enum DataErrorPolicy */ enum DataErrorPolicy { Skip, PassZero, Ignore, StopReceiving }; /*! \~english \enum SerialError */ enum SerialError { NoError, /*!< \~english Last operation successfully finished */ NoSuchDevice, /*!< \~english Port open failed due to device absence in the system */ PermissionDenied, /*!< \~english Port open failed due to insufficient rights */ DeviceAlreadyOpened, /*!< \~english Port open failed due to previously opened */ DeviceIsNotOpened, /*!< \~english Port is not opened before processing operation */ ParityError, /*!< \~english Last read operation stops due to parity error */ IoError /*!< \~english Other error */ }; explicit SerialPort(QObject *parent = 0); explicit SerialPort(const QString &deviceName, QObject *parent = 0); virtual ~SerialPort(); void setDeviceName(const QString &deviceName); QString deviceName() const; virtual bool open(OpenMode mode); virtual void close(); bool setup(qint32 bauds, Parity parity = NoParity, StopBits stopBits = One, FlowControl flowControl = NoFlowControl, DataErrorPolicy policy = Ignore); SerialError error() const; void unsetError(); bool setBaudRate(qint32 bauds, Directions dir = Input | Output); qint32 baudRate(Directions dir = Input | Output) const; bool setDataBits(DataBits dataBits); DataBits dataBits() const; bool setParity(Parity parity); Parity parity() const; bool setStopBits(StopBits stopBits); StopBits stopBits() const; bool setFlowControl(FlowControl flow); FlowControl flowControl() const; void setDataInterval(int usecs); int dataInterval() const; void setReadTimeout(int msecs); //-1 - infinity, 0 - only buffered, ... int readTimeout() const; bool dtr() const; bool rts() const; Lines lines() const; bool flush(Directions dir = Input | Output); virtual bool reset(); void setDataErrorPolicy(DataErrorPolicy policy); DataErrorPolicy dataErrorPolicy() const; virtual bool isSequential() const; virtual qint64 bytesAvailable() const; virtual qint64 bytesToWrite() const; virtual bool waitForReadyRead(int msecs); virtual bool waitForBytesWritten(int msecs); public Q_SLOTS: bool setDtr(bool set); bool setRts(bool set); bool sendBreak(int duration = 0); bool setBreak(bool set = true); bool clearBreak(bool clear = true); protected: virtual qint64 readData(char *data, qint64 maxSize); virtual qint64 writeData(const char *data, qint64 maxSize); private: const SerialPortPrivate *d_ptr; Q_DECLARE_PRIVATE(SerialPort) Q_DISABLE_COPY(SerialPort)}; inline bool SerialPort::clearBreak(bool clear){ return setBreak(!clear); } Q_DECLARE_OPERATORS_FOR_FLAGS(SerialPort::Directions)Q_DECLARE_OPERATORS_FOR_FLAGS(SerialPort::Lines) #endif // SERIALPORT_H
C++ (Qt)bool SerialPort::open(OpenMode mode){ if ("baud rate" is not set) { setBaudRate(Baud9600); //set default baud rate } if ("parity" is not set) { setParity(NoParity); //set default parity } if ("stop bits" is not set) { setStopBits(One); //set default stop bits } ..... //open serial port here}
C++ (Qt)SerialPort port("foo");port.setBaudRate(SerialPort::Baud9600); if (!port.open(_mode_)) { // show error message return;}
C++ (Qt)bool open(OpenMode mode, qint32 bauds);
C++ (Qt)bool SerialPort::open(OpenMode mode, qint32 bauds){ setBaudRate(bauds); return open(mode);}
C++ (Qt)SerialPort port("foo");port.setBaudRate(SerialPort::Baud9600);port.setDataBits(SerialPort::Data7); if (!port.open(_mode_)) { // show error message return;}