⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qcppdialogimpl.cpp

📁 在linux下,应用QT开发平台,开发的串口通信程序,能接收,发送和存储数据,对于串口通信开发很有借签意义
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*  Copyright (C) 2004-2005 Alexander Neundorf <neundorf@kde.org>    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/#include "qcppdialogimpl.h"#include <qcombobox.h>#include <qpushbutton.h>#include <qcheckbox.h>#include <qtextbrowser.h>#include <qlistbox.h>#include <qlineedit.h>#include <qfiledialog.h>#include <qdir.h>#include <qfile.h>#include <qmessagebox.h>#include <qsettings.h>#include <qevent.h>#include <qcstring.h>#include <qprogressdialog.h>#include <qapplication.h>#include <qprocess.h>#include <qfileinfo.h>#include <qregexp.h>#include <qspinbox.h>#include <iostream.h>#include <errno.h>#include <unistd.h>#include <ctype.h>#include <sys/ioctl.h>#include <sys/termios.h>#include <sys/time.h>#include <sys/types.h>#include <sys/select.h>#include <fcntl.h>void millisleep(int ms){   if (ms>0)   {      struct timeval tv;      tv.tv_sec=0;      tv.tv_usec=ms*1000;      select(0, 0, 0, 0, &tv);   }}QCPPDialogImpl::QCPPDialogImpl(QWidget* parent):CuteCommDlg(parent),m_isConnected(false),m_fd(-1),m_cmdBufIndex(0),m_notifier(0),m_sz(0),m_progress(0),m_progressStepSize(1000),m_fileDlg(0),m_outputTimer(this),m_keyRepeatTimer(this),m_keyCode(0)//,m_firstRep(true),m_hexBytes(0){   connect(m_connectPb, SIGNAL(clicked()), this, SLOT(connectTTY()));   connect(m_closePb, SIGNAL(clicked()), this, SLOT(disconnectTTY()));   connect(m_clearOutputPb, SIGNAL(clicked()), m_outputView, SLOT(clear()));//   connect(m_clearInputPb, SIGNAL(clicked()), m_oldCmdsLb, SLOT(clear()));   connect(m_cmdLe, SIGNAL(returnPressed()), this, SLOT(execCmd()));   connect(m_sendPb, SIGNAL(clicked()), this, SLOT(sendFile()));   connect(m_aboutPb, SIGNAL(clicked()), this, SLOT(showAboutMsg()));   connect(m_quitPb, SIGNAL(clicked()), this, SLOT(close()));   connect(m_oldCmdsLb, SIGNAL(clicked(QListBoxItem*)), this, SLOT(oldCmdClicked(QListBoxItem*)));   connect(m_oldCmdsLb, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(execCmd()));   connect(m_hexOutputCb, SIGNAL(toggled(bool)), this, SLOT(hexOutputClicked(bool)));   connect(m_connectPb, SIGNAL(clicked()), this, SLOT(saveSettings()));//   connect(m_deviceCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_baudCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_dataBitsCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_parityCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_stopCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_protoPb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_softwareCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_hardwareCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_readCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_writeCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_applyCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_hexOutputCb, SIGNAL(clicked()), this, SLOT(saveSettings()));   connect(m_inputModeCb, SIGNAL(activated(int)), this, SLOT(saveSettings()));   connect(m_charDelaySb, SIGNAL(valueChanged(int)), this, SLOT(saveSettings()));   connect(m_applyCb, SIGNAL(toggled(bool)), this, SLOT(enableSettingWidgets(bool)));   connect(&m_outputTimer, SIGNAL(timeout()), this, SLOT(doOutput()));   connect(&m_keyRepeatTimer, SIGNAL(timeout()), this, SLOT(sendKey()));   m_outputView->setWrapPolicy(QTextEdit::Anywhere);   m_outputView->setWordWrap(QTextEdit::WidgetWidth);/*   QAccel* accel=new QAccel(this);   accel->insertItem(CTRL+Key_C, 3);   accel->insertItem(CTRL+Key_Q, 17);   accel->insertItem(CTRL+Key_S, 19);   connect(accel, SIGNAL(activated(int)), this, SLOT(sendByte(int)));*/   m_outputTimerStart.start();   readSettings();   disconnectTTY();   m_cmdLe->installEventFilter(this);}void QCPPDialogImpl::resizeEvent(QResizeEvent *e){   QWidget::resizeEvent(e);   saveSettings();}void QCPPDialogImpl::saveSettings(){   QSettings settings;   settings.writeEntry("/cutecom/HardwareHandshake", m_hardwareCb->isChecked());   settings.writeEntry("/cutecom/SoftwareHandshake", m_softwareCb->isChecked());   settings.writeEntry("/cutecom/OpenForReading", m_readCb->isChecked());   settings.writeEntry("/cutecom/OpenForWriting", m_writeCb->isChecked());   settings.writeEntry("/cutecom/DontApplySettings", !m_applyCb->isChecked());   //   settings.writeEntry("/cutecom/Device", m_deviceCb->currentItem());   settings.writeEntry("/cutecom/Baud", m_baudCb->currentItem());   settings.writeEntry("/cutecom/Databits", m_dataBitsCb->currentItem());   settings.writeEntry("/cutecom/Parity", m_parityCb->currentItem());   settings.writeEntry("/cutecom/Stopbits", m_stopCb->currentItem());   settings.writeEntry("/cutecom/Protocol", m_protoPb->currentItem());   settings.writeEntry("/cutecom/width", width());   settings.writeEntry("/cutecom/height", height());   settings.writeEntry("/cutecom/LineMode", m_inputModeCb->currentItem());   settings.writeEntry("/cutecom/HexOutput", m_hexOutputCb->isChecked());   settings.writeEntry("/cutecom/CharDelay", m_charDelaySb->value());   QString currentDevice=m_deviceCb->currentText();   settings.writeEntry("/cutecom/CurrentDevice", currentDevice);   bool currentDeviceIsInList=false;   QStringList devices;   for (int i=0; i<m_deviceCb->count(); i++)   {      QString s=m_deviceCb->text(i);      devices<<s;      if (s==currentDevice)         currentDeviceIsInList=true;   }   if (!currentDeviceIsInList)      devices<<currentDevice;   settings.writeEntry("/cutecom/AllDevices", devices);   int historyCount=m_oldCmdsLb->count();   if (historyCount>50)      historyCount=50;   QStringList saveHist;   for (int i=m_oldCmdsLb->count()-historyCount; i<m_oldCmdsLb->count(); i++)      saveHist<< m_oldCmdsLb->text(i);   settings.writeEntry("/cutecom/History", saveHist);}void QCPPDialogImpl::readSettings(){   QSettings settings;   m_hardwareCb->setChecked(settings.readBoolEntry("/cutecom/HardwareHandshake", false));   m_softwareCb->setChecked(settings.readBoolEntry("/cutecom/SoftwareHandshake", false));   m_readCb->setChecked(settings.readBoolEntry("/cutecom/OpenForReading", true));   m_writeCb->setChecked(settings.readBoolEntry("/cutecom/OpenForWriting", true));   m_applyCb->setChecked(!settings.readBoolEntry("/cutecom/DontApplySettings", false));   enableSettingWidgets(m_applyCb->isChecked());   m_baudCb->setCurrentItem(settings.readNumEntry("/cutecom/Baud", 7));   m_dataBitsCb->setCurrentItem(settings.readNumEntry("/cutecom/Databits", 3));   m_parityCb->setCurrentItem(settings.readNumEntry("/cutecom/Parity", 0));   m_stopCb->setCurrentItem(settings.readNumEntry("/cutecom/Stopbits", 0));   m_protoPb->setCurrentItem(settings.readNumEntry("/cutecom/Protocol", 0));   m_inputModeCb->setCurrentItem(settings.readNumEntry("/cutecom/LineMode", 0));   m_hexOutputCb->setChecked(settings.readBoolEntry("/cutecom/HexOutput", false));   m_charDelaySb->setValue(settings.readNumEntry("/cutecom/CharDelay", 1));   int x=settings.readNumEntry("/cutecom/width", -1);   int y=settings.readNumEntry("/cutecom/height", -1);   if ((x>100) && (y>100))      resize(x,y);   bool entryFound=false;   QStringList devices=settings.readListEntry("/cutecom/AllDevices", &entryFound);   if (!entryFound)      devices<<"/dev/ttyS0"<<"/dev/ttyS1"<<"/dev/ttyS2"<<"/dev/ttyS3";   m_deviceCb->insertStringList(devices);   m_deviceCb->setCurrentText(settings.readEntry("/cutecom/CurrentDevice", "/dev/ttyS0"));   QStringList history=settings.readListEntry("/cutecom/History");   if (!history.empty())   {      m_oldCmdsLb->insertStringList(history);      m_oldCmdsLb->setCurrentItem(m_oldCmdsLb->count()-1);      m_oldCmdsLb->ensureCurrentVisible();      m_oldCmdsLb->clearSelection();   }}void QCPPDialogImpl::showAboutMsg(){   QMessageBox::about(this, tr("About CuteCom"), tr("This is CuteCom 0.13.2<br>(c)2004 Alexander Neundorf, &lt;neundorf@kde.org&gt;<br>Licensed under the GNU GPL v2"));}void QCPPDialogImpl::sendFile(){   if (m_fileDlg==0)   {      m_fileDlg=new QFileDialog(QDir::homeDirPath());      m_fileDlg->setMode(QFileDialog::ExistingFile);   }   QString filename;   if ( m_fileDlg->exec() == QDialog::Accepted )      filename = m_fileDlg->selectedFile();   else      return;   unsigned int charDelay=m_charDelaySb->value();   if (m_protoPb->currentText()=="Script")   {      QFile file(filename);      if (!file.open(IO_ReadOnly))      {         QMessageBox::information(this, tr("Opening file failed"), tr("Could not open file %1").arg(filename));         return;      }      QTextStream stream(&file);      while (!stream.atEnd())      {         QString nextLine=stream.readLine();         nextLine=nextLine.left((unsigned int)nextLine.find('#'));         if (nextLine.isEmpty())            continue;         if (!sendString(nextLine))         {            QMessageBox::information(this, tr("Comm error"), tr("Sending failed"));            return;         }         millisleep(charDelay*3);      }   }   else if (m_protoPb->currentText()=="Plain")   {      QFile file(filename);      if (!file.open(IO_ReadOnly))      {         QMessageBox::information(this, tr("Opening file failed"), tr("Could not open file %1").arg(filename));         return;      }      QByteArray data=file.readAll();      delete m_progress;      m_progress=new QProgressDialog(tr("Sending file..."), tr("Cancel"), 100, this, "progress", TRUE);      m_progress->setMinimumDuration(100);      unsigned int step=data.size()/100;      if (step<1)         step=1;      for (unsigned int i=0; i<data.size(); i++)      {         if ((i%step)==0)         {            m_progress->setProgress(i/step);            qApp->processEvents();         }         sendByte(data.data()[i], charDelay);         if ((data.data()[i]=='\n') || (data.data()[i]=='\r')) //wait twice as long after bytes which might be line ends (helps some uCs)            millisleep(charDelay);//         if (!sendByte(data.data()[i]))         if (0)         {            QMessageBox::information(this, tr("Comm error"), tr("Sending failed (%1/%2").arg(i).arg(data.count()));            break;         }         if ( m_progress->wasCancelled() )            break;      }      delete m_progress;      m_progress=0;   }   else if ((m_protoPb->currentText()=="XModem")            || (m_protoPb->currentText()=="YModem")            || (m_protoPb->currentText()=="ZModem"))   {//      QProcess sx(this);      disconnectTTYRestore(false);      m_sz=new QProcess(this);      m_sz->addArgument("sh");      m_sz->addArgument("-c");//      QString tmp=QString("sx -vv \"")+filename+"\" < "+m_deviceCb->currentText()+" > "+m_deviceCb->currentText();      QString tmp=QString("sz ");      if (m_protoPb->currentText()=="XModem")         tmp+="--xmodem ";      else if (m_protoPb->currentText()=="YModem")         tmp+="--ymodem ";      else if (m_protoPb->currentText()=="ZModem")         tmp+="--zmodem ";      tmp=tmp+"-vv \""+filename+"\" < "+m_deviceCb->currentText()+" > "+m_deviceCb->currentText();      m_sz->addArgument(tmp);      m_sz->setCommunication(QProcess::Stderr);      connect(m_sz, SIGNAL(readyReadStderr()), this, SLOT(readFromStderr()));/*      m_sz->addArgument("sx");      m_sz->addArgument("-vv");      m_sz->addArgument(filename);      m_sz->setCommunication(QProcess::Stdin|QProcess::Stdout|QProcess::Stderr);      connect(m_sz, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()));      connect(m_sz, SIGNAL(readyReadStderr()), this, SLOT(readFromStderr()));*/      connect(m_sz, SIGNAL(processExited()), this, SLOT(sendDone()));      if (!m_sz->start())      {         QMessageBox::information(this, tr("Comm error"), tr("Could not start sx"));         delete m_sz;         m_sz=0;         connectTTY();         return;      }      m_progress=new QProgressDialog(tr("Sending file via xmodem..."), tr("Cancel"), 100, this, "progress", TRUE);      connect(m_progress, SIGNAL(cancelled()), this, SLOT(killSz()));      m_progress->setMinimumDuration(100);      QFileInfo fi(filename);      m_progressStepSize=fi.size()/1024/100;      if (m_progressStepSize<1)         m_progressStepSize=1;//    cerr<<"while(isRunning)"<<endl;      m_progress->setProgress(0);      while (m_sz->isRunning())      {         qApp->processEvents();         millisleep(10);      }//      cerr<<"----------------- sx done"<<endl;      delete m_sz;      m_sz=0;      delete m_progress;      m_progress=0;      connectTTY();   }   else   {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -