C++ (Qt)setup(baud115200); //115200n1setup(baud57600, SerialDevice::Even); //57600e1setup(baud9600, SerialDevice::Mark, SerialDevice::Two); //9600m2
C++ (Qt)SerialDevice *port = new SerialDevice("COM1");if (!port->open(QIODevice::ReadWrite)) {}port->setup(baud9600, SerialDevice::Odd);//...
C++ (Qt)/* License...*/ #ifndef SERIALDEVICE_H#define SERIALDEVICE_H #include <QtCore/QIODevice> class SerialDevicePrivate; class QSERIALDEVICE_EXPORT SerialDevice : 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( Directions BaudRate DataBits Parity StopBits FlowControl Lines DataErrorPolicy SerialError ) public: enum Direction { Input = 1, Output = 2 }; Q_DECLARE_FLAGS(Directions, Direction) enum BaudRate { Baud1200 = 1200, Baud2400 = 2400, Baud4800 = 4800, Baud9600 = 9600, Baud19200 = 19200, Baud38400 = 38400, Baud57600 = 57600, Baud115200 = 115200, UnknownBaudRate = -1 }; enum DataBits { Data5 = 5, Data6 = 6, Data7 = 7, Data8 = 8, UnknownDataBits = -1 }; enum Parity { NoParity = 0, EvenParity = 2, OddParity = 3, SpaceParity = 4, MarkParity = 5, UnknownParity = -1 }; enum StopBits { OneStop = 1, OneAndHalfStop = 3, TwoStop = 2, UnknownStopBits = -1 }; enum FlowControl { NoControl, HardwareControl, SoftwareControl, UnknownFlowControl = -1 }; enum Line { Le = 0x01, Dtr = 0x02, Rts = 0x04, St = 0x08, Sr = 0x10, Cts = 0x20, Dcd = 0x40, Ri = 0x80, Dsr = Le }; Q_DECLARE_FLAGS(Lines, Line) enum DataErrorPolicy { SkipPolicy, PassZeroPolicy, IgnorePolicy, StopReceivingPolicy }; enum SerialError { NoError, NoSuchDeviceError, PermissionDeniedError, DeviceAlreadyOpenedError, DeviceIsNotOpenedError, ParityError, IoError }; explicit SerialDevice(QObject *parent = 0); explicit SerialDevice(const QString &deviceName, QObject *parent = 0); virtual ~SerialDevice(); void setDeviceName(const QString &deviceName); QString deviceName() const; virtual bool open(OpenMode mode); virtual void close(); bool setBaudRate(qint32 baud, 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 = 0); int dataInterval() const; void setReadTimeout(int msecs = 0); int readTimeout() const; bool dtr() const; bool rts() const; Lines lines() const; bool flush(); virtual bool reset(); void setDataErrorPolicy(DataErrorPolicy policy = IgnorePolicy); DataErrorPolicy dataErrorPolicy() const; SerialError error() const; void unsetError(); 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: SerialDevicePrivate * const d_ptr; Q_DECLARE_PRIVATE(SerialDevice) Q_DISABLE_COPY(SerialDevice)}; inline bool SerialDevice::clearBreak(bool clear){ return setBreak(!clear); } Q_DECLARE_OPERATORS_FOR_FLAGS(SerialDevice::Directions)Q_DECLARE_OPERATORS_FOR_FLAGS(SerialPort::Lines) #endif // SERIALDEVICE_H
C++ (Qt)class SerialDevice;class SerialDeviceInfoPrivate; class SerialDeviceInfo{public: SerialDeviceInfo(); //produces NULL info SerialDeviceInfo(const SerialDeviceInfo &other); //copy constructor explicit SerialDeviceInfo(const SerialDevice &device); SerialDeviceInfo& operator=(const SerialDeviceInfo &other); void swap(SerialDeviceInfo &other); QString deviceName() const; //OS depended (/dev/ttyS0) QString name() const; //frendly name like COM1, ttyS0... bool isNull() const; bool isBusy() const; QList<int> standardBauds() const; static QList<SerialDeviceInfo> availableDevices();private: Q_DECLARE_PRIVATE(SerialDeviceInfo) SerialDeviceInfoPrivate *d_ptr;};
C++ (Qt).... QString deviceName() const; //OS depended (/dev/ttyS0) QString shortName() const; //Обрезаное имя, COM1, ttyUSB0 и т.п. +++ QString friendlyName() const; //+++ QString description() const; //+++ QString manufacturer() const; //+++ bool isNull() const; //Что сие значит? bool isBusy() const; //Насчет этого метода не уверен, т.к. тормозит в винде. ....
C++ (Qt)QSerialDevice *port = new QSerialDevice(); setBodeRate(...);setParity(...);// и так далее // где-то в кодеif (!port->open()) ERROR// пользуемся port->close(); // где-то опять в кодеif (!port->open()) ERROR// пользуемся port->close();