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

📄 commimainform.ui.h

📁 commi-0.3.2版本,linux下串口程序的源代码,类似minicom
💻 H
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** ui.h extension file, included from the uic-generated form implementation.**** If you want to add, delete, or rename functions or slots, use** Qt Designer to update this file, preserving your code.**** You should not define a constructor or destructor in this file.** Instead, write your code in functions called init() and destroy().** These will automatically be called by the form's constructor and** destructor.*****************************************************************************/#include <sys/ioctl.h>#include <sys/termios.h>#include <sys/time.h>#include <sys/types.h>#include <sys/select.h>#include <fcntl.h>#include <errno.h>#include <unistd.h>#include <ctype.h>#include <iostream>#define debug_receiveusing std::cerr; using std::endl;void millisleep(int ms){    struct timeval tv;    tv.tv_sec=0;    tv.tv_usec=ms*1000;    select(0, 0, 0, 0, &tv);}void CommiMainForm::timerDone() {  timer->stop();    //cerr << "Timer Done !!!" <<endl;}void CommiMainForm::sleep(int ms) {      timer = new QTimer(this);      connect( timer, SIGNAL(timeout()), this, SLOT(timerDone()) );      timer->start(ms,true);      while(timer->isActive()) {          qApp->processEvents();        }      delete timer;}void CommiMainForm::fileOpen(){    QString filename = QFileDialog::getOpenFileName(                    "",                    tr("Commi Batch Files (*.commi)"),                    this,                    tr("open file dialog"),                    tr("Choose a file") );    QFile file(filename);    if(file.open(IO_ReadOnly)) { QTextStream stream( &file ); QString line; int sleeptime; int fd = -1;             while ( !stream.atEnd() ) {     line = stream.readLine();       line = line.simplifyWhiteSpace();     cerr << line << endl;     if(line.startsWith("!!"))      { // Command         if(line.startsWith("!!sleep")) { // wait x ms for actions            line = line.mid(8);            sleeptime = line.toInt();            sleep(sleeptime);         }         else if(line.startsWith("!!openPort")) { // open IO-Port             connectionOpen();         }         else if(line.startsWith("!!closePort")) { // close IO-Port             connectionClose();         }         else if(line.startsWith("!!send")) { // send a file             if(line.startsWith("!!send xmodem")) {                 fd = ::open(line.mid(14),O_RDONLY);                 if(fd!=-1) {                   ::close(fd);                   sendFileSZ(line.mid(14),0);                   }                 else cerr << tr("Can't open file %1").arg(line.mid(14)) << endl;             }             else if(line.startsWith("!!send ymodem")) {                 fd = ::open(line.mid(14),O_RDONLY);                 if(fd!=-1) {                   ::close(fd);                   sendFileSZ(line.mid(14),1);                   }                 else cerr << tr("Can't open file %1").arg(line.mid(14)) << endl;             }             else if(line.startsWith("!!send zmodem")) {                 fd = ::open(line.mid(14),O_RDONLY);                 if(fd!=-1) {                   ::close(fd);                   sendFileSZ(line.mid(14),2);                   }                 else cerr << tr("Can't open file %1").arg(line.mid(14)) << endl;             }             else if(line.startsWith("!!send plain")) {                 fd = ::open(line.mid(13),O_RDONLY);                 if(fd!=-1) {                   ::close(fd);                   sendFileSZ(line.mid(13),0);                   }                 else cerr << tr("Can't open file %1").arg(line.mid(13)) << endl;             }                  }     }     else      {          sendString(line);      } }    }    else { if(filename!="") QMessageBox::critical(this,tr("Open File Error"), tr("Can't open file \"%1\" !!").arg(filename));}}void CommiMainForm::saveSettings() {    QSettings settings;    settings.writeEntry("/commi/device/DeviceName",opt_devicename);    settings.writeEntry("/commi/device/Baudrate",opt_baudrate);    settings.writeEntry("/commi/device/DataBits",opt_databits);    settings.writeEntry("/commi/device/Parity",opt_parity);    settings.writeEntry("/commi/device/StopBits",opt_stop);    settings.writeEntry("/commi/device/SoftwareFlowControl",softwarehandshake);    settings.writeEntry("/commi/device/HardwareFlowControl",hardwarehandshake);    settings.writeEntry("/commi/device/OpenRead",opt_read);    settings.writeEntry("/commi/device/OpenWrite",opt_write);    settings.writeEntry("/commi/device/ApplySettings",opt_apply);    settings.writeEntry("/commi/TerminalFont",textBrowser->font().toString());    settings.writeEntry("/commi/Width",width());    settings.writeEntry("/commi/Height",height());    settings.writeEntry("/commi/Top",x());    settings.writeEntry("/commi/Left",y());    settings.writeEntry("/commi/AutoReceiveZModem",opt_autoreceive);    settings.writeEntry("/commi/AutoReceiveDirectory",opt_autoreceivedir);}void CommiMainForm::readSettings() {    QFont *font = new QFont();    QSettings settings;    int x,y, w, h;    // general    font->fromString(settings.readEntry("/commi/TerminalFont","Helvetica"));    textBrowser->setFont(*font);    x = (settings.readNumEntry("/commi/Top",50));    y = (settings.readNumEntry("/commi/Left",50));    w = (settings.readNumEntry("/commi/Width",550));    h = (settings.readNumEntry("/commi/Height",450));    opt_autoreceive = settings.readBoolEntry("/commi/AutoReceiveZModem",true);    opt_autoreceivedir = settings.readEntry("/commi/AutoReceiveDirectory","");    resize(w,h);    move (x,y);    // Device    opt_devicename=settings.readEntry("/commi/device/DeviceName","/dev/ttyS0");    opt_baudrate = settings.readNumEntry("/commi/device/Baudrate",38400);    opt_databits = settings.readNumEntry("/commi/device/DataBits",8);    opt_parity = settings.readEntry("/commi/device/Parity","None");    opt_stop = settings.readEntry("/commi/device/StopBits","1");    softwarehandshake = settings.readBoolEntry("/commi/device/SoftwareFlowControl",false);    hardwarehandshake = settings.readBoolEntry("/commi/device/HardwareFlowControl",false);    opt_read = settings.readBoolEntry("/commi/device/OpenRead",true);    opt_write = settings.readBoolEntry("/commi/device/OpenWrite",false);    opt_apply = settings.readBoolEntry("/commi/device/ApplySettings",true);}void CommiMainForm::closeEvent( QCloseEvent * ){    fileExit();}void CommiMainForm::fileExit(){    saveSettings();    QApplication::exit( 0 );}void CommiMainForm::editCopy(){//    clipboard->setText ( textBrowser->text());    textBrowser->copy();}void CommiMainForm::editPaste(){    lineEdit->setText( clipboard->text() );}void CommiMainForm::helpAbout(){    aboutForm *aForm = new aboutForm(this, "about", TRUE );    aForm->exec();    }void CommiMainForm::init(){    clipboard = QApplication::clipboard();    m_fd=-1;    if ( clipboard->supportsSelection() ) clipboard->setSelectionMode( TRUE );    readSettings();    m_sending=false;    helpContentsAction->setAccel( QKeySequence( tr("F1")));    lineEdit->setFocus();}void CommiMainForm::textChange( const QString &test){    QMessageBox::about(this, tr("About"), test );}void CommiMainForm::sendCommand(){    //QMessageBox::about(this, tr("About"), tr(lineEdit->command()) );    if (m_fd==-1) return;    if(lineEdit->command() == "!ctrl_c") sendByte(3);    else if(lineEdit->command() == "!ctrl_q") sendByte(17);    else if(lineEdit->command() == "!ctrl_s") sendByte(19);    else sendString(lineEdit->command());}/** This function features some code from minicom 2.0.0, src/sysdep1.c */void CommiMainForm::setNewOptions(int baudrate, int databits, const QString& parity, const QString& stop, bool softwareHandshake, bool hardwareHandshake){    struct termios newtio;    //   memset(&newtio, 0, sizeof(newtio));    if (tcgetattr(m_fd, &newtio)!=0) cerr<<"tcgetattr() 3 failed"<<endl;        /*{      unsigned int i;      char *c =(char*)&newtio;      fprintf(stderr,"*****************\n");      for (i=0; i<sizeof(newtio); i++)      {  unsigned int t=*c;  if (i%8 == 0)     fprintf(stderr,"\n");  fprintf(stderr, " 0x%02x", t&0xff);  c++;      }   }*/            speed_t _baud=0;    switch (baudrate)    {    case 600: _baud=B600; break;    case 1200: _baud=B1200; break;    case 2400: _baud=B2400; break;    case 4800: _baud=B4800; break;    case 9600: _baud=B9600; break; //   case 14400: //      _baud=B14400; //      break;    case 19200: _baud=B19200; break; //   case 28800: //      _baud=B28800; //      break;    case 38400: _baud=B38400; break; //   case 56000: //      _baud=B56000; //      break;    case 57600: _baud=B57600; break;    case 115200: _baud=B115200; break;    case 230400: _baud=B230400; break;    case 460800: _baud=B460800; break;    case 576000: _baud=B576000; break;    case 921600: _baud=B921600; break; //   case 128000: //      _baud=B128000; //      break;    default: //   case 256000: //      _baud=B256000; break;    }    cfsetospeed(&newtio, (speed_t)_baud);    cfsetispeed(&newtio, (speed_t)_baud);        /* We generate mark and space parity ourself. */    if (databits == 7 && (parity=="Mark" || parity == "Space")) databits = 8;    switch (databits)    {    case 5: newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS5; break;    case 6: newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS6; break;    case 7: newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS7; break;    case 8:    default: newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS8; break;    }    newtio.c_cflag |= CLOCAL | CREAD;        //parity    newtio.c_cflag &= ~(PARENB | PARODD);    if (parity == "Even") newtio.c_cflag |= PARENB;    else if (parity== "Odd") newtio.c_cflag |= (PARENB | PARODD);        //hardware handshake    /*   if (hardwareHandshake)      newtio.c_cflag |= CRTSCTS;   else      newtio.c_cflag &= ~CRTSCTS;*/    newtio.c_cflag &= ~CRTSCTS;        //stopbits    if (stop=="2") newtio.c_cflag |= CSTOPB;    else newtio.c_cflag &= ~CSTOPB;        //   newtio.c_iflag=IGNPAR | IGNBRK;    newtio.c_iflag=IGNBRK;    //   newtio.c_iflag=IGNPAR;        //software handshake    if (softwareHandshake) newtio.c_iflag |= IXON | IXOFF;    else newtio.c_iflag &= ~(IXON|IXOFF|IXANY);        newtio.c_lflag=0;        newtio.c_oflag=0;            newtio.c_cc[VTIME]=2;    newtio.c_cc[VMIN]=60;        //   tcflush(m_fd, TCIFLUSH);    if (tcsetattr(m_fd, TCSANOW, &newtio)!=0) cerr<<"tcsetattr() 1 failed"<<endl;        int mcs=0;    //   ioctl(m_fd, TIOCMODG, &mcs);    ioctl(m_fd, TIOCMGET, &mcs);    mcs |= TIOCM_RTS;    ioctl(m_fd, TIOCMSET, &mcs);        if (tcgetattr(m_fd, &newtio)!=0) cerr<<"tcgetattr() 4 failed"<<endl;    //hardware handshake    if (hardwareHandshake) newtio.c_cflag |= CRTSCTS;    else newtio.c_cflag &= ~CRTSCTS;    /*  if (on)     newtio.c_cflag |= CRTSCTS;  else     newtio.c_cflag &= ~CRTSCTS;*/    if (tcsetattr(m_fd, TCSANOW, &newtio)!=0) cerr<<"tcsetattr() 2 failed"<<endl;        /*{      unsigned int i;      char *c =(char*)&newtio;      fprintf(stderr,"-----------------\n");      tcgetattr(m_fd, &newtio);      for (i=0; i<sizeof(newtio); i++)      {  unsigned int t=*c;  if (i%8 == 0)     fprintf(stderr,"\n");  fprintf(stderr, " 0x%02x", t&0xff);  c++;      }   }*/}void CommiMainForm::connectionOpen(){    openTTY(true);      if(m_fd!=-1) {      connectionOpenAction->setEnabled(false);      connectionCloseAction->setEnabled(true);      transferSend_FileAction->setEnabled(true);      transferReceive_FileAction->setEnabled(true);    }}void CommiMainForm::openTTY( bool oldsettings) {    int flags=0;    if (opt_read && opt_write) flags=O_RDWR;    else if (!opt_read && opt_write) flags=O_WRONLY;    else if (opt_read && !opt_write) 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(opt_devicename.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(opt_devicename)); return;    }    if (opt_apply)    {        int n = fcntl(m_fd, F_GETFL, 0);        fcntl(m_fd, F_SETFL, n & ~O_NDELAY);         if (oldsettings && tcgetattr(m_fd, &m_oldtio)!=0)            cerr<<"tcgetattr() 2 failed"<<endl;        setNewOptions(opt_baudrate, opt_databits, opt_parity, opt_stop, softwarehandshake, hardwarehandshake);    }        if (opt_read)   {    m_notifier=new QSocketNotifier(m_fd, QSocketNotifier::Read, this);    connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readData(int)));   }}void CommiMainForm::readData( int fd ){    if (fd!=m_fd) return;    int bytesRead=::read(m_fd, m_buf, 4096);        if (bytesRead<0)    { cerr<<"read result: "<<bytesRead<<endl; perror("read: \n"); return;    }    const char* c=m_buf;    QString text;    for (int i=0; i<bytesRead; i++)    { if ((isprint(*c)) || (*c=='\n') || (*c=='\r')) {     if (*c!='\r')  text+=(*c); } else if(*c==0x07) { // Bell     // TODO or not .. It is a pain in my neck. } else if(*c==0x1B) { // control char     c++;     i++;     if(*c=='[') {	 c++;	 i++;	 if(*c=='2') {	     c++;i++;	     if(*c=='J') { // 		 editClearAction_activated();	     }	     else { c--;i--; }	 }	 else { c--;i--; }     }     else {	 c--;i--;	 text.append("ESC");     } } else {     char buf[16];     unsigned int zw=*c;     snprintf(buf, 16, "\\0x%02x", zw & 0xff);     text+=buf; } c++;    } //    cerr << text.latin1() << endl;    if(text.startsWith("rz**") && !text.endsWith("\n") && opt_autoreceive) { receiveFileSZ(opt_autoreceivedir,2);    }    else { textBrowser->setCursorPosition(textBrowser->paragraphs()-1, textBrowser->paragraphLength(textBrowser->paragraphs()-1));      textBrowser->insert(text);   }}bool CommiMainForm::sendString(const QString& s){    const char *bytes=s.latin1();    for (unsigned int i=0; i<s.length(); i++)    { if (!sendByte(*bytes))     return false; bytes++; millisleep(1);    }    if (!sendByte('\n')) return false;    return true;}

⌨️ 快捷键说明

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