Changeset - 70f95e53d915
[Not reviewed]
default
0 1 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-07 17:57:44
hy@ozderya.net
enabled Start toolbar button
1 file changed with 3 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -119,99 +119,102 @@ void MainWindow::togglePort()
 
            }
 
        }
 
        else
 
        {
 
            qDebug() << "Port open error: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectPort(QString portName)
 
{
 
    // 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();
 
        }
 
    }
 
}
 

	
 
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->actionStart->isChecked())
 
    {
 
    QByteArray data = serialPort.readAll();
 
    addData((unsigned char)(data[0]));
 
}
 
}
 

	
 
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(double data)
 
{
 
    // shift data array and place new data at the end
 
    for (int i = 0; i < dataArray.size()-1; i++)
 
    {
 
        dataArray[i] = dataArray[i+1];
 
    }
 
    dataArray.last() = data;
 

	
 
    // update plot
 
    curve.setSamples(dataX, dataArray);
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
{
 
    int oldNum = this->numOfSamples;
 
    numOfSamples = value;
 

	
 
    // resize data arrays
 
    if (numOfSamples < oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        dataArray.remove(0, oldNum - numOfSamples);
 
    }
0 comments (0 inline, 0 general)