-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialthread.cpp
More file actions
139 lines (120 loc) · 2.85 KB
/
serialthread.cpp
File metadata and controls
139 lines (120 loc) · 2.85 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "serialthread.h"
#include "datathread.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QThread>
#include <QDataStream>
#include <QByteArray>
#include <QVector>
#include <cmath>
#include <QReadWriteLock>
using namespace std;
class MainWindow;
extern QVector<QString> rawstring;
extern QVector<quint8> rawbyte;
extern QByteArray rawdata;
extern QVector<QByteArray> RxA;
extern QReadWriteLock lock;
//测试
extern QVector<quint8> buffer;
serialThread::serialThread()
{
serial=new QSerialPort;
stopped=false;
connect(serial, &QSerialPort::readyRead,this,&serialThread::readData);
}
//setup serialport
void serialThread::setCom(QString s)
{
com=s;
serial->clear();
serial->setPortName(com);
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
}
//open serialport
void serialThread::openCom()
{
bool flag=serial->open(QIODevice::ReadWrite);
if(flag)
{
QString mes(tr("串口打开成功"));
emit updateStatusSignal(mes);
}
else
{
QString mes=serial->errorString();
emit updateStatusSignal(mes);
}
}
//close serialport
void serialThread::closeCom()
{
if(serial->isOpen())
{
serial->close();
QString mes(tr("串口关闭成功"));
emit updateStatusSignal(mes);
}
else
{
QString mes=serial->errorString();
emit updateStatusSignal(mes);
}
}
//send data to serialport
void serialThread::sendData(QString str)
{
const QByteArray text = str.toLocal8Bit();
serial->write(text);
}
//read data from serialport
void serialThread::readData()
{
//读取串口缓冲区的数据
QByteArray Rx;
Rx=serial->readAll();
/*QDataStream out(&Rx,QIODevice::ReadWrite); //将字节数组读入
while(!out.atEnd())
{
//每字节填充一次,直到结束
quint8 outChar = 0;
out>>outChar;
rawbyte.push_back(outChar);
//每字节填充成一个字符串
/*QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0')); //十六进制的转换
str=str.toUpper();
rawstring.push_back(str);*/
/*static int flag=0;
//The first time meet 0xA0
if(flag==0&&outChar==0xA0)
{
buffer.push_back(outChar);
flag=1;
}
else if(flag==1)
{
buffer.push_back(outChar);
}
else
;
if(buffer.size()==37)
emit solveSignal();
}*/
//rawdata+=Rx;
lock.lockForWrite();
RxA.append(Rx);
lock.unlock();
emit solveRxASignal();
}
void serialThread::run()
{
//maybe need to write some code
}
serialThread::~serialThread()
{
stopped=true;
delete serial;
}