Home · All Classes ·
  • Examples
  • UartAssistant Demo
  • UartAssistant Demo

    Files:

    The example demonstrates how to use QextSerialPort.

    Initialze UI element.

        foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
            ui->portBox->addItem(info.portName);
        //make sure user can input their own port name!
        ui->portBox->setEditable(true);
    
        ui->baudRateBox->addItem("1200", BAUD1200);
        ui->baudRateBox->addItem("2400", BAUD2400);
        ui->baudRateBox->addItem("4800", BAUD4800);
        ui->baudRateBox->addItem("9600", BAUD9600);
        ui->baudRateBox->addItem("19200", BAUD19200);
        ui->baudRateBox->setCurrentIndex(3);
    
        ui->parityBox->addItem("NONE", PAR_NONE);
        ui->parityBox->addItem("ODD", PAR_ODD);
        ui->parityBox->addItem("EVEN", PAR_EVEN);
    
        ui->dataBitsBox->addItem("5", DATA_5);
        ui->dataBitsBox->addItem("6", DATA_6);
        ui->dataBitsBox->addItem("7", DATA_7);
        ui->dataBitsBox->addItem("8", DATA_8);
        ui->dataBitsBox->setCurrentIndex(3);
    
        ui->stopBitsBox->addItem("1", STOP_1);
        ui->stopBitsBox->addItem("2", STOP_2);
    
        ui->queryModeBox->addItem("Polling", QextSerialPort::Polling);
        ui->queryModeBox->addItem("EventDriven", QextSerialPort::EventDriven);

    Initialize serial port

        PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
        port = new QextSerialPort(ui->portBox->currentText(), settings, QextSerialPort::Polling);

    port Settings

        void Dialog::onBaudRateChanged(int idx)
        {
            port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
        }
    
        void Dialog::onParityChanged(int idx)
        {
            port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
        }
    
        void Dialog::onDataBitsChanged(int idx)
        {
            port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
        }
    
        void Dialog::onStopBitsChanged(int idx)
        {
            port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
        }
    
        void Dialog::onQueryModeChanged(int idx)
        {
            port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
        }
    
        void Dialog::onTimeoutChanged(int val)
        {
            port->setTimeout(val);
        }

    Open or Close the port.

        void Dialog::onOpenCloseButtonClicked()
        {
            if (!port->isOpen()) {
                port->setPortName(ui->portBox->currentText());
                port->open(QIODevice::ReadWrite);
            }
            else {
                port->close();
            }
    
            //If using polling mode, we need a QTimer
            if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
                timer->start();
            else
                timer->stop();
    
            //update led's status
            ui->led->turnOn(port->isOpen());
        }

    Read from or Write to the port

        void Dialog::onSendButtonClicked()
        {
            if (port->isOpen() && !ui->sendEdit->toPlainText().isEmpty())
                port->write(ui->sendEdit->toPlainText().toLatin1());
        }
    
        void Dialog::onReadyRead()
        {
            if (port->bytesAvailable()) {
                ui->recvEdit->moveCursor(QTextCursor::End);
                ui->recvEdit->insertPlainText(QString::fromLatin1(port->readAll()));
            }
        }
    
        void Dialog::onPortAddedOrRemoved()
        {
            QString current = ui->portBox->currentText();
    
            ui->portBox->blockSignals(true);
            ui->portBox->clear();
            foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
                ui->portBox->addItem(info.portName);
    
            ui->portBox->setCurrentIndex(ui->portBox->findText(current));
    
            ui->portBox->blockSignals(false);
        }


    Copyright © 2000-2012 QextSerialPort Project
    QextSerialPort Manual