forked from demitov/dashApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialport.cpp
53 lines (45 loc) · 1.31 KB
/
serialport.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
#include "serialport.h"
//#include <QDebug>
SerialPort::SerialPort(QObject *parent):QObject(parent)
{
arduino = new QSerialPort(this);
connect(arduino, &QSerialPort::readyRead, this, &SerialPort::onReadData);
openDefault();
}
SerialPort::~SerialPort()
{
delete arduino;
}
void SerialPort::set_serial_data(QString newValue)
{
mSerial_data = newValue;
emit serial_data_Changed(mSerial_data);
}
void SerialPort::onReadData()
{
// IMPORTANT: That's a *while*, not an *if*!
while (arduino->canReadLine()) {
QByteArray data = arduino->readLine();
// qDebug()<<QString(data).trimmed();
QString value = QString(data).trimmed();
set_serial_data(value);
}
}
void SerialPort::openDefault()
{
arduino->setPortName("ttyAMA0");
// arduino->setPortName("COM3");
arduino->setBaudRate(QSerialPort::Baud115200);
arduino->setDataBits(QSerialPort::Data8);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
arduino->setFlowControl(QSerialPort::NoFlowControl);
// if (arduino->open(QSerialPort::ReadWrite))
// qDebug()<<"Connected to "<< arduino->portName();
// else
// qCritical()<<"Serial Port error: " << arduino->errorString();
}
QString SerialPort::get_serial_data() const
{
return mSerial_data;
}