QT5有专门的串口类:
QSerialPort:提供访问串口的功能
QSerialPortInfo:提供系统中存在的串口的信息
具体使用方法:
1.在pro文件中加入: QT += serialport
2.mainwindow.h中添加头文件及成员:
<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <QSerialPort></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <QSerialPortInfo></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>: QSerialPort serial;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//串口实例</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> initSeialPort();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化串口函数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> slots://必须加slots <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">serialRead</span>(); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
3.在主窗口添加一个comboBox,一个label,初始化串口
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> MainWindow<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::initSeialPort</span>() { connect(<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&</span>serial,SIGNAL(readyRead()),this,SLOT(serialRead())); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//连接槽</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取计算机上所有串口并添加到comboBox中</span> QList<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>QSerialPortInfo<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> infos <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> QSerialPortInfo<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::availablePorts</span>(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(infos<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>isEmpty()) { ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>comboBox<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>addItem(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"无串口"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } foreach (QSerialPortInfo info, infos) { ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>comboBox<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>addItem(info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName()); qDebug() <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName(); } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
3.comboBox选择改变事件函数:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> MainWindow<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::on_comboBox_currentIndexChanged</span>(const QString <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&</span>arg1) { QSerialPortInfo info; QList<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>QSerialPortInfo<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> infos <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> QSerialPortInfo<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::availablePorts</span>(); int i <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; foreach (info, infos) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName() <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> arg1) break; i<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">++</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(i <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> infos<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>size ()){<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//can find</span> ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>lblStatus<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口打开成功"</span>); serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>close(); serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setPort(info); serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>open(QIODevice<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::ReadWrite</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//读写打开</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setBaudRate(QSerialPort::Baud9600); //波特率</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setDataBits(QSerialPort::Data8); //数据位</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setParity(QSerialPort::NoParity); //无奇偶校验</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setStopBits(QSerialPort::OneStop); //无停止位</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setFlowControl(QSerialPort::NoFlowControl); //无控制</span> }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>close(); ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>lblStatus<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口打开失败"</span>); } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
4.串口发送
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> MainWindow::on_btnSend_clicked() { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// QByteArray arr = "abcd";</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.write(arr);</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> dat[] = {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x55</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x01</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x05</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xAA</span>}; serial.write(dat,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
5.串口接收,需添加一个textEdit。
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">void <span class="hljs-constant" style="box-sizing: border-box;">MainWindow::</span>serialRead() { <span class="hljs-constant" style="box-sizing: border-box;">QByteArray</span> qa = serial.readAll(); <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(qa.at(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)==<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>) ui->textEdit->append(qa); <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-constant" style="box-sizing: border-box;">QMessageBox::</span>warning (this,tr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"提示信息"</span>),tr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口接收成功"</span>)); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="color: rgb(106, 57, 6); box-sizing: border-box;"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box; max- 100%;" alt="" /></a></div>
可以用虚拟串口软件和丁丁串口助手调试。