日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下Qt编写串口调试助手,如何在linux下用QT写一个简单的串口调试助手

發(fā)布時間:2023/12/4 linux 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下Qt编写串口调试助手,如何在linux下用QT写一个简单的串口调试助手 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如何在linux下用QT寫一個簡單的串口調(diào)試助手

QT5串口類

在QT5以前,編寫串口一般使用的是qextserialport類,但在QT5之后有了QT自帶的串口類SerialPort(串口基礎(chǔ)類)和SerialPortInfo(串口信息類)

使用方法

pro中添加

QT += serialport

工程中包含相應(yīng)的頭文件

#include

#include

linux下查詢串口名的方法

ctrl+alt+a打開終端,輸入dmesg | grep ttyS*查詢串口名,一般為ttyUSB0

如圖 可以看到串口名為ttyUSB0,為ch340的驅(qū)動

linux的串口權(quán)限問題

我在剛開始寫這個程序的時候總是無法打開串口,嘗試了qextserialport類和SerialPort類都無法成功,后來想起來再用cutecom來調(diào)試串口的時候想到需要用管理員權(quán)限打開cutecom才可以打開串口,可是我又不知道怎么給QT管理員權(quán)限,因此我讓普通用戶也能使用串口,增加udev規(guī)則來實現(xiàn)

sudo vim /etc/udev/rules.d/70-ttyusb.rules

增加如下內(nèi)容:

KERNEL=="ttyUSB[0-9]*",MODE="0666"

保存并退出,重新插入USB轉(zhuǎn)串口,就可以打開串口了

此方法參考的如下鏈接:https://blog..net/touch_dream/article/details/52873651

以下給出部分代碼

mainwindow.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include

#include

#include

#include

#include

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

private slots:

void on_clearButton_clicked();//清除數(shù)據(jù)

void on_sendButton_clicked();//發(fā)送數(shù)據(jù)

void on_openButton_clicked();//打開串口

void Read_Data();//讀取串口數(shù)據(jù)

private:

Ui::MainWindow *ui;

QSerialPort *serial;//串口對象

};

#endif // MAINWINDOW_H

下面是mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

//查找可用的串口

foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())

{

QSerialPort serial;

serial.setPort(info);

if(serial.open(QIODevice::ReadWrite))

{

ui->PortBox->addItem(serial.portName());

serial.close();

}

}

//關(guān)閉發(fā)送按鈕的使能

ui->sendButton->setEnabled(false);

}

MainWindow::~MainWindow()

{

delete ui;

}

//清空接受窗口

void MainWindow::on_clearButton_clicked()

{

ui->textEdit->clear();

}

//發(fā)送數(shù)據(jù)

void MainWindow::on_sendButton_clicked()

{

serial->write(ui->textEdit_2->toPlainText().toLatin1());

}

void MainWindow::readMyCom() //讀取串口數(shù)據(jù)并顯示出來

{

QByteArray temp = myCom->readAll(); //讀取串口緩沖區(qū)的所有數(shù)據(jù)給臨時變量temp

ui->textEdit->insertPlainText(temp); //將串口的數(shù)據(jù)顯示在窗口的文本瀏覽器中

}

//讀取接收到的數(shù)據(jù)

void MainWindow::Read_Data()

{

qDebug() << tr("youshuju");

QByteArray buf;

buf = serial->readAll();

if(!buf.isEmpty())

{

QString str = ui->textEdit->toPlainText();

QTextCodec *codec = QTextCodec::codecForName("GBK");//指定QString的編碼方式

QString str_buf=codec->toUnicode(buf);//buf轉(zhuǎn)換成QString類型

str=str+'\n'+str_buf;

//buf=buf.toHex(); 或者使用這句話將buf轉(zhuǎn)換成十六進制顯示

//str=str+'\n'+tr(buf);

ui->textEdit->clear();

ui->textEdit->append(str);

}

buf.clear();

}

void MainWindow::on_openButton_clicked()

{

if(ui->openButton->text()==tr("打開串口"))

{

serial = new QSerialPort;

//設(shè)置串口名

serial->setPortName("/dev/ttyUSB0");//linux下默認(rèn)的串口名,可以進入終端查詢

//打開串口

if(serial->open(QIODevice::ReadWrite)) //如果成功打開串口

//設(shè)置波特率

{

qDebug() << tr("succeed");

// serial->setBaudRate(QSerialPort::Baud9600);

// serial->setDataBits(QSerialPort::Data8);

// serial->setParity(QSerialPort::NoParity);

// serial->setStopBits(QSerialPort::OneStop);

//設(shè)置數(shù)據(jù)位數(shù)

switch(ui->BitNumBox->currentIndex())

{

case 8: serial->setDataBits(QSerialPort::Data8); break;

default: break;

}

//設(shè)置奇偶校驗

switch(ui->ParityBox->currentIndex())

{

case 0: serial->setParity(QSerialPort::NoParity); break;

default: break;

}

//設(shè)置停止位

switch(ui->StopBox->currentIndex())

{

case 1: serial->setStopBits(QSerialPort::OneStop); break;

case 2: serial->setStopBits(QSerialPort::TwoStop); break;

default: break;

}

//設(shè)置流控制

serial->setFlowControl(QSerialPort::NoFlowControl);

//關(guān)閉設(shè)置菜單使能

ui->PortBox->setEnabled(false);

ui->BaudBox->setEnabled(false);

ui->BitNumBox->setEnabled(false);

ui->ParityBox->setEnabled(false);

ui->StopBox->setEnabled(false);

ui->openButton->setText(tr("關(guān)閉串口"));

ui->sendButton->setEnabled(true);

//連接信號槽

QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::Read_Data);

}

else

{

qDebug() << tr("default");

}

}

else

{

//關(guān)閉串口

serial->clear();

serial->close();

serial->deleteLater();

//恢復(fù)設(shè)置使能

ui->PortBox->setEnabled(true);

ui->BaudBox->setEnabled(true);

ui->BitNumBox->setEnabled(true);

ui->ParityBox->setEnabled(true);

ui->StopBox->setEnabled(true);

ui->openButton->setText(tr("打開串口"));

ui->sendButton->setEnabled(false);

}

}

最終效果

打開串口后即可收到串口發(fā)送過來的數(shù)據(jù)

總結(jié)

以上是生活随笔為你收集整理的linux下Qt编写串口调试助手,如何在linux下用QT写一个简单的串口调试助手的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。