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

📄 qcppdialogimpl.cpp

📁 在linux下,应用QT开发平台,开发的串口通信程序,能接收,发送和存储数据,对于串口通信开发很有借签意义
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      QMessageBox::information(this, tr("Opening file failed"), tr("Protocol %1 not supported yet").arg(m_protoPb->currentText()));   }}void QCPPDialogImpl::killSz(){   if (m_sz==0)      return;   m_sz->tryTerminate();}void QCPPDialogImpl::readFromStdout(){   QByteArray ba=m_sz->readStdout();//   cerr<<"readFromStdout() "<<ba.count()<<endl;   unsigned int bytesToWrite=ba.count();   char* src=ba.data();   while (bytesToWrite>0)   {      int bytesWritten=::write(m_fd, src, (bytesToWrite>CUTECOMM_BUFSIZE?CUTECOMM_BUFSIZE:bytesToWrite));      if (bytesWritten<0)      {//         cerr<<"readFromStdout() error "<<bytesWritten<<" , "<<bytesToWrite<<" left"<<endl;         return;      }      src+=bytesWritten;      bytesToWrite-=bytesWritten;   }}void QCPPDialogImpl::readFromStderr(){   QByteArray ba=m_sz->readStderr();//   cerr<<"readFromStderr() "<<ba.count()<<endl;   if (m_progress==0)      return;   QString s(ba);   QRegExp regex(".+\\d+/ *(\\d+)k.*");   int pos=regex.search(s);   if (pos>-1)   {      QString captured=regex.cap(1);//      cerr<<"captured kb: -"<<captured.latin1()<<"-"<<endl;      int kb=captured.toUInt();      if ((kb%m_progressStepSize)==0)      {         int p=kb/m_progressStepSize;         if (p<100)            m_progress->setProgress(p);      }   }//   else//      cerr<<"--------"<<s.latin1()<<"-"<<endl;/*   for (unsigned int i=0; i<ba.count(); i++)   {      char c=ba.data()[i];      unsigned int tmp=c;      if (isprint(tmp))         cerr<<c;      else         fprintf(stderr, " \\0x%02x ", tmp&0xff);   }*/}void QCPPDialogImpl::sendDone(){   cerr<<"sx exited"<<endl;}bool QCPPDialogImpl::eventFilter(QObject* watched, QEvent *e){   QKeyEvent *ke=(QKeyEvent*)e;   if ((watched==m_cmdLe)       && (e->type()==QEvent::KeyPress))   {      if (ke->state()==0)      {         if (ke->key()==Key_Up)         {            prevCmd();            return true;         }         else if (ke->key()==Key_Down)         {            nextCmd();            return true;         }      }      else if (ke->state()==ControlButton)      {         if (ke->key()==Key_C)         {//            cerr<<"c";            m_keyCode=3;            sendByte(m_keyCode, 0);//            if (m_firstRep)               m_keyRepeatTimer.start(0, false);            return true;         }         else if (ke->key()==Key_Q)         {//            cerr<<"#";            m_keyCode=17;            sendByte(m_keyCode, 0);//            m_keyRepeatTimer.start(200, true);            return true;         }         else if (ke->key()==Key_S)         {            m_keyCode=19;            sendByte(m_keyCode, 0);//            m_keyRepeatTimer.start(200, true);            return true;         }      }   }//   else if ((watched==m_cmdLe) && (e->type()==QEvent::KeyRelease) && (ke->key()==Key_Control))   else if ((watched==m_cmdLe) && (e->type()==QEvent::KeyRelease))   {      m_keyRepeatTimer.stop();//      m_firstRep=true;   }   return false;}void QCPPDialogImpl::sendKey(){//   cerr<<"-";   sendByte(m_keyCode, 0);/*   if (m_firstRep)   {      m_firstRep=false;      m_keyRepeatTimer.start(1, false);   }*/}void QCPPDialogImpl::oldCmdClicked(QListBoxItem* item){   if (item==0)      return;   int index=m_oldCmdsLb->index(item);   m_cmdLe->setText(item->text());   m_cmdBufIndex=m_oldCmdsLb->count()-index;   m_cmdLe->setFocus();}void QCPPDialogImpl::prevCmd(){   if (m_oldCmdsLb->count()<=m_cmdBufIndex)      return;//   cerr<<"prevCmd() count: "<<m_oldCmdsLb->count()<<" bufIndex: "<<   m_cmdBufIndex++;   m_oldCmdsLb->setCurrentItem(m_oldCmdsLb->count()-m_cmdBufIndex);   m_oldCmdsLb->setSelected(m_oldCmdsLb->currentItem(), true);   m_cmdLe->setText(m_oldCmdsLb->currentText());//   cerr<<"prev() count: "<<m_oldCmdsLb->count()<<" bufIndex: "<<m_cmdBufIndex<<endl;}void QCPPDialogImpl::nextCmd(){   if (m_cmdBufIndex==0)      return;   m_cmdBufIndex--;   if (m_cmdBufIndex==0)   {      m_cmdLe->clear();      m_oldCmdsLb->setSelected(m_oldCmdsLb->currentItem(), false);   }   else   {      m_oldCmdsLb->setCurrentItem(m_oldCmdsLb->count()-m_cmdBufIndex);      m_cmdLe->setText(m_oldCmdsLb->currentText());   }//   cerr<<"next() count: "<<m_oldCmdsLb->count()<<" bufIndex: "<<m_cmdBufIndex<<endl;}void QCPPDialogImpl::execCmd(){   m_cmdBufIndex=0;   QString cmd=m_cmdLe->text().stripWhiteSpace();   m_cmdLe->clear();   if (!cmd.isEmpty())   {      if ((m_oldCmdsLb->count()<1) || (m_oldCmdsLb->text(m_oldCmdsLb->count()-1)!=cmd))      {         m_oldCmdsLb->insertItem(cmd);         m_oldCmdsLb->setCurrentItem(m_oldCmdsLb->count()-1);//       m_oldCmdsLb->setSelected(m_oldCmdsLb->currentItem(), false);         if (m_oldCmdsLb->count()>50)            m_oldCmdsLb->removeItem(0);         saveSettings();      }   }   m_oldCmdsLb->clearSelection();   if (m_fd==-1)      return;   sendString(cmd);/*   cerr<<"paras: "<<m_outputView->paragraphs()<<endl;   if (m_outputView->paragraphs()>1100)   {      m_outputView->setUpdatesEnabled(false);      m_outputView->setSelection(0, 0, 100, 0);      m_outputView->removeSelectedText();      m_outputView->scrollToBottom();      m_outputView->append("abc\n");      m_outputView->setUpdatesEnabled(true);   }   else*//*   m_outputView->setCursorPosition(m_outputView->paragraphs()-1, 0);   static int i=0;   i++;   QString s=QString::number(i);   s=" -"+s+"-\n ";   m_outputView->insert(s);*//*   m_outputView->insert("abc");   m_outputView->insert("def");   m_outputView->insert("ghi\n");   m_outputView->update();*/}bool QCPPDialogImpl::sendString(const QString& s){   unsigned int lineMode=m_inputModeCb->currentItem();   unsigned int charDelay=m_charDelaySb->value();   if (lineMode==4) // hex   {      QString hex=s;      hex.remove(QRegExp("\\s"));      if ((hex.startsWith("0x")) || (hex.startsWith("0X")))         hex=hex.mid(2);      if (hex.length()%2 != 0)         hex="0"+hex;      for (unsigned int i=0; i<hex.length()/2; i++)      {         QString nextByte=hex.mid(i*2, 2);         bool ok=true;         nextByte.toUInt(&ok, 16);         if (!ok)         {            QMessageBox::information(this, tr("Invalid hex characters"), tr("The input string contains invalid hex characters: 0x%1").arg(nextByte));            return false;         }      }      for (unsigned int i=0; i<hex.length()/2; i++)      {         QString nextByte=hex.mid(i*2, 2);         unsigned int byte=nextByte.toUInt(0, 16);         sendByte(byte & 0xff, charDelay);         fprintf(stderr, " 0x%x d:%d ", byte & 0xff, charDelay);      }      return true;   }   const char *bytes=s.latin1();   for (unsigned int i=0; i<s.length(); i++)   {      if (!sendByte(*bytes, charDelay))         return false;      bytes++;   }   if (lineMode==0)   {      if (!sendByte('\n', charDelay))         return false;   }   else if (lineMode==1)   {      if (!sendByte('\r', charDelay))         return false;   }   else if (lineMode==2)   {      if (!sendByte('\r', charDelay))         return false;      if (!sendByte('\n', charDelay))         return false;   }   return true;}bool QCPPDialogImpl::sendByte(char c, unsigned int delay){   if (m_fd==-1)      return false;//   c=c&0xff;   int res=::write(m_fd, &c, 1);//   cerr<<"wrote "<<(unsigned int)(c)<<endl;   if (res<1)   {      cerr<<"write returned "<<res<<" errno: "<<errno<<endl;      perror("write\n");      return false;   }   millisleep(delay);//   else//      cerr<<" \""<<c<<"\"; ";   return true;}/*void QCPPDialogImpl::connectOrDisconnectTTY(){   if (m_fd==-1)      connectTTY();   else      disconnectTTY();}*/void QCPPDialogImpl::connectTTY(){   QString dev=m_deviceCb->currentText();   int baudrate=m_baudCb->currentText().toInt();   int dataBits=m_dataBitsCb->currentText().toInt();   QString parity=m_parityCb->currentText();   QString stop=m_stopCb->currentText();   bool softwareHandshake=m_softwareCb->isChecked();   bool hardwareHandshake=m_hardwareCb->isChecked();//  m_fd=open(dev.latin1(), O_RDWR | O_NOCTTY | O_NONBLOCK);// m_fd=open(dev.latin1(), O_RDWR | O_NOCTTY);   int flags=0;   if (m_readCb->isChecked() && m_writeCb->isChecked())      flags=O_RDWR;   else if (!m_readCb->isChecked() && m_writeCb->isChecked())      flags=O_WRONLY;   else if (m_readCb->isChecked() && !m_writeCb->isChecked())      flags=O_RDONLY;   else   {      QMessageBox::information(this, tr("Error"), tr("Opening the device neither for reading nor writing doesn't seem to make much sense ;-)"));      return;   }   m_fd=open(dev.latin1(), flags | O_NDELAY);   if (m_fd<0)   {      cerr<<"opening failed"<<endl;      m_fd=-1;      QMessageBox::information(this, tr("Error"), tr("Could not open %1").arg(dev));      return;   }   if (m_applyCb->isChecked())   {      int n = fcntl(m_fd, F_GETFL, 0);      fcntl(m_fd, F_SETFL, n & ~O_NDELAY);      if (tcgetattr(m_fd, &m_oldtio)!=0)         cerr<<"tcgetattr() 2 failed"<<endl;      setNewOptions(baudrate, dataBits, parity, stop, softwareHandshake, hardwareHandshake);   }   m_connectPb->setEnabled(false);   m_deviceCb->setEnabled(false);   enableSettingWidgets(false);   m_applyCb->setEnabled(false);   m_readCb->setEnabled(false);   m_writeCb->setEnabled(false);   m_isConnected=true;   if (m_readCb->isChecked())   {      m_notifier=new QSocketNotifier(m_fd, QSocketNotifier::Read, this);      connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readData(int)));//      m_outputView->setEnabled(true);   }   m_oldCmdsLb->setEnabled(true);

⌨️ 快捷键说明

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