欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā))

 更新時間:2020年03月12日 14:51:37   作者:imkelt  
這篇文章主要介紹了Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā)),需要的朋友可以參考下

之前自己寫了用于上位機做基本收發(fā)的界面,獨立出來相當于一個串口助手,先貼圖:

功能作為串口助手來說還算完善,五個發(fā)送槽,一個接收槽,可以檢測可用串口并加上相關(guān)標志,串口設(shè)置,記數(shù)功能,還有菜單欄上的文件操作和一些選擇功能。

下面說一說這個項目:

做這個串口助手分為兩步,第一步是設(shè)計界面,第二部是功能的代碼實現(xiàn)。

一、界面設(shè)計

界面設(shè)計用Qt Designer,當然用Qt Creator的界面編輯器也可以,只不過感覺Qt Designer更好用一點,因為可以隨時運行查看你的界面效果而不用編譯整個項目輸出一個可執(zhí)行程序再看看界面效果,這樣會影響效率。

界面設(shè)計你想界面是什么樣就怎么樣設(shè)計,拉控件,排版,設(shè)置大小,修改對象名等等,都在這上面做好,這些用程序?qū)懙脑挄苈闊?,工作量也大。這上面的對象名很重要,因為在后面的代碼實現(xiàn)中會用到,這個界面用到的控件還是挺多的,這里也不一個一個講,我直接貼出來:

senderGB_1 - 5都是一樣的,改下數(shù)就行

當然,用你自己喜歡的命名也可以,后面程序改下名字就行。

二、代碼實現(xiàn)

先貼代碼

basictransceiver.h

#ifndef BASICTRANSCEIVER_H
#define BASICTRANSCEIVER_H

#include <QMainWindow>
#include "ui_basictransceiver.h"

class QTimer;
class SerialPortSetting;
class QSerialPort;
class QPushButton;

class BasicTransceiver : public QMainWindow, public Ui::BasicTransceiver
{
 Q_OBJECT

public:
 explicit BasicTransceiver(QWidget *parent = 0);

 ~BasicTransceiver();

 void StringToHex(QString str, QByteArray &senddata);

 char ConvertHexChar(char ch);

 void startAutoSend(QPushButton *sendButton);

 void setConnections();

 void writeHex(QTextEdit *textEdit);

 void writeChr(QTextEdit *textEdit);

 void resetCnt();

protected:
 void dragEnterEvent(QDragEnterEvent *event);

 void dropEvent(QDropEvent *event);

private slots:
 void checkAutoSendCB();

 void on_cleanButton_clicked();

 void on_receiveTextBrowser_textChanged();

 void setBaudRate();

 void setParity();

 void setDataBits();

 void setStopBits();

 void setFlowCtrl();

 void on_connectButton_toggled(bool checked);

 void setComLabel();

 void setBaudLabel();

 void writeToBuf();

 void enabledSendButton();

 void disabledSendButton();

 void enabledAutoSend();

 void disabledAutoButton();

 void resetAutoSendCB();

 void readMyCom();

 void checkAvailablePorts();

 void on_checkAPButton_clicked();

 void checkPort();

 void on_resetCntButton_clicked();

 void on_exitButton_clicked();

 bool saveAs();

 void open();

 //void about();

private:
 bool loadFile(const QString &fileName);

 bool readFile(const QString &fileName);

 bool saveFile(const QString &fileName);

 bool writeFile(const QString &fileName);

 QTimer *Timer_AS;//自動發(fā)送定時器
 QTimer *Timer_UPDATE;
 QTimer *Timer_CP;//定時檢測串口是否存在
 SerialPortSetting *SPSetting;
 QSerialPort *mySerialPort;
 QSet<QString> portSet;
 QVector<int> iVec;
 QString senderFlag;
 QString readData;
 bool trashFlag = false;
 bool portIsOpen = false;
 int BaudCnt = 0;
 int ParityCnt = 0;
 int DataBitsCnt = 0;
 int StopBitsCnt = 0;
 int FlowCtrlCnt = 0;


};

#endif // BASICTRANSCEIVER_H

basictransceiver.cpp

#include "basictransceiver.h"
#include "serialportsetting.h"
#include "ui_basictransceiver.h"
#include "ui_serialportsetting.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QMessageBox>
#include <QStatusBar>
#include <QPushButton>
#include <QByteArray>
#include <QDataStream>
#include <QTimer>
#include <QRegExp>
#include <QRegExpValidator>
#include <QFile>
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QAction>

BasicTransceiver::BasicTransceiver(QWidget *parent) :
 QMainWindow(parent)
{
 setupUi(this);
 setFixedSize(1074, 627);
 receiveTextBrowser->setAcceptDrops(false);//缺省情況下,QTextEdit接受來自其他應(yīng)用程序拖拽來的文本,把文件名顯示出來。
 senderTextEdit_1->setAcceptDrops(false);
 senderTextEdit_2->setAcceptDrops(false);
 senderTextEdit_3->setAcceptDrops(false);
 senderTextEdit_4->setAcceptDrops(false);
 senderTextEdit_5->setAcceptDrops(false);
 setAcceptDrops(true);//通過禁止QTextEdit控件的drop事件,允許主窗口得到drop事件

 connectButton->setIcon(QIcon(":/images/open.png"));
 cleanButton->setIcon(QIcon(":/images/empty_bin.png"));
 checkAPButton->setIcon(QIcon(":/images/find.png"));
 resetCntButton->setIcon(QIcon(":/images/to_zero.png"));
 exitButton->setIcon(QIcon(":/images/exit.png"));
 actionWrite_data->setIcon(QIcon(":/images/write.png"));
 actionRead_data->setIcon(QIcon(":/images/read.png"));
 actionChoose_file->setIcon(QIcon(":/images/select_file.png"));
 exitAction->setIcon(QIcon(":/images/exit.png"));
 actionAbout->setIcon(QIcon(":/images/about.png"));
 sendButton_1->setIcon(QIcon(":/images/send.png"));
 sendButton_2->setIcon(QIcon(":/images/send.png"));
 sendButton_3->setIcon(QIcon(":/images/send.png"));
 sendButton_4->setIcon(QIcon(":/images/send.png"));
 sendButton_5->setIcon(QIcon(":/images/send.png"));

 setConnections();
 emit checkAvailablePorts();

 Timer_CP = new QTimer(this);
 Timer_UPDATE = new QTimer(this);
 connect(Timer_UPDATE, SIGNAL(timeout()), this, SLOT(repaint()));
 Timer_UPDATE->start(2000);
 Timer_AS = new QTimer(this);
}

BasicTransceiver::~BasicTransceiver()
{

}

void BasicTransceiver::checkAutoSendCB()
{
 QObject *signalSender = sender();
 if ( signalSender->objectName() == "autoSendCB_1")
 {
  if (autoSendCB_1->isChecked())
  {
   intervalSB_1->setEnabled(false);
   autoSendCB_2->setEnabled(false);
   autoSendCB_3->setEnabled(false);
   autoSendCB_4->setEnabled(false);
   autoSendCB_5->setEnabled(false);
   startAutoSend(sendButton_1);
  } else if (!autoSendCB_1->isChecked()) {
   Timer_AS->stop();
   Timer_AS->disconnect();
   intervalSB_1->setEnabled(true);
   autoSendCB_2->setEnabled(true);
   autoSendCB_3->setEnabled(true);
   autoSendCB_4->setEnabled(true);
   autoSendCB_5->setEnabled(true);
   enabledSendButton();
  }
 } else if ( signalSender->objectName() == "autoSendCB_2") {
   if (autoSendCB_2->isChecked())
   {
    intervalSB_2->setEnabled(false);
    autoSendCB_1->setEnabled(false);
    autoSendCB_3->setEnabled(false);
    autoSendCB_4->setEnabled(false);
    autoSendCB_5->setEnabled(false);
    startAutoSend(sendButton_2);
   } else if (!autoSendCB_2->isChecked()) {
    Timer_AS->stop();
    Timer_AS->disconnect();
    intervalSB_2->setEnabled(true);
    autoSendCB_1->setEnabled(true);
    autoSendCB_3->setEnabled(true);
    autoSendCB_4->setEnabled(true);
    autoSendCB_5->setEnabled(true);
    enabledSendButton();
   }
  } else if ( signalSender->objectName() == "autoSendCB_3") {
   if (autoSendCB_3->isChecked())
   {
    intervalSB_3->setEnabled(false);
    autoSendCB_1->setEnabled(false);
    autoSendCB_2->setEnabled(false);
    autoSendCB_4->setEnabled(false);
    autoSendCB_5->setEnabled(false);
    startAutoSend(sendButton_3);
   } else if (!autoSendCB_3->isChecked()) {
    Timer_AS->stop();
    Timer_AS->disconnect();
    intervalSB_3->setEnabled(true);
    autoSendCB_1->setEnabled(true);
    autoSendCB_2->setEnabled(true);
    autoSendCB_4->setEnabled(true);
    autoSendCB_5->setEnabled(true);
    enabledSendButton();
   }
  } else if ( signalSender->objectName() == "autoSendCB_4") {
   if (autoSendCB_4->isChecked())
   {
    intervalSB_4->setEnabled(false);
    autoSendCB_1->setEnabled(false);
    autoSendCB_2->setEnabled(false);
    autoSendCB_3->setEnabled(false);
    autoSendCB_5->setEnabled(false);
    startAutoSend(sendButton_4);
   } else if (!autoSendCB_4->isChecked()) {
    Timer_AS->stop();
    Timer_AS->disconnect();
    intervalSB_4->setEnabled(true);
    autoSendCB_1->setEnabled(true);
    autoSendCB_2->setEnabled(true);
    autoSendCB_3->setEnabled(true);
    autoSendCB_5->setEnabled(true);
    enabledSendButton();
   }
  } else if ( signalSender->objectName() == "autoSendCB_5") {
   if (autoSendCB_5->isChecked())
   {
    intervalSB_5->setEnabled(false);
    autoSendCB_1->setEnabled(false);
    autoSendCB_2->setEnabled(false);
    autoSendCB_3->setEnabled(false);
    autoSendCB_4->setEnabled(false);
   startAutoSend(sendButton_5);
   } else if (!autoSendCB_5->isChecked()) {
    Timer_AS->stop();
    Timer_AS->disconnect();
    intervalSB_5->setEnabled(true);
    autoSendCB_1->setEnabled(true);
    autoSendCB_2->setEnabled(true);
    autoSendCB_3->setEnabled(true);
    autoSendCB_4->setEnabled(true);
    enabledSendButton();
   }
  }
 }

//清除接收區(qū)的內(nèi)容
void BasicTransceiver::on_cleanButton_clicked()
{
 if (trashFlag == true) {
  receiveTextBrowser->clear();
  cleanButton->setIcon(QIcon(":/images/empty_bin.png"));
 }
}

void BasicTransceiver::on_receiveTextBrowser_textChanged()
{
 QString tempStr = receiveTextBrowser->toPlainText();
 if (!tempStr.isEmpty()) {
  trashFlag = true;
  if (autoClean->isChecked()){
   if (tempStr.size() >6200 ){
    receiveTextBrowser->clear();
    cleanButton->setIcon(QIcon(":/images/empty_bin.png"));
   }
  } else {
  cleanButton->setIcon(QIcon(":/images/clean.png"));
  }
 } else {
  trashFlag = false;
  cleanButton->setIcon(QIcon(":/images/empty_bin.png"));
 }
}


void BasicTransceiver::setBaudRate()
{
 if (portIsOpen) {
  if (BAUDCB->currentText() == "115200")
   mySerialPort->setBaudRate(QSerialPort::Baud115200);
  else if (BAUDCB->currentText() == "9600")
   mySerialPort->setBaudRate(QSerialPort::Baud9600);
  else if (BAUDCB->currentText() == "1200")
   mySerialPort->setBaudRate(QSerialPort::Baud1200);
  else if (BAUDCB->currentText() == "2400")
   mySerialPort->setBaudRate(QSerialPort::Baud2400);
  else if (BAUDCB->currentText() == "4800")
   mySerialPort->setBaudRate(QSerialPort::Baud4800);
  else if (BAUDCB->currentText() == "19200")
   mySerialPort->setBaudRate(QSerialPort::Baud19200);
  else if (BAUDCB->currentText() == "38400")
   mySerialPort->setBaudRate(QSerialPort::Baud38400);
  else if (BAUDCB->currentText() == "57600")
   mySerialPort->setBaudRate(QSerialPort::Baud57600);
  emit setBaudLabel();
  if (BaudCnt) {
   statusBar()->showMessage("BaudRate set successfully", 2000);
  }
  ++BaudCnt;
 }
}

void BasicTransceiver::setParity()
{
 if (portIsOpen) {
  if (ParityCB->currentText() == QString::fromLocal8Bit("無校驗"))
   mySerialPort->setParity(QSerialPort::NoParity);
  else if (ParityCB->currentText() == QString::fromLocal8Bit("奇校驗"))
   mySerialPort->setParity(QSerialPort::OddParity);
  else if (ParityCB->currentText() == QString::fromLocal8Bit("偶校驗"))
   mySerialPort->setParity(QSerialPort::EvenParity);
  if (ParityCnt) {
   statusBar()->showMessage("Parity set successfully", 2000);
  }
  ++ParityCnt;
 }
}

void BasicTransceiver::setDataBits()
{
 if (portIsOpen) {
  if (DataBitsCB->currentText() == "8")
   mySerialPort->setDataBits(QSerialPort::Data8);
  else if (DataBitsCB->currentText() == "7")
   mySerialPort->setDataBits(QSerialPort::Data7);
  else if (DataBitsCB->currentText() == "6")
   mySerialPort->setDataBits(QSerialPort::Data6);
  else if (DataBitsCB->currentText() == "5")
   mySerialPort->setDataBits(QSerialPort::Data5);
  if (DataBitsCnt) {
    statusBar()->showMessage("DataBits set successfully", 2000);
  }
  ++DataBitsCnt;
 }
}

void BasicTransceiver::setStopBits()
{
 if (portIsOpen) {
  if (StopBitsCB->currentText() == "1")
   mySerialPort->setStopBits(QSerialPort::OneStop);
  else if (StopBitsCB->currentText() == "1.5")
   mySerialPort->setStopBits(QSerialPort::OneAndHalfStop);
  else if (StopBitsCB->currentText() == "2")
   mySerialPort->setStopBits(QSerialPort::TwoStop);
  if (StopBitsCnt) {
   statusBar()->showMessage("StopBits set successfully", 2000);
  }
  ++StopBitsCnt;
 }
}

void BasicTransceiver::setFlowCtrl()
{
 if (portIsOpen) {
  if (FlowCtrlCB->currentText() == "off")
   mySerialPort->setFlowControl(QSerialPort::NoFlowControl);
  else if (FlowCtrlCB->currentText() == "hardware")
   mySerialPort->setFlowControl(QSerialPort::HardwareControl);
  else if (FlowCtrlCB->currentText() == "xonxoff")
   mySerialPort->setFlowControl(QSerialPort::SoftwareControl);
  if (FlowCtrlCnt) {
   statusBar()->showMessage("FlowCtrl set successfully", 2000);
  }
  ++FlowCtrlCnt;
 }
}

void BasicTransceiver::resetCnt()
{
 BaudCnt = 0;
 ParityCnt = 0;
 DataBitsCnt = 0;
 StopBitsCnt = 0;
 FlowCtrlCnt = 0;
}

//打開和關(guān)閉串口
void BasicTransceiver::on_connectButton_toggled(bool checked)
{
 if (checked == true) {
  mySerialPort = new QSerialPort(this);
  QString tempStr = COMCB->currentText();
  tempStr.remove(" avail", Qt::CaseSensitive);
  mySerialPort->setPortName(tempStr);
  if (mySerialPort->open(QIODevice::ReadWrite)) {
    portIsOpen = true;
    emit setBaudRate();
    emit setParity();
    emit setDataBits();
    emit setStopBits();
    emit setFlowCtrl();
    statusBar()->showMessage(mySerialPort->portName() + " is opened", 2000);
    emit setComLabel();
    emit enabledSendButton();
    emit enabledAutoSend();
    COMCB->setEnabled(false);
    connect(Timer_CP, SIGNAL(timeout()), this, SLOT(checkPort()));
    Timer_CP->start(1000);
    connect(mySerialPort, SIGNAL(readyRead()), this, SLOT(readMyCom()));
    connectButton->setText(QString::fromLocal8Bit("關(guān)閉連接"));
    connectButton->setIcon(QIcon(":/images/close.png"));
   } else {
    QMessageBox::warning(this,
              QString::fromLocal8Bit("串口打開失敗"),
              QString::fromLocal8Bit("串口不存在或本串口"
                        "已經(jīng)被占用,請重試!"),
              QMessageBox::Cancel);
    connectButton->setChecked(false);
    return;
   }
 } else if (checked == false) {
   if (Timer_AS->isActive()) {
    Timer_AS->stop();
    emit resetAutoSendCB();
   }
   if (Timer_CP->isActive()) Timer_CP->stop();
   Timer_CP->disconnect();
   if (mySerialPort->isOpen()) mySerialPort->close();
   emit disabledSendButton();
   emit disabledAutoButton();
   emit setComLabel();
   emit setBaudLabel();
   resetCnt();
   COMCB->setEnabled(true);
   connectButton->setText(QString::fromLocal8Bit("打開連接"));
   connectButton->setIcon(QIcon(":/images/open.png"));
   portIsOpen = false;
 }
}

//設(shè)置顯示串口號和波特率的Label
void BasicTransceiver::setComLabel()
{
 if (mySerialPort->isOpen()) {
  comLabel->setText(QString(mySerialPort->portName()));
 } else if (!mySerialPort->isOpen()) {
  comLabel->setText(QString::fromLocal8Bit("COM:#"));
 }
}

void BasicTransceiver::setBaudLabel()
{
 if (mySerialPort->isOpen()) {
  int i_baud = mySerialPort->baudRate();
  QString s_baud;
  baudLabel->setText(s_baud.setNum(i_baud));
 } else if (!mySerialPort->isOpen()) {
  baudLabel->setText(QString::fromLocal8Bit("BAUD:#"));
 }
}

void BasicTransceiver::writeToBuf()
{
 QObject *signalSender = sender();
 if (signalSender->objectName() == "sendButton_1") {
  if (hexRB_1->isChecked()) {
   writeHex(senderTextEdit_1);
  } else {
   writeChr(senderTextEdit_1);
  }
 } else if (signalSender->objectName() == "sendButton_2") {
  if (hexRB_2->isChecked()) {
   writeHex(senderTextEdit_2);
  } else {
   writeChr(senderTextEdit_2);
  }
 } else if (signalSender->objectName() == "sendButton_3") {
  if (hexRB_3->isChecked()) {
   writeHex(senderTextEdit_3);
  } else {
   writeChr(senderTextEdit_3);
  }
 } else if (signalSender->objectName() == "sendButton_4") {
  if (hexRB_4->isChecked()) {
   writeHex(senderTextEdit_4);
  } else {
   writeChr(senderTextEdit_4);
  }
 } else if (signalSender->objectName() == "sendButton_5") {
  if (hexRB_5->isChecked()) {
   writeHex(senderTextEdit_5);
  } else {
   writeChr(senderTextEdit_5);
  }
 }
}

void BasicTransceiver::writeHex(QTextEdit *textEdit)
{
 QString dataStr = textEdit->toPlainText();
 //如果發(fā)送的數(shù)據(jù)個數(shù)為奇數(shù)的,則在前面最后落單的字符前添加一個字符0
 if (dataStr.length() % 2){
  dataStr.insert(dataStr.length()-1, '0');
 }
 QByteArray sendData;
 StringToHex(dataStr, sendData);
 mySerialPort->write(sendData);
 RxLCD->display(RxLCD->value() + sendData.size());
}

void BasicTransceiver::writeChr(QTextEdit *textEdit)
{
 QByteArray sendData = textEdit->toPlainText().toUtf8();
 if (!sendData.isEmpty() && !sendData.isNull()) {
  mySerialPort->write(sendData);
 }
 RxLCD->display(RxLCD->value() + sendData.size());
}

void BasicTransceiver::StringToHex(QString str, QByteArray &senddata) //字符串轉(zhuǎn)換為十六進制數(shù)據(jù)0-F
{
 int hexdata,lowhexdata;
 int hexdatalen = 0;
 int len = str.length();
 senddata.resize(len / 2);
 char lstr,hstr;
 for (int i = 0; i < len; ) {
  hstr = str[i].toLatin1();
  if (hstr == ' ') {
   ++i;
   continue;
  }
  ++i;
  if (i >= len) break;
  lstr = str[i].toLatin1();
  hexdata = ConvertHexChar(hstr);
  lowhexdata = ConvertHexChar(lstr);
  if ((hexdata == 16) || (lowhexdata == 16))
   break;
  else
   hexdata = hexdata*16 + lowhexdata;
  ++i;
  senddata[hexdatalen] = (char)hexdata;
  ++hexdatalen;
 }
 senddata.resize(hexdatalen);
}

char BasicTransceiver::ConvertHexChar(char ch)
{
 if ((ch >= '0') && (ch <= '9'))
  return ch - 0x30;
 else if ((ch >= 'A') && (ch <= 'F'))
  return ch - 'A' + 10;
 else if ((ch >= 'a') && (ch <= 'f'))
  return ch - 'a' + 10;
 else return ch - ch;
}

void BasicTransceiver::enabledSendButton()
{
 sendButton_1->setEnabled(true);
 sendButton_2->setEnabled(true);
 sendButton_3->setEnabled(true);
 sendButton_4->setEnabled(true);
 sendButton_5->setEnabled(true);
}

void BasicTransceiver::disabledSendButton()
{
 sendButton_1->setEnabled(false);
 sendButton_2->setEnabled(false);
 sendButton_3->setEnabled(false);
 sendButton_4->setEnabled(false);
 sendButton_5->setEnabled(false);
}

void BasicTransceiver::startAutoSend(QPushButton *sendButton)
{
 connect(Timer_AS, SIGNAL(timeout()), sendButton, SIGNAL(clicked()));
 QString interval;
 if (sendButton->objectName() == "sendButton_1") {
  disabledSendButton();
  Timer_AS->start(intervalSB_1->value());
  statusBar()->showMessage(QString::fromLocal8Bit("每 ") + interval.setNum(intervalSB_1->value())+ QString::fromLocal8Bit("ms 自動發(fā)送一次") , 2000);
 } else if (sendButton->objectName() == "sendButton_2") {
  disabledSendButton();
  Timer_AS->start(intervalSB_2->value());
  statusBar()->showMessage(QString::fromLocal8Bit("每 ") + interval.setNum(intervalSB_2->value())+ QString::fromLocal8Bit("ms 自動發(fā)送一次") , 2000);
 } else if (sendButton->objectName() == "sendButton_3") {
  disabledSendButton();
  Timer_AS->start(intervalSB_3->value());
  statusBar()->showMessage(QString::fromLocal8Bit("每 ") + interval.setNum(intervalSB_3->value())+ QString::fromLocal8Bit("ms 自動發(fā)送一次") , 2000);
 } else if (sendButton->objectName() == "sendButton_4") {
  disabledSendButton();
  Timer_AS->start(intervalSB_4->value());
  statusBar()->showMessage(QString::fromLocal8Bit("每 ") + interval.setNum(intervalSB_4->value())+ QString::fromLocal8Bit("ms 自動發(fā)送一次") , 2000);
 } else if (sendButton->objectName() == "sendButton_5") {
  disabledSendButton();
  Timer_AS->start(intervalSB_5->value());
  statusBar()->showMessage(QString::fromLocal8Bit("每 ") + interval.setNum(intervalSB_5->value())+ QString::fromLocal8Bit("ms 自動發(fā)送一次") , 2000);
 }
}

void BasicTransceiver::setConnections()
{
 connect(autoSendCB_1, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
 connect(autoSendCB_2, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
 connect(autoSendCB_3, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
 connect(autoSendCB_4, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
 connect(autoSendCB_5, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));

 connect(actionWrite_data, SIGNAL(triggered()), this, SLOT(saveAs()));
 connect(actionRead_data, SIGNAL(triggered()), this, SLOT(open()));
 connect(actionChoose_file, SIGNAL(triggered()), this, SLOT(open()));
 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
 connect(actionAbout, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

 connect(sendButton_1, SIGNAL(clicked()), this, SLOT(writeToBuf()));
 connect(sendButton_2, SIGNAL(clicked()), this, SLOT(writeToBuf()));
 connect(sendButton_3, SIGNAL(clicked()), this, SLOT(writeToBuf()));
 connect(sendButton_4, SIGNAL(clicked()), this, SLOT(writeToBuf()));
 connect(sendButton_5, SIGNAL(clicked()), this, SLOT(writeToBuf()));

 connect(BAUDCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setBaudRate()));
 connect(ParityCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setParity()));
 connect(DataBitsCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setDataBits()));
 connect(StopBitsCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setStopBits()));
 connect(FlowCtrlCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setFlowCtrl()));
}

void BasicTransceiver::enabledAutoSend()
{
 autoSendCB_1->setEnabled(true);
 autoSendCB_2->setEnabled(true);
 autoSendCB_3->setEnabled(true);
 autoSendCB_4->setEnabled(true);
 autoSendCB_5->setEnabled(true);
}

void BasicTransceiver::disabledAutoButton()
{
 autoSendCB_1->setEnabled(false);
 autoSendCB_2->setEnabled(false);
 autoSendCB_3->setEnabled(false);
 autoSendCB_4->setEnabled(false);
 autoSendCB_5->setEnabled(false);
}



void BasicTransceiver::resetAutoSendCB()
{
 autoSendCB_1->setChecked(false);
 autoSendCB_2->setChecked(false);
 autoSendCB_3->setChecked(false);
 autoSendCB_4->setChecked(false);
 autoSendCB_5->setChecked(false);
}

void BasicTransceiver::readMyCom()
{
 QByteArray temp = mySerialPort->readAll();
 QString buf;

 if (actionAlways_show->isChecked()) {
  if(!temp.isEmpty()){
    if(chrReceive->isChecked()){
     buf = temp;
    }else if(hexReceive->isChecked()){
     for(int i = 0; i < temp.count(); i++){
      QString s;
      s.sprintf("0x%02x, ", (unsigned char)temp.at(i));
      buf += s;
     }
    }
   receiveTextBrowser->setText(receiveTextBrowser->document()->toPlainText() + buf);
   receiveTextBrowser->moveCursor(QTextCursor::End);


   //ui->statusBar->showMessage(tr("成功讀取%1字節(jié)數(shù)據(jù)").arg(temp.size()));
  }
 }
 TxLCD->display(TxLCD->value() + temp.size());
}

//檢測可用串口并在可用串口號后面加上可用標志
void BasicTransceiver::checkAvailablePorts()
{//找不到存在串口是不會進入到foreach內(nèi)部的。。。存在不一定可用
 foreach ( const QSerialPortInfo &Info, QSerialPortInfo::availablePorts()) {
  QSerialPort availablePort;
  availablePort.setPortName(Info.portName());
  if (availablePort.open(QIODevice::ReadWrite)) {
   int index = COMCB->findText(Info.portName());
   COMCB->setItemText(index, Info.portName() + " avail");
   COMCB->setCurrentIndex(index);
   iVec.push_back(index);//將修改了內(nèi)容的項索引添加到容器中
   checkAPButton->setIcon(QIcon(":/images/find_it.png"));
   availablePort.close();
   }
 }
 if (iVec.size() == 0) {checkAPButton->setIcon(QIcon(":/images/find.png"));}
 QString availPortCnt;
 statusBar()->showMessage(availPortCnt.setNum(iVec.size()) + " available ports", 2000);
}
//將選擇串口號的checkBox重置并重新檢測可用串口
void BasicTransceiver::on_checkAPButton_clicked()
{
 if (!iVec.isEmpty()) {
  for (int i = 0; i != iVec.size(); ++i) {
   QString tempStr;
   COMCB->setItemText(iVec[i], QString("COM") +
               tempStr.setNum(iVec[i]));
  }
  COMCB->setCurrentIndex(0);
  iVec.clear();
 }
 emit checkAvailablePorts();
}

void BasicTransceiver::checkPort()
{
 QSet<QString> portSet;
 foreach ( const QSerialPortInfo &Info, QSerialPortInfo::availablePorts()) {
  portSet.insert(Info.portName());
 }
 if (portSet.find(mySerialPort->portName()) == portSet.end()) {
  QMessageBox::warning(this,
            QString::fromLocal8Bit("Application error"),
            QString::fromLocal8Bit("Fail with the following error : \n串口訪問失敗\n\nPort:%1")
            .arg(mySerialPort->portName()),
            QMessageBox::Close);
  emit on_connectButton_toggled(false);
 }
}

void BasicTransceiver::on_resetCntButton_clicked()
{
 RxLCD->display(0);
 TxLCD->display(0);
}

void BasicTransceiver::on_exitButton_clicked()
{
 qApp->quit();
}
//另存為
bool BasicTransceiver::saveAs()
{
 QString fileName = QFileDialog::getSaveFileName(this,
             tr("Save Data"), ".",
             tr("Text File (*.txt)"));
 if (fileName.isEmpty()) {
  return false;
 }
 return saveFile(fileName);
}
//保存文件
bool BasicTransceiver::saveFile(const QString &fileName)
{
 if (!writeFile(fileName)) {
  statusBar()->showMessage(tr("Saving canceled"), 2000);
  return false;
 }
 statusBar()->showMessage(tr("Data saved"), 2000);
 return true;
}

bool BasicTransceiver::writeFile(const QString &fileName)
{
 QFile file(fileName);
 if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
  QMessageBox::warning(this, tr("Save Data"),
              tr("Cannot write file %1 : \n%2")
              .arg(file.fileName())
              .arg(file.errorString()));
  return false;
 }
 QTextStream out(&file);
 out << receiveTextBrowser->toPlainText();
 return true;
}

//打開文件的函數(shù)
void BasicTransceiver::open()
{
 QString fileName = QFileDialog::getOpenFileName(this,
               tr("Choose Text File"), ".",
               tr("Text File (*.txt)"));
 if (!fileName.isEmpty()) {
  loadFile(fileName);
 }
}
//加載文件
bool BasicTransceiver::loadFile(const QString &fileName)
{
 if (!readFile(fileName)) {
   statusBar()->showMessage(tr("Loading canceled"), 2000);
   return false;
  }
  statusBar()->showMessage(tr("Data loaded"), 2000);
  return true;
}
//讀取文件
bool BasicTransceiver::readFile(const QString &fileName)
{
 QFile file(fileName);
  if (!file.open(QIODevice::ReadOnly)) {
   QMessageBox::warning(this, tr("Read failed"),
              tr("Cannot read file %1 : \n%2.")
              .arg(file.fileName())
              .arg(file.errorString()));
   return false;
  }
  QTextStream in(&file);
  QObject *signalSender = sender();
  if (signalSender->objectName() == "actionRead_data"){
   receiveTextBrowser->setText(in.readAll());
  } else if (signalSender->objectName() == "actionChoose_file") {
   senderTextEdit_1->setText(in.readAll());
  }
  return true;
}

void BasicTransceiver::dragEnterEvent(QDragEnterEvent *event)
{
 if (event->mimeData()->hasFormat("text/uri-list"))
   event->acceptProposedAction();
}

void BasicTransceiver::dropEvent(QDropEvent *event)
{
 QList<QUrl> urls = event->mimeData()->urls();
  if (urls.isEmpty())
   return;
  QString fileName = urls.first().toLocalFile();
  if (fileName.isEmpty()){
   return;
  }
  loadFile(fileName);
}

程序完全是面向?qū)ο蟮?,每個功能都是用相應(yīng)函數(shù)實現(xiàn)的,這也提高了函數(shù)的重用性。

本文是一個完整的Qt串口通信模塊QSerialPort開發(fā)完整實例,感興趣的可以細細閱讀源碼內(nèi)容,更多關(guān)于Qt串口通信知識請查看下面的相關(guān)鏈接

相關(guān)文章

  • C語言實現(xiàn)魔方陣算法(幻方陣 奇魔方 單偶魔方實現(xiàn))

    C語言實現(xiàn)魔方陣算法(幻方陣 奇魔方 單偶魔方實現(xiàn))

    魔方陣是指由1,2,3……n2填充的,每一行、每一列、對角線之和均相等的方陣,階數(shù)n = 3,4,5…。魔方陣也稱為幻方陣,看下面的實現(xiàn)方法吧
    2013-11-11
  • 詳解C語言中雙指針算法的使用

    詳解C語言中雙指針算法的使用

    雙指針,指的是在遍歷對象的過程中,不是普通的使用單個指針進行訪問,而是使用兩個相同方向(快慢指針)或者相反方向(對撞指針)的指針進行掃描,從而達到相應(yīng)的目的。本文將通過示例帶大家深入了解雙指針算法的使用
    2022-08-08
  • C語言題解Leetcode56合并區(qū)間實例

    C語言題解Leetcode56合并區(qū)間實例

    這篇文章主要為大家介紹了C語言題解Leetcode56合并區(qū)間實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • C語言入門篇--初識指針和指針變量

    C語言入門篇--初識指針和指針變量

    本篇文章是基礎(chǔ)篇,適合c語言剛?cè)腴T的朋友,本文對初識c語言的指針和指針變量做了簡單的分析,幫助大家快速入門c語言的世界,更好的理解c語言
    2021-08-08
  • C++模擬實現(xiàn)vector的示例代碼

    C++模擬實現(xiàn)vector的示例代碼

    大家在學習C++的時候一定會學到STL(標準模板庫),這是C++標準庫中最重要的組成部分,它包含了常用的數(shù)據(jù)結(jié)構(gòu)和算法。今天呢,我們首先來學習STL中的vector容器
    2022-09-09
  • C經(jīng)典冒泡排序法實現(xiàn)代碼

    C經(jīng)典冒泡排序法實現(xiàn)代碼

    這篇文章主要介紹了C經(jīng)典冒泡排序法實現(xiàn)代碼,需要的朋友可以參考下
    2014-02-02
  • C語言使用矩形法求定積分的通用函數(shù)

    C語言使用矩形法求定積分的通用函數(shù)

    這篇文章主要為大家詳細介紹了C語言使用矩形法求定積分的通用函數(shù),分別求解sinx, cosx,e^x,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++?STL標準庫之std::list使用介紹及用法詳解

    C++?STL標準庫之std::list使用介紹及用法詳解

    std::list是支持常數(shù)時間從容器任何位置插入和移除元素的容器,下面這篇文章主要給大家介紹了關(guān)于C++?STL標準庫之std::list使用介紹及用法詳解的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • C語言實現(xiàn)簡單猜數(shù)字游戲

    C語言實現(xiàn)簡單猜數(shù)字游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單猜數(shù)字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++之openFrameworks框架介紹

    C++之openFrameworks框架介紹

    本章我們將介紹一個非常好用的跨平臺的 C++開源框架 openFrameworks。它是一個開源的跨平臺的C++工具包,方便開發(fā)者創(chuàng)建出一個更簡單和直觀的框架,擅長開發(fā)圖像和動畫,感興趣的同學可以參考一下
    2023-05-05

最新評論