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

📄 serialthread.cpp

📁 基于qt的串口多线程编程
💻 CPP
字号:
#include <sys/types.h>     #include <sys/stat.h>     #include <fcntl.h>     #include <termios.h>     #include <stdio.h>  #include "my_define.h"#include "serialthread.h"#include "mainwindow.h"   int set_nc_mode(int fd){    struct termios options;    if  ( tcgetattr( fd,&options)  !=  0){          perror("SetupSerial 1");          return(FALSE);      } /* get current port settings */      bzero(&options, sizeof(options));       options.c_cflag |= BAUDRATE | CS8 | CLOCAL | CREAD;     options.c_cflag &= ~CRTSCTS;     options.c_iflag = IGNPAR;       options.c_oflag &=~OPOST;                   //       options.c_lflag = 0;                    options.c_cc[VTIME] = WAIT_TIME;    options.c_cc[VMIN] = BLOCK_SIZE;    /* blocking read until 5 chars received */          tcflush(fd, TCIFLUSH);      tcsetattr(fd,TCSANOW,&options);    return(TRUE);} int set_c_mode(int fd){    struct termios options;    if  ( tcgetattr( fd,&options)  !=  0){          perror("SetupSerial 1");          return(FALSE);      }     bzero(&options, sizeof(options));    tcflush(fd, TCIOFLUSH);    cfsetispeed(&options, BAUDRATE);        cfsetospeed(&options, BAUDRATE);     options.c_cflag |=(CLOCAL|CREAD);     options.c_cflag &= ~CRTSCTS;     options.c_cflag &= ~CSIZE;    options.c_cflag |= CS8;     options.c_cflag &= ~PARENB;   /* Clear parity enable,clear control mode flag */    options.c_iflag &= ~INPCK;     /* Disable parity checking ,*/    options.c_cflag &= ~CSTOPB;     options.c_iflag |= IGNBRK;     options.c_lflag |= ICANON;             options.c_lflag &= ~(ECHO | ECHOE | ISIG);    options.c_oflag &= ~(OPOST);      tcflush(fd, TCIOFLUSH);      if (tcsetattr(fd,TCSANOW,&options) != 0){          perror("SetupSerial 3");        return (FALSE);    }    return(TRUE);}void send_ack(int fd){    char buf[]={'A','C','K',CHANGE_LINE};    write(fd,buf,sizeof(buf));}void resend(int fd){    char buf[]={'R','S','D',CHANGE_LINE};    write(fd,buf,sizeof(buf));}void delay(int i){    int j;    for (;i>0;i--)        for(j=0;j<65535;j++);}  //////////////////////////////////////////////////////////////////////////////////////////// SerialThread::SerialThread(MainWindow *parent){    this->parent = parent;} void SerialThread::run(){     int fd,c, res;         int block_num,last_block;    int i;         char buf[BLOCK_SIZE];                 char file_name[32];    FILE *fp;    struct termios oldtio;     block_num=last_block=0;     fd = open(DEVICE, O_RDWR | O_NOCTTY );          parent->setCounter(fd);    parent->setMsgText("opend device fd::::::");                    if (fd <0){        perror(DEVICE);         parent->setMsgText("open device failed");         //    exit(-1);     }        tcgetattr(fd,&oldtio);    set_nc_mode(fd);    printf("Changed to nc mode\n");    /*    res=read(fd,( char *)file_name,32);    parent->setCounter(res);    parent->setMsgText("res is ::::::");         */ /*          if(res>0){        file_name[res-1]='\0';        printf("Received the file name:%s\n",file_name);        }    else        printf("The received file name is error.\n");     fp=fopen(file_name,"wb");    if(fp==NULL){        printf("Can not creat file %s!\n",file_name);        return;    //    exit(-1);        }    else        {            send_ack(fd);            printf("The file %s is created.\nWaitting for the block num and last block size\n",file_name);        }     //set_nc_mode(fd);    //printf("Changed to nc mode\n");     res=read(fd,buf,4);    printf("res=%d\n",res);    printf("Received the block num \n");     for(i=0,block_num=0;i<4;i++){        block_num=block_num*10+buf[i];        printf("buf[%d]=%x\n",i,buf[i]);        }    printf("The number of blocks is %d\n",block_num);    send_ack(fd);     res=read(fd,buf,3);    printf("res=%d\n",res);    printf("Received the last block size \n");        for(i=0,last_block=0;i<3;i++){                last_block=last_block*10+buf[i];        printf("buf[%d]=%x\n",i,buf[i]);        }         printf("The last block size is %d\n",last_block);    send_ack(fd);          printf("Starting receive blocks\n");     for(i=0;i<block_num;i++){        res=read(fd,buf,BLOCK_SIZE);        if(res!=BLOCK_SIZE){                        printf("res=%d,\t Request resend the %d block\n",res,i);            i--;                tcflush(fd, TCIOFLUSH);            resend(fd);            }        else{                         fwrite(buf,1,BLOCK_SIZE,fp);            printf("Received the %d block,    res=%d\n",i,res);            printf("Send ack signal complete,waiting to read\n");            send_ack(fd);            }    }    printf("start transporting the last block\n");     if(last_block>0){        send_ack(fd);        res=read(fd,buf,last_block);        printf("res=%d\n",res);                if(res!=last_block){            printf("Request resend the last block\n");            tcflush(fd, TCIOFLUSH);                        resend(fd);                        }        else            fwrite(buf,1,last_block,fp);        }    send_ack(fd);    printf("The file transports end\n");     fclose(fp);    printf("close the file\n");    tcsetattr(fd,TCSANOW,&oldtio);    close(fd);    printf("close the serial port\n");    */      }

⌨️ 快捷键说明

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