Untitled
public
May 15, 2024
Never
44
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 , serial(new QSerialPort) 8 { 9 ui->setupUi(this); 10 11 } 12 13 MainWindow::~MainWindow() 14 { 15 delete ui; 16 } 17 18 void MainWindow::on_btnReset_clicked(){ 19 fnc_closeSerialPort(); 20 ui->btnStart->setEnabled(true); 21 ui->btnReset->setEnabled(false); 22 // RESET VAR 23 red = green = yellow = sum = 0; 24 update_numColor(); 25 } 26 27 void MainWindow::on_btnStart_clicked() 28 { 29 fnc_closeSerialPort(); 30 open_serial_port(); 31 ui->btnStart->setEnabled(false); 32 ui->btnReset->setEnabled(true); 33 readData(); 34 35 } 36 37 void MainWindow::open_serial_port() { 38 // Set up serial port parameters 39 serial->setPortName("COM3"); 40 serial->setBaudRate(QSerialPort::Baud9600); 41 serial->setDataBits(QSerialPort::Data8); 42 serial->setParity(QSerialPort::NoParity); 43 serial->setStopBits(QSerialPort::OneStop); 44 serial->setFlowControl(QSerialPort::NoFlowControl); 45 // Attempt to open the serial port 46 if (serial->open(QIODevice::ReadWrite)) { 47 // Connection established, connect signals and slots 48 connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData); 49 connect(this, &MainWindow::updateNumColor, this, &MainWindow::update_numColor, Qt::QueuedConnection); 50 connect(ui->PauseButton, &QPushButton::clicked, this, &MainWindow::pauseArduino); 51 connect(ui->ContinueButton, &QPushButton::clicked, this, &MainWindow::continueArduino); 52 53 qDebug() << "Connected successfully"; 54 // Optionally, trigger initial readData() here if needed 55 // readData(); 56 } else { 57 // Connection failed, show error message 58 QMessageBox::critical(this, tr("Connection Error"), serial->errorString()); 59 } 60 } 61 void MainWindow::fnc_closeSerialPort() 62 { 63 if (serial->isOpen()) 64 serial->close(); 65 } 66 67 void MainWindow::readData() 68 { 69 //This code is to read all data on serial port 70 QByteArray data = serial->readAll(); 71 QString strData; 72 if (!data.isEmpty()) { 73 // qDebug() << "check"; 74 strData = QString(data); 75 // qDebug() << strData; 76 if (strData.compare("red\r\n", Qt::CaseInsensitive) == 0) { 77 red++; 78 } 79 80 if (strData.compare("green\r\n", Qt::CaseInsensitive) == 0) { 81 green++; 82 } 83 84 if (strData.compare("yellow\r\n", Qt::CaseInsensitive) == 0) { 85 yellow++; 86 } 87 88 sum = red + green + yellow; 89 90 update_numColor(); 91 } 92 } 93 void MainWindow::pauseArduino() { 94 // Gá»i tÃn hiệu cho Arduino yêu cầu tạm dừng chÆ°Æ¡ng trình 95 if (serial->isOpen()) { 96 // Gá»i dữ liệu tá»›i Arduino 97 serial->write("P"); 98 } 99 } 100 void MainWindow::continueArduino() { 101 // Gá»i tÃn hiệu cho Arduino yêu cầu tạm dừng chÆ°Æ¡ng trình 102 if (serial->isOpen()) { 103 // Gá»i dữ liệu tá»›i Arduino 104 serial->write("C"); 105 } 106 } 107 108 void MainWindow::update_numColor(){ 109 ui->numRed->setText(QString::number(red)); 110 ui->numgreen->setText(QString::number(green)); 111 ui->numyellow->setText(QString::number(yellow)); 112 ui->numsum->setText(QString::number(sum)); 113 }