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

📄 commimainform.ui.h

📁 commi-0.3.2版本,linux下串口程序的源代码,类似minicom
💻 H
📖 第 1 页 / 共 2 页
字号:
bool CommiMainForm::sendByte(char c){    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;    }    //   else    //      cerr<<" \""<<c<<"\"; ";    return true;}void CommiMainForm::closeTTY(bool oldsettings) {    if(m_fd!=-1) {        if (oldsettings) tcsetattr(m_fd, TCSANOW, &m_oldtio);        ::close(m_fd); // close Port    }    m_fd=-1;    delete m_notifier;    m_notifier=0;}void CommiMainForm::connectionClose(){    connectionOpenAction->setEnabled(true);    connectionCloseAction->setEnabled(false);    transferSend_FileAction->setEnabled(false);    transferReceive_FileAction->setEnabled(false);    closeTTY(true);}void CommiMainForm::editOptions(){    QString str;    optionsForm *optForm = new optionsForm(this, "options", TRUE );    // general settings    optForm->fontBtn->setFont(textBrowser->font());    optForm->autosaveCB->setChecked(opt_autoreceive);    optForm->autosaveLEdit->setText(opt_autoreceivedir);    // old port settings    optForm->deviceNameComboBox->setCurrentText(opt_devicename);    optForm->writeCheckBox->setChecked(opt_write);    optForm->readCheckBox->setChecked(opt_read);    optForm->applyCheckBox->setChecked(opt_apply);    optForm->stopbitsComboBox->setCurrentText(opt_stop);    optForm->parityComboBox->setCurrentText(opt_parity);    str = str.setNum(opt_databits,10);    optForm->databitsComboBox->setCurrentText(str);    str = str.setNum(opt_baudrate,10);    optForm->baudrateComboBox->setCurrentText(str);    if(softwarehandshake && !hardwarehandshake) optForm->flowcontrolComboBox->setCurrentItem(1);    else if(!softwarehandshake && hardwarehandshake) optForm->flowcontrolComboBox->setCurrentItem(0);    else optForm->flowcontrolComboBox->setCurrentItem(2);  // Execute Options    if ( optForm->exec() ) { // general textBrowser->setFont(optForm->fontBtn->font()); opt_autoreceive = optForm->autosaveCB->isChecked(); opt_autoreceivedir = optForm->autosaveLEdit->text(); // port opt_devicename = optForm->deviceNameComboBox->currentText(); opt_write = optForm->writeCheckBox->isChecked(); opt_read = optForm->readCheckBox->isChecked(); opt_apply = optForm->applyCheckBox->isChecked(); opt_stop = optForm->stopbitsComboBox->currentText(); opt_parity = optForm->parityComboBox->currentText(); opt_databits = optForm->databitsComboBox->currentText().toInt(); opt_baudrate = optForm->baudrateComboBox->currentText().toInt(); switch(optForm->flowcontrolComboBox->currentItem()) { case 2:      softwarehandshake=false;     hardwarehandshake=false;     break; case 1:     softwarehandshake=true;     hardwarehandshake=false;     break; case 0:     softwarehandshake=false;     hardwarehandshake=true;break; } if(m_fd!=-1 && opt_apply) setNewOptions(opt_baudrate, opt_databits, opt_parity, opt_stop, softwarehandshake, hardwarehandshake);    }}void CommiMainForm::sendPlainFile(QString filename) {  m_sending=true;  QFile file(filename);  file.open(IO_ReadOnly);  QByteArray data=file.readAll();//  if(m_progress!=0) 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;  cerr << "test" << endl;  for (unsigned int i=0; i<data.size(); i++)  {      if ((i%step)==0)      {   m_progress->setProgress(i/step);   qApp->processEvents();      }      sendByte(data.data()[i]);//         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;      m_sending=false;}void CommiMainForm::sendFileSZ(QString filename, int mode) {    m_sending = true;    closeTTY(false);    m_sz=new QProcess(this);    m_sz->addArgument("sh");    m_sz->addArgument("-c");    QString tmp=QString("sz ");    switch(mode) {    case 0 :  tmp+="--xmodem "; break;case 1:    tmp+="--ymodem ";    break;case 2:    tmp+="--zmodem ";}    tmp=tmp+"-vv \""+filename+"\" < "+opt_devicename+" > "+opt_devicename;    m_sz->addArgument(tmp);    m_sz->setCommunication(QProcess::Stderr);    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;         openTTY(false);         m_sending=false;         return;      }      switch (mode) {   case 0: tmp="xmodem";       break;   case 1:       tmp="ymodem";       break;       case 2:    tmp="zmodem";}          m_progress=new QProgressDialog(tr("Sending file via %1...").arg(tmp), 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;            openTTY(false);    m_sending=false;}void CommiMainForm::killSz(){   if (m_sz==0)      return;   m_sz->tryTerminate();}void CommiMainForm::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>4096?4096:bytesToWrite));      if (bytesWritten<0)      {  //       cerr<<"readFromStdout() error "<<bytesWritten<<" , "<<bytesToWrite<<" left"<<endl;         return;      }      src+=bytesWritten;      bytesToWrite-=bytesWritten;   }}void CommiMainForm::readFromStderr(){   QByteArray ba=m_sz->readStderr();//   cerr<<"readFromStderr() "<<ba.count()<<endl;   if (m_progress==0)      return;   QString s(ba);  // cerr << "text : " << s << endl;      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 CommiMainForm::sendDone(){}void CommiMainForm::transferSend_FileAction_activated(){   // closeTTY(false);   transferForm *transForm = new transferForm(this, "transfer", true);   if(!transForm->exec()) return; // break if fails   if(transForm->protocolCB->currentItem()==3) {     sendPlainFile(transForm->fileNameLEdit->text());   }   else if(transForm->protocolCB->currentItem()>-1 && transForm->protocolCB->currentItem()<3) {       sendFileSZ(transForm->fileNameLEdit->text(),transForm->protocolCB->currentItem());   }   /*else if(transForm->protocolCB->currentItem()==3) {       sendKermitFile(transForm->fileNameLEdit->text());   }*/}void CommiMainForm::textBrowser_clicked( int, int ){    lineEdit->setFocus();}void CommiMainForm::editClearAction_activated(){    textBrowser->clear();}void CommiMainForm::helpAbout_QtAction_activated(){    QMessageBox::aboutQt( this, "Commi" );}void CommiMainForm::helpContents_activated(){     helpForm *hForm = new helpForm(this, "help", true);     hForm->exec();}void CommiMainForm::fileSave_asAction_activated(){    QString fn = QFileDialog::getSaveFileName( QString::null, "*", this );    QFile f ( fn);    if(f.exists() && QMessageBox::question(            this,            tr("Overwrite File?"),            tr("A file called %1 already exists."                "Do you want to overwrite it?")                .arg( fn ),            tr("&Yes"), tr("&No"),            QString::null, 0, 1 ) )        return;    if(!f.open(IO_WriteOnly )) { QMessageBox::critical(this,tr("I/O Error"),tr("Can't open %1 for writing").arg(fn));return;    }    QTextStream t(&f);    t << textBrowser->text();    f.close();    statusBar()->message(tr("Terminal content saved to %1 ...").arg(fn), 4000);}void CommiMainForm::receiveFileSZ( QString filename, int mode) {    m_sending = true;    closeTTY(false);    m_sz=new QProcess(this);    m_sz->addArgument("sh");    m_sz->addArgument("-c");    QString tmp=QString("export LANG=C && rb ");    switch(mode) {    case 0 :  tmp+="--xmodem " + filename; break;case 1:    tmp+="--ymodem ";    break;case 2:    tmp+="--zmodem ";}    if(mode==1 || mode==2) {    QDir dir(filename);  m_sz->setWorkingDirectory(dir);    }//    tmp=tmp+"-vv \""+filename+"\" < "+opt_devicename+" > "+opt_devicename;   tmp=tmp+" < "+opt_devicename+" > "+opt_devicename;    m_sz->addArgument(tmp);    m_sz->setCommunication(QProcess::Stderr);//|QProcess::Stdout);    connect(m_sz, SIGNAL(readyReadStderr()), this, SLOT(readFromStderrReceive()));      connect(m_sz, SIGNAL(readyReadStdout()), this, SLOT(readFromStderrReceive()));    connect(m_sz, SIGNAL(processExited()), this, SLOT(sendDone()));      if (!m_sz->start())      {         QMessageBox::information(this, tr("Comm error"), tr("Could not start rb"));         delete m_sz;         m_sz=0;         openTTY(false);         m_sending=false;         return;      }      switch (mode) {   case 0: tmp="xmodem";       break;   case 1:       tmp="ymodem";       break;       case 2:    tmp="zmodem";}    receiveblock=0;    receivemode = mode;      m_progress=new QProgressDialog(tr("Receiving file via %1...").arg(tmp), tr("Cancel"), 11, this, "progress", TRUE);      connect(m_progress, SIGNAL(cancelled()), this, SLOT(killSz()));      m_progress->setMinimumDuration(100);      QFileInfo fi(filename);      m_progressStepSize=10;//      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;     openTTY(false);    m_sending=false;/*  Exit Status    -128   Abort transfer  -128   Timeout   */    if(!m_sz->normalExit()) {  QMessageBox::critical(this,tr("Error"),tr("Aborted by user"));    }    else {    if(m_sz->exitStatus()==0) { switch(mode) { case 0:    QMessageBox::information(this,tr("Transer complete"),tr("File %1 receiced complete").arg(filename));     break; case 1: case 2:     QMessageBox::information(this,tr("Transfer complete"),tr("File %1 receiced complete").arg(receivefilename)); }    }    else QMessageBox::information(this,tr("Transfer error"), tr("Error while transfer file\nExit Status : %1").arg(m_sz->exitStatus()));}      delete m_sz;      m_sz=0;      delete m_progress;      m_progress=0;        }void CommiMainForm::readFromStderrReceive(){   QByteArray ba=m_sz->readStderr();   if (m_progress==0)      return;   QString s(ba);   s = s.stripWhiteSpace();   s = s.latin1();//   textBrowser->append("text:"+s+"\n");   //ba = m_sz->readStdout();    QRegExp regex("[a-z]: (\\d+)"); // Blocks received   QRegExp regex4("[a-z]: -(\\d+)"); // mistake   QRegExp regex2("[a-z]: +(\\d+)/ +(\\d+) +BPS:(\\d+) +ETA (\\d+):(\\d+)"); // zmodem   QRegExp regex3("[a-z]: +(\\d+)/ +(\\d+) +BPS:(\\d+)"); // ymodem   QRegExp regex5("[a-z]: +(\\d+) +BPS:(\\d+)"); // xmodem   int pos2 = regex2.search(s);   int pos3 = regex3.search(s);   int pos=regex.search(s);   int pos4 = regex4.search(s);   int pos5 = regex5.search(s);   QString t;   if(pos2>-1) {     m_progress->setLabelText(tr("BPS: %1  -  ETA : %2:%3\nFile: %4").arg(regex2.cap(3),regex2.cap(4),regex2.cap(5), receivefilename));       t = regex2.cap(2);       m_progress->setTotalSteps(t.toInt());       t = regex2.cap(1);       m_progress->setProgress(t.toInt());//       textBrowser->append("pos2\n");      }   else if (pos3>-1) {       t = regex3.cap(2);//       QMessageBox::information(this,"Received File",tr("Bytes %1\nFilename: %2").arg(t,receivefilename));   }   else if(pos4>-1) {     // do nothing ... is a mistake from rb-system     }   else if(pos5>-1) {       t = regex5.cap(1);//       textBrowser->append("received "+t+"Bytes\n");   }   else if (pos>-1)   {       do {    receiveblock++;    pos += regex.matchedLength();       }       while((pos=regex.search(s,pos)) != -1);       m_progress->setLabelText(tr("Received %1 blocks").arg(receiveblock));       if(m_progress->progress()<10) m_progress->setProgress(m_progress->progress()+1);       else m_progress->setProgress(0);//    textBrowser->append("pos1\n");   }   else if(s.contains("TIMEOUT"))    {       killSz(); // kill it when timeout       QMessageBox::critical(this,tr("Receiving File"),tr("Stop while TIMEOUT"));//       textBrowser->append("timeout\n");      }   else if(s.contains(":")) {      receivefilename=s.mid(s.find(":")+2);//      textBrowser->append("filename\n");      }   else {//      textBrowser->append("else\n");           }  textBrowser->setCursorPosition(textBrowser->paragraphs()-1, textBrowser->paragraphLength(textBrowser->paragraphs()-1));    }void CommiMainForm::transferReceive(){           transferForm *transForm = new transferForm(this, "transfer", true);    transForm->setReceive();   if(!transForm->exec()) return; // break if fails   if(transForm->protocolCB->currentItem()==3) {     cerr << "receive plain" << endl;   }   else {      receiveFileSZ( transForm->fileNameLEdit->text(),transForm->protocolCB->currentItem());   }}void CommiMainForm::editsearch(){    findDialog *fDialog = new findDialog(this, "find", TRUE );    fDialog->textB = textBrowser;    fDialog->exec();    /*cerr << "Paragraphs: " << textBrowser->paragraphs() << endl;    QString text;    text = textBrowser->text();    int strpos = 0;    strpos = text.find("an", 0, true);    cerr << "TextPos:" << strpos << endl;    int parag = 0;    for(int para=0; para<textBrowser->paragraphs(); para++) {	if(textBrowser->paragraphLength(para)<strpos) {	    //cerr << "Paragraph " << para << "Laenge " << textBrowser->paragraphLength(para) << endl;	    if(textBrowser->paragraphLength(para)==0) strpos--;	    else strpos-=textBrowser->paragraphLength(para);	    parag = para;	}	else break;    }    parag++;    cerr << "Parag" << parag << endl << "Strpos:" <<strpos << endl;//    parag=3;  //  strpos=0;    textBrowser->setSelection(parag, strpos, parag, strpos+2, 0);*/}void CommiMainForm::find( QString findtext ){    cerr << findtext.latin1()<< endl;}void CommiMainForm::sendKermitFile( QString filename ){}

⌨️ 快捷键说明

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