-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc_serialport.cpp
211 lines (187 loc) · 4.66 KB
/
c_serialport.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "c_serialport.h"
#include <QDebug>
C_SerialPort::C_SerialPort(QObject *parent) : QObject(parent)
{
m_sum = 0;
m_byte = 0;
m_byte_s = 0;
// 建立数据接收槽
connect(&this->m_serialPort,&QSerialPort::readyRead,this,&C_SerialPort::slot_readData);
}
// 读数据槽函数
void C_SerialPort::slot_readData()
{
QByteArray array = this->m_serialPort.readAll();
emit this->sig_readData(array);
m_sum += 1;
m_byte_s = array.size();
m_byte += array.size();
}
// 打开串口
bool C_SerialPort::openCOM(QString comName, qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits, QSerialPort::Parity parity, QSerialPort::FlowControl flowControl)
{
if(this->m_serialPort.isOpen())
{
return true;
}
this->m_serialPort.setPortName(comName);
this->m_serialPort.setBaudRate(baudRate);
this->m_serialPort.setDataBits(dataBits);
this->m_serialPort.setStopBits(stopBits);
this->m_serialPort.setParity(parity);
this->m_serialPort.setFlowControl(flowControl);
return this->m_serialPort.open(QIODevice::ReadWrite); // 读写模式
}
bool C_SerialPort::openCOM(serialCFG cfg)
{
return this->openCOM(cfg.comName,
cfg.baudRate,
cfg.dataBits,
cfg.stopBits,
cfg.parity,
cfg.flowControl);
}
//关闭串口
void C_SerialPort::closeCOM()
{
if(this->m_serialPort.isOpen())
{
this->m_serialPort.close();
}
}
//写数据
void C_SerialPort::writeData(QByteArray &array)
{
if(this->m_serialPort.isOpen())
{
this->m_serialPort.write(array);
}
}
void C_SerialPort::get_count(quint64 &sum, quint32 &byte_s, quint32 &byte)
{
sum = this->m_sum;
byte_s = this->m_byte_s;
byte = this->m_byte;
}
QStringList C_SerialPort::comList()
{
QStringList comlist;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
if(serial.open(QIODevice::ReadWrite))
{
comlist.append(serial.portName());
serial.close();
}
}
return comlist;
}
QStringList C_SerialPort::baudList()
{
QStringList list;
list<<"110"<<"300"<<"600"<<"1200"<<"2400"<<"4800"<<"9600"<<"14400"<<"19200"<<"38400"<<"56000"<<"57600"<<"115200"<<"128000"<<"256000";
return list;
}
QStringList C_SerialPort::dataBList()
{
QStringList list;
list<<"8"<<"7"<<"6"<<"5";
return list;
}
QStringList C_SerialPort::stopBList()
{
QStringList list;
list<<"1"<<"1.5"<<"2";
return list;
}
QStringList C_SerialPort::parityList()
{
QStringList list;
list<<"无校验"<<"奇校验"<<"偶校验"<<"标记校验"<<"空格校验";
return list;
}
QStringList C_SerialPort::flowCtrlList()
{
QStringList list;
list<<""<<"300"<<"600"<<"1200"<<"2400"<<"4800"<<"9600"<<"14400"<<"19200"<<"38400"<<"56000"<<"57600"<<"115200"<<"128000"<<"256000";
return list;
}
QSerialPort::Parity C_SerialPort::strToParity(QString str)
{
if("无校验"==str)
{
return QSerialPort::NoParity;
}else if("奇校验"==str)
{
return QSerialPort::OddParity;
}else if("偶校验"==str)
{
return QSerialPort::EvenParity;
}else if("标记校验"==str)
{
return QSerialPort::MarkParity;
}else if("空格校验"==str)
{
return QSerialPort::SpaceParity;
}else
{
return QSerialPort::NoParity;
}
}
QString C_SerialPort::parityToStr(QSerialPort::Parity parity)
{
if(QSerialPort::NoParity==parity)
{
return "无校验";
}
else if(QSerialPort::OddParity==parity)
{
return "奇校验";
}else if(QSerialPort::EvenParity==parity)
{
return "偶校验";
}else if(QSerialPort::MarkParity==parity)
{
return "标记校验";
}else if(QSerialPort::SpaceParity==parity)
{
return "空格校验";
}else
{
return "无校验";
}
}
QSerialPort::StopBits C_SerialPort::strToStopBits(QString str)
{
if("1"==str)
{
return QSerialPort::OneStop;
}else if("1.5"==str)
{
return QSerialPort::OneAndHalfStop;
}else if("2"==str)
{
return QSerialPort::TwoStop;
}else
{
return QSerialPort::UnknownStopBits;
}
}
QString C_SerialPort::stopBitsToStr(QSerialPort::StopBits stopBits)
{
if(QSerialPort::OneStop==stopBits)
{
return "1";
}else if(QSerialPort::OneAndHalfStop==stopBits)
{
return "1.5";
}else if(QSerialPort::TwoStop==stopBits)
{
return "2";
}else
{
return "未知";
}
}