-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.cpp
67 lines (47 loc) · 1.28 KB
/
serial.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
#include "serial.h"
Serial::Serial(QString _port,qint32 _baudrate)
{
port = _port;
baudrate = _baudrate;
}
Serial::~Serial() {
serial_port->close();
}
bool Serial::init() {
serial_port = new QSerialPort(this);
serial_port->setPortName(port);
qDebug() << port;
qDebug() << baudrate;
serial_port->setBaudRate(baudrate);
if(!serial_port->open(QIODevice::ReadWrite))
qDebug() << "erreur d'ouverture du port série";
QObject::connect(serial_port, SIGNAL(readyRead()), this, SLOT(readData()));
return true;
}
void Serial::write(QString str) {
serial_port->write(str.toStdString().c_str());
}
QStringList Serial::getPortList() {
QStringList portlist;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
portlist.push_back(info.portName());
}
return portlist;
}
qint32 Serial::getBaudrate() {
return serial_port->baudRate();
}
void Serial::setBaudrate(qint32 bd) {
serial_port->setBaudRate(bd);
}
void Serial::setPort(QString port) {
serial_port->setPortName(port);
serial_port->close();
serial_port->open(QIODevice::ReadWrite);
}
QString Serial::getPort() {
return serial_port->portName();
}
void Serial::readData() {
QByteArray dataread = serial_port->readAll();
}