-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
71 lines (61 loc) · 1.78 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort();
arduino_available = false;
foreach (const QSerialPortInfo &serial_Info, QSerialPortInfo::availablePorts()) {
qDebug()<<"Port: "<<serial_Info.portName();
portname = serial_Info.portName();
qDebug()<<"Vendor Id: "<<serial_Info.vendorIdentifier();
vendorId = serial_Info.vendorIdentifier();
qDebug()<<"Product Id: "<<serial_Info.productIdentifier();
productId = serial_Info.productIdentifier();
arduino_available = true;
}
if(arduino_available){
arduino_init();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::arduino_init()
{
serial->setPortName(portname);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
connect(serial, SIGNAL(readyRead()),this,SLOT(serial_read()));
}
void MainWindow::on_pushButton_ON_clicked()
{
if (serial->isWritable()) {
serial->write("2");
}
}
void MainWindow::on_pushButton_OFF_clicked()
{
if (serial->isWritable()) {
serial->write("3");
}
}
void MainWindow::serial_read()
{
if(serial->isWritable()&&arduino_available) {
QByteArray data = serial->readAll();
qDebug()<<"Veri okuma"<<data.toInt();
ui->lcdNumber->display(data.toInt());
}
if(ui->lcdNumber->value()>0) {
QMessageBox::information(this,"Buton durumu","Butona basıldı");
}
}