前言:
最近在學(xué)習(xí)嵌入式qt開發(fā),然后跟著教程編寫了一個簡單的串口上位機程序,在編寫的時候還算比較順利,但在調(diào)試的時候花了點功夫,折騰了一下午。最后還是理清了思路,解決了問題,特寫此博客進行記錄和總結(jié)。
串口上位機界面設(shè)計:
整個軟件的界面我都是用ui來設(shè)計的,其實也可以用代碼,但是想了想有好多布局相互嵌套比較麻煩。最后就使用了ui界面來設(shè)計。

ui界面設(shè)計
串口上位機程序功能設(shè)計:
然后像串口對象初始化,槽函數(shù),一些邏輯關(guān)系都用代碼來實現(xiàn),頭文件就只有一些變量、槽的定義。

頭文件文章來源:http://www.zghlxwxcb.cn/news/detail-700706.html
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//lcd屏的分辨率是1024X600
this->setFixedSize(QSize(1024, 600));
//new 一個QSerialPort對象
serialPort = new QSerialPort(this);
//通過foreach語句將windows或imx6ull可用串口識別出來,并添加至對應(yīng)的下拉選擇框
QStringList serialList;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){
serialList += info.portName();
}
ui->comboBox->addItems(serialList);
//設(shè)置串口的信號與槽
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_radioButton_clicked(bool checked)
{
//點擊“打卡串口”button之后的一些串口配置操作,和一些邏輯關(guān)系
if(checked){
//設(shè)置串口端
serialPort->setPortName(ui->comboBox->currentText());
//設(shè)置波特率
serialPort->setBaudRate(ui->comboBox_2->currentText().toInt());
//設(shè)置停止位
serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_4->currentText().toInt()));
//設(shè)置數(shù)據(jù)位
serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_6->currentText().toInt()));
//設(shè)置校驗位
switch(ui->comboBox_5->currentIndex()){
case 0:
serialPort->setParity(QSerialPort::Parity::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::Parity::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::Parity::OddParity);
break;
case 3:
serialPort->setParity(QSerialPort::Parity::SpaceParity);
break;
case 4:
serialPort->setParity(QSerialPort::Parity::MarkParity);
break;
default:
break;
}
serialPort->setFlowControl(QSerialPort::NoFlowControl);
if(!serialPort->open(QSerialPort::ReadWrite)){
QMessageBox::about(this, "串口打開錯誤提示", "可能被占用了");
return;
}
//打開串口之后其他的選擇按鈕設(shè)置成不可用
ui->comboBox->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->comboBox_4->setEnabled(false);
ui->comboBox_5->setEnabled(false);
ui->comboBox_6->setEnabled(false);
ui->radioButton->setText("關(guān)閉串口");
}
else{
serialPort->close();
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
ui->comboBox_6->setEnabled(true);
ui->radioButton->setText("打開串口");
}
}
void Widget::readData()
{
//將串口中的數(shù)據(jù)打印到textBrowser中
ui->textBrowser->insertPlainText(serialPort->readAll());
}
void Widget::on_pushButton_clicked()
{
//點擊“發(fā)送”button之后,將textEdit中的內(nèi)容寫入串口
serialPort->write(ui->textEdit->toPlainText().toUtf8());
}
void Widget::on_pushButton_2_clicked()
{
//清屏操作
ui->textEdit->clear();
ui->textBrowser->clear();
}
在windows端編寫好程序之后,將工程文件發(fā)送到ubuntu進行qmake、make最后通過scp將ARM的可執(zhí)行文件發(fā)送到開發(fā)板,接下來就是驗證了
我用的是正點原子出廠的系統(tǒng),所以在驗證的時候需要使用到一個串口來連接到開發(fā)板執(zhí)行編寫好的上位機程序,所以一個串口就被占用了,正點原子教程視頻就用開發(fā)板的另一個串口來進行測試

正點原子教程測試方式文章來源地址http://www.zghlxwxcb.cn/news/detail-700706.html
因為放假回了家,我也沒有帶太多的東西,usb轉(zhuǎn)ttl模塊放在了學(xué)校。所以我在想怎么就用一個串口來完成實驗。困擾我的就是唯一的一個串口必須留個上位機,然后要怎么執(zhí)行開發(fā)板上的串口上位機程序?最初我想到了可以使用網(wǎng)絡(luò)通過Xshell遠程連接到開發(fā)板就可以執(zhí)行,唯一的串口就拿來驗證。

解決完上位機軟件怎么在imx6ull上執(zhí)行的問題,接下來就是驗證了。驗證的時候又出現(xiàn)了一些問題,上位機軟件不像想象的那樣一邊發(fā)數(shù)據(jù),一邊就接收對應(yīng)的數(shù)據(jù),而出現(xiàn)了一些imx6ull的信息,好像還可以輸入命令,感到非常奇怪。

我以為我的軟件有問題,就在windows上換了一個串口軟件,結(jié)果還是出現(xiàn)了類似的情況

最后也是在不斷猜想和測試之后找到了原因,就是應(yīng)因為我在給開發(fā)板上電的時候接入了串口線,然后先打開了windows上的串口,然后就使用到了串口,導(dǎo)致就相當于有一個串口控制imx6ull開發(fā)板,像Xshell中通過串口連接開發(fā)板一樣。然后我嘗試在windows的上位機軟件輸入命令想打開一個qt程序,最后也是成功打開證實了猜想


執(zhí)行./list命令打開了蛇姐list程序(qt程序的路徑是/home/root/qt_project/list)

如果想要實現(xiàn)實驗最初windows和imx6ull兩個上位機互換數(shù)據(jù)的效果的話。要先通過windows中的上位機軟件輸入命令來打開imx6ull的上位機軟件,這樣相當于windows中的上位機連接的就是imx6ull的上位機了,而不是直接控制imx6ull了,之后就可以正常收發(fā)數(shù)據(jù)了。



總結(jié):
學(xué)了qt快半個月了,這個串口上位機算是一個比較綜合的練習(xí)了,結(jié)合了ui和代碼設(shè)計。加上最后的調(diào)試也是收獲頗多,如果有usb-ttl模塊的話驗證步驟就會更簡單。
到了這里,關(guān)于正點原子Linux開發(fā)板——Qt串口上位機實驗的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!