Changeset - 4c9e48a45ddf
[Not reviewed]
default
0 1 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-07-26 15:12:41
hy@ozderya.net
show device information in port name list (such as USB devices)
1 file changed with 16 insertions and 2 deletions:
0 comments (0 inline, 0 general)
portcontrol.cpp
Show inline comments
 
@@ -29,24 +29,26 @@ PortControl::PortControl(QSerialPort* po
 
    ui(new Ui::PortControl)
 
{
 
    ui->setupUi(this);
 

	
 
    serialPort = port;
 

	
 
    QObject::connect(ui->pbReloadPorts, &QPushButton::clicked,
 
                     this, &PortControl::loadPortList);
 

	
 
    QObject::connect(ui->pbOpenPort, &QPushButton::clicked,
 
                     this, &PortControl::togglePort);
 

	
 
    // TODO: port name coming from combobox is dirty, create a separate layer of signals
 
    //       that will sanitize this information
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectPort);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::onPortNameChanged);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectBaudRate);
 

	
 
@@ -103,25 +105,33 @@ PortControl::~PortControl()
 
    delete ui;
 
}
 

	
 
void PortControl::loadPortList()
 
{
 
    QString currentSelection = ui->cbPortList->currentText();
 

	
 
    ui->cbPortList->clear();
 

	
 
    discoveredPorts.clear();
 
    for (auto port : QSerialPortInfo::availablePorts())
 
    {
 
        ui->cbPortList->addItem(port.portName());
 
        QString pName = port.portName();
 
        if (!port.description().isEmpty()) pName += QString(" ") + port.description();
 
        if (port.hasProductIdentifier())
 
        {
 
            QString vID = QString("%1").arg(port.vendorIdentifier(), 4, 16, QChar('0'));
 
            QString pID = QString("%1").arg(port.productIdentifier(), 4, 16, QChar('0'));
 
            pName = pName + " [" + vID + ":" + pID + "]";
 
        }
 
        ui->cbPortList->addItem(pName);
 
        discoveredPorts << port.portName();
 
    }
 

	
 
    ui->cbPortList->addItems(userEnteredPorts);
 

	
 
    // find current selection in the new list, maybe it doesn't exist anymore?
 
    int currentSelectionIndex = ui->cbPortList->findText(currentSelection);
 
    if (currentSelectionIndex >= 0)
 
    {
 
        ui->cbPortList->setCurrentIndex(currentSelectionIndex);
 
    }
 
    else // our port doesn't exist anymore, close port if it's open
 
@@ -200,45 +210,49 @@ void PortControl::selectFlowControl(int 
 
}
 

	
 
void PortControl::togglePort()
 
{
 
    if (serialPort->isOpen())
 
    {
 
        serialPort->close();
 
        qDebug() << "Closed port:" << serialPort->portName();
 
        emit portToggled(false);
 
    }
 
    else
 
    {
 
        serialPort->setPortName(ui->cbPortList->currentText());
 
        // port name may contain description
 
        QString portName = ui->cbPortList->currentText().split(" ")[0];
 
        serialPort->setPortName(portName);
 

	
 
        // open port
 
        if (serialPort->open(QIODevice::ReadWrite))
 
        {
 
            // set port settings
 
            selectBaudRate(ui->cbBaudRate->currentText());
 
            selectParity((QSerialPort::Parity) parityButtons.checkedId());
 
            selectDataBits((QSerialPort::DataBits) dataBitsButtons.checkedId());
 
            selectStopBits((QSerialPort::StopBits) stopBitsButtons.checkedId());
 
            selectFlowControl((QSerialPort::FlowControl) flowControlButtons.checkedId());
 

	
 
            qDebug() << "Opened port:" << serialPort->portName();
 
            emit portToggled(true);
 
        }
 
    }
 
    ui->pbOpenPort->setChecked(serialPort->isOpen());
 
}
 

	
 
void PortControl::selectPort(QString portName)
 
{
 
    // portName may be coming from combobox
 
    portName = portName.split(" ")[0];
 
    // has selection actually changed
 
    if (portName != serialPort->portName())
 
    {
 
        // if another port is already open, close it by toggling
 
        if (serialPort->isOpen())
 
        {
 
            togglePort();
 

	
 
            // open new selection by toggling
 
            togglePort();
 
        }
 
    }
0 comments (0 inline, 0 general)