-
Notifications
You must be signed in to change notification settings - Fork 3
/
serialmain.cpp
95 lines (72 loc) · 1.93 KB
/
serialmain.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "serialmain.h"
#include <iostream>
SerialMain::SerialMain(QObject *parent ) : QObject(parent)
{
port = new QextSerialPort("/dev/ttyACM0", QextSerialPort::EventDriven); //Serial Port
port->setBaudRate(BAUD115200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
//set timeouts to 500 ms
port->setTimeout(500);
connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));
openPort();
}
SerialMain::~SerialMain(){
delete port;
port = NULL;
}
void SerialMain::transmitMsg(const QByteArray &message)
{
// int i = port->write(message.toLatin1());
int i = port->write(message);
qDebug("trasmitted : %d", i);
}
void SerialMain::receiveMsg()
{
char buff[1024];
int numBytes;
numBytes = port->bytesAvailable();
if(numBytes > 1024)
numBytes = 1024;
int i = port->read(buff, numBytes);
if (i != -1)
buff[i] = '\0';
else
buff[0] = '\0';
QString msg = QLatin1String(buff);
//received_msg->append(msg);
// received_msg->ensureCursorVisible();
qDebug("bytes available: %d", numBytes);
qDebug("received: %d", i);
}
void SerialMain::appendCR()
{
//message.insert(QLatin1String("\x0D"));
}
void SerialMain::appendLF()
{
// message.insert(QLatin1String("\x0A"));
}
void SerialMain::closePort()
{
port->close();
qDebug("is open: %d", port->isOpen());
}
void SerialMain::openPort()
{
port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
qDebug("is open: %d", port->isOpen());
if(port->isOpen())std::cout<<"Port Open"<<std::endl;
else std::cout<<"Port Close"<<std::endl;
}
void SerialMain::onReadyRead()
{
if (port->bytesAvailable()) {
//QByteArray test=port->readAll();
emit dataReceived(port->readAll());
//QByteArray testHex=test.toHex();
//std::cout<<(QString::fromLatin1(testHex)).toStdString();
}
}