-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialservice.cpp
More file actions
90 lines (82 loc) · 1.64 KB
/
serialservice.cpp
File metadata and controls
90 lines (82 loc) · 1.64 KB
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
#include "serialservice.h"
SerialService::SerialService(QObject *parent) :
QObject(parent),
my_com(0),
com_state(0)
{
read_timer = new QTimer();
read_timer->start(READTIME);
connect(this->read_timer, SIGNAL(timeout()), this, SLOT(readFromSerial()));
}
void SerialService::readFromSerial()
{
if(my_com && 1 == com_state)
{
QByteArray byte = my_com->readAll();
if(!byte.isEmpty())
emit receiveMsgFromSerial(byte);
}
}
int SerialService::writeToSerial(const QByteArray &byte)
{
if(my_com && 1 == com_state)
{
return my_com->write(byte);
}
return -1;
}
bool SerialService::openCom()
{
if(my_com)
{
closeCom();
}
else
{
QString com_name = "/dev/ttySAC1";
my_com = new Posix_QextSerialPort(com_name, QextSerialBase::Polling);
}
my_com->open(QIODevice::ReadWrite);
if(my_com->isOpen())
{
com_state = 1;
my_com->setBaudRate(BAUD115200);
my_com->setDataBits(DATA_8);
my_com->setParity(PAR_NONE);
my_com->setStopBits(STOP_1);
my_com->setFlowControl(FLOW_OFF);
my_com->setTimeout(50);
return true;
}
else
{
com_state = 0;
return false;
}
}
bool SerialService::closeCom()
{
if(my_com && 0 != com_state)
{
my_com->close();
}
com_state = 0;
return true;
}
void SerialService::releaseSerial()
{
if(my_com)
{
closeCom();
delete my_com;
my_com = 0;
}
}
QTimer* SerialService::getTimer()
{
return read_timer;
}
int SerialService::getComState()
{
return this->com_state;
}