Serial data capture through timer (since timer is implemented as thread)
1) declare slots in header file and
2) define them in its .cpp file
mainwindow.h
2) define them in its .cpp file
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSerialPort>
#include <QDebug>
#include<QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
QTimer *dataCaptTimer;
QSerialPort serial;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_start_btn_pressed();
void on_stop_btn_pressed();
private:
Ui::MainWindow *ui;
public slots:
void startDataCapture();
void stopDataCapture();
};
#endif // MAINWINDOW_H
mainwindow.cpp file
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){dataCaptTimer=new QTimer;dataCaptTimer->setInterval(50);connect(dataCaptTimer,SIGNAL(timeout()),this,SLOT(startDataCapture()));ui->setupUi(this);serial.setPortName("COM6");serial.setBaudRate(QSerialPort::Baud9600);serial.setDataBits(QSerialPort::Data8);serial.setStopBits(QSerialPort::OneStop);serial.setFlowControl(QSerialPort::NoFlowControl);serial.setParity(QSerialPort::NoParity);}MainWindow::~MainWindow(){serial.close();delete ui;}void MainWindow::on_start_btn_pressed(){startDataCapture();}void MainWindow::on_stop_btn_pressed(){serial.close();dataCaptTimer->stop();}void MainWindow::startDataCapture(){dataCaptTimer->stop(); //stop timer so that it doesn't generate timeout() interrupt signals while capturing dataserial.open(QIODevice::ReadWrite);char buffer[50];serial.waitForReadyRead();serial.readLine(buffer, 50);qDebug()<<buffer;dataCaptTimer->start(); //restart the timer}
Comments
Post a Comment