Changeset - 73abbe942246
[Not reviewed]
default
0 1 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-16 18:27:43
hy@ozderya.net
discard all received data when paused
1 file changed with 4 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -185,96 +185,100 @@ void MainWindow::selectPort(QString port
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectBaudRate(QString baudRate)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if (!serialPort.setBaudRate(baudRate.toInt()))
 
        {
 
            qDebug() << "Set baud rate failed during select: "
 
                     << serialPort.error();
 
        }
 
        else
 
        {
 
            qDebug() << "Baud rate changed: " << serialPort.baudRate();
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortToggled(bool open)
 
{
 
    ui->pbOpenPort->setChecked(open);
 
}
 

	
 
void MainWindow::onDataReady()
 
{
 
    if (!ui->actionPause->isChecked())
 
    {
 
        int bytesAvailable = serialPort.bytesAvailable();
 
        if (bytesAvailable < sampleSize)
 
        {
 
            return;
 
        }
 
        else
 
        {
 
            int numOfSamplesToRead =
 
                (bytesAvailable - (bytesAvailable % sampleSize)) / sampleSize;
 
            QVector<double> samples(numOfSamplesToRead);
 
            int i = 0;
 
            while(i < numOfSamplesToRead)
 
            {
 
                samples.replace(i, (this->*readSample)());
 
                i++;
 
            }
 
            addData(samples);
 
        }
 
    }
 
    else
 
    {
 
        serialPort.clear(QSerialPort::Input);
 
    }
 
}
 

	
 
void MainWindow::onPortError(QSerialPort::SerialPortError error)
 
{
 
    switch(error)
 
    {
 
        case QSerialPort::NoError :
 
            break;
 
        case QSerialPort::ResourceError :
 
            qDebug() << "Port error: resource unavaliable; most likely device removed.";
 
            if (serialPort.isOpen())
 
            {
 
                qDebug() << "Closing port on resource error: " << serialPort.portName();
 
                togglePort();
 
            }
 
            loadPortList();
 
            break;
 
        default:
 
            qDebug() << "Unhandled port error: " << error;
 
            break;
 
    }
 

	
 
}
 

	
 
void MainWindow::addData(QVector<double> data)
 
{
 
    int offset = numOfSamples - data.size();
 

	
 
    if (offset < 0)
 
    {
 
        for (int i = 0; i < numOfSamples; i++)
 
        {
 
            dataArray[i] = data[i - offset];
 
        }
 
    }
 
    else if (offset == 0)
 
    {
 
        dataArray = data;
 
    }
 
    else
 
    {
 
        // shift old samples
 
        int shift = data.size();
 
        for (int i = 0; i < offset; i++)
 
        {
 
            dataArray[i] = dataArray[i + shift];
 
        }
 
        // place new samples
0 comments (0 inline, 0 general)