Changeset - fa6975789e02
[Not reviewed]
default
0 1 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-07 18:03:15
hy@ozderya.net
close serial port when closing the application
1 file changed with 4 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -36,48 +36,52 @@ MainWindow::MainWindow(QWidget *parent) 
 
                     this, &MainWindow::onNumOfSamplesChanged);
 

	
 
    // init port signals
 
    QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)),
 
                     this, SLOT(onPortError(QSerialPort::SerialPortError)));
 

	
 
    loadPortList();
 
    loadBaudRateList();
 
    ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600"));
 

	
 
    // init plot
 
    numOfSamples = ui->spNumOfSamples->value();
 
    dataArray.resize(numOfSamples);
 
    dataX.resize(numOfSamples);
 
    for (int i = 0; i < dataX.size(); i++)
 
    {
 
        dataX[i] = i;
 
    }
 
    curve.setSamples(dataX, dataArray);
 
    curve.attach(ui->plot);
 
}
 

	
 
MainWindow::~MainWindow()
 
{
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
    }
 
    delete ui;
 
}
 

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

	
 
    ui->cbPortList->clear();
 

	
 
    for (auto port : QSerialPortInfo::availablePorts())
 
    {
 
        ui->cbPortList->addItem(port.portName());
 
    }
 

	
 
    // 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
 
    {
 
        if (serialPort.isOpen()) togglePort();
 
    }
0 comments (0 inline, 0 general)