-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmythread.cpp
More file actions
118 lines (104 loc) · 3.06 KB
/
Copy pathmythread.cpp
File metadata and controls
118 lines (104 loc) · 3.06 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
#include "mythread.h"
#include <QtDebug>
//FOR RS232
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QtCore/QCoreApplication>
#include "publicdataclass.h"
extern PublicDataclass Dataclass;
MyThread::MyThread()
{
stopped=false;
}
void MyThread::run()
{
QSerialPort *my_serialport= new QSerialPort;
while(!stopped)
{
if(stopped&&com_opened)
{
my_serialport->close();
com_opened=false;
}
if(!com_opened)//串口未打开,打开串口
{
//open com
qDebug() << "Brush:" <<"---open com port------";
//com_opened=true;
my_serialport->setPortName(Dataclass.get_PortName());//Dataclass.PortName
my_serialport->open(QIODevice::ReadWrite);
my_serialport->setBaudRate(Dataclass.get_BaudRate());//Dataclass.BaudRate38400
my_serialport->setDataBits(QSerialPort::Data8);
my_serialport->setParity(QSerialPort::EvenParity);//NoParity
my_serialport->setStopBits(QSerialPort::OneStop);
my_serialport->setFlowControl(QSerialPort::NoFlowControl);
com_opened=true;
}
if(this->com_opened&&this->tx_event)//有内容待发送
{
this->tx_event=false;
my_serialport->clear(QSerialPort::AllDirections);
qDebug() << "Brush:" <<"send data to com2"<<this->TxData.length();
qDebug() << "arr size:" <<this->TxData.length();
my_serialport->write(this->TxData);
emit(this->comSend());
if (my_serialport->waitForBytesWritten(5))
{
qDebug() << "Brush:" <<"send data success";
if (my_serialport->waitForReadyRead(2000)) //1s
{
requestData = my_serialport->readAll();
while (my_serialport->waitForReadyRead(15))
requestData += my_serialport->readAll();
emit(this->comRecive());
}else
{
qDebug() << "Brush:" <<"wait return time out";
}
}else
{
qDebug() << "Brush:" <<"send time out";
}
}
if (my_serialport->waitForReadyRead(5)) //50ms
{
while (my_serialport->waitForReadyRead(5))
this->msleep(20);
requestData = my_serialport->readAll();
emit(this->comRecive());
}
if(stopped&&com_opened)
{
my_serialport->close();
com_opened=false;
}
}
}
void MyThread::stop()
{
stopped=true;
}
void MyThread::startCom()
{
stopped=false;
}
void MyThread::changeComState(bool stat)
{
com_opened=stat;
}
void MyThread::setMessage(const QString &message)
{
messageStr = message;
}
//void MyThread::setPortnum(const QString &num)
//{
// portnum=num;
//}
void MyThread:: changeTxState(bool stat)
{
tx_event=stat;
}
void MyThread:: changeRxState(bool stat)
{
rx_event=stat;
}