-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchuankou.cpp
175 lines (157 loc) · 5.38 KB
/
chuankou.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "mainwindow.h"
#include "ui_mainwindow.h"
/*============================================
函数名:MainWindow::Port_Scan()
作用:串口可用扫描
==========================================*/
void MainWindow::Port_Scan()
{
QList<QSerialPortInfo> infos;
infos.clear();
infos=QSerialPortInfo::availablePorts();
if(infos.isEmpty())
{
ui->COMBox->addItem("无串口");
ui->ChuanKouButton->setEnabled(false);
return;
}
foreach (QSerialPortInfo info, infos)
{
ui->COMBox->addItem(info.portName());
}
}
/*============================================
函数名:MainWindow::Port_Init()
作用:打开串口
==========================================*/
void MainWindow::Port_Init()
{
if(ui->ChuanKouButton->text()==tr("打开串口"))
{
ui->ChuanKouButton->setEnabled(false);
Port.setPortName(ui->COMBox->currentText());
Port.open(QIODevice::ReadWrite);
Port.setBaudRate(ui->SpeedBox->currentText().toInt()); //设置波特率
switch(ui->SpeedBox->currentIndex())
{
case 0:
Port.setBaudRate(QSerialPort::Baud115200);
break;
case 1:
Port.setBaudRate(QSerialPort::Baud57600);
break;
case 2:
Port.setBaudRate(QSerialPort::Baud38400);
break;
case 3:
Port.setBaudRate(QSerialPort::Baud19200);
break;
case 4:
Port.setBaudRate(QSerialPort::Baud9600);
break;
}
switch(ui->DataBox->currentIndex()) //设置数据位
{
case 0:
Port.setDataBits(QSerialPort::Data8);
break;
case 1:
Port.setDataBits(QSerialPort::Data7);
break;
case 2:
Port.setDataBits(QSerialPort::Data6);
break;
case 3:
Port.setDataBits(QSerialPort::Data5);
break;
}
switch(ui->StopBox->currentIndex()) //设置停止位
{
case 0:
Port.setStopBits(QSerialPort::OneStop);
break;
case 1:
Port.setStopBits(QSerialPort::TwoStop);
break;
}
switch(ui->CheckBox->currentIndex()) //设置效验位
{
case 0:
Port.setParity(QSerialPort::NoParity);
break;
case 1:
Port.setParity(QSerialPort::OddParity);
break;
case 2:
Port.setParity(QSerialPort::EvenParity);
break;
}
ui->COMBox->setEnabled(false); //禁用按钮
ui->DataBox->setEnabled(false);
ui->SpeedBox->setEnabled(false);
ui->StopBox->setEnabled(false);
ui->CheckBox->setEnabled(false);
ui->ChuanKouButton->setText(tr("关闭串口")); //设置开关键
ui->ChuanKouButton->setEnabled(true);
QObject::connect(&this->Port,SIGNAL(readyRead()),this,SLOT(Receive_Slot()));
if(!Port.isOpen()&&ui->ChuanKouButton->text()=="关闭串口")
{
QMessageBox::information(this,"您可能使用了假串口","设备可能未连接或被其他程序占用",QMessageBox::Ok);
Port_Init();
}
}
else
{
ui->COMBox->setEnabled(true); //启用按钮
ui->DataBox->setEnabled(true);
ui->SpeedBox->setEnabled(true);
ui->StopBox->setEnabled(true);
ui->CheckBox->setEnabled(true);
Port.clear(); //关闭并删除串口
Port.close();
ui->ChuanKouButton->setText("打开串口");
}
}
/*============================================
函数名:MainWindow::Receive_Slot()
作用:接收串口信息
==========================================*/
void MainWindow::Receive_Slot()
{
int data[30];
if(Port.bytesAvailable()>=10)
{
QByteArray buf;
QString temp;
for(int i=0;i<10;i++)
{
buf =Port.read(1);
bool ok=true;
temp=buf.toHex();
data[i]=temp.toInt(&ok,16);
buf.clear();
temp.clear();
}
for(int i=0;i<10;i++)
{
if(data[i]==3&&data[i+1]==252)
{
if(ui->StartButton->text()=="发车!")
{
this->Start_Slot();//等效成按下按钮
}
else if(ui->StartButton->text()=="完成!")
{
if(this->Time<QTime(0,0,5,0))//防止重复触发
{
return;
}
else
{
this->Start_Slot();//等效成按下按钮
}
}
}
}
}
}