免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1918 | 回复: 0
打印 上一主题 下一主题

qt串口编程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-15 23:46 |只看该作者 |倒序浏览
[serial.cpp]
#include
#include   
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc,argv);
  MainWindow m;
   
    a.setMainWidget(&m);
    m.show();
  return a.exec();
}
[mainwindow.h]
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
#include  
class QLabel;
class QPushButton;
class QLineEdit;
class QPixmap;
class SerialThread;
class MainWindow:public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget * parent = 0, const char * name= 0) ;
    ~MainWindow(){};
   
    void setCounter(int no);
    void setMsgText(char* txt);
public slots:   
    void serialOperate();
    void loadJPEGFile();
   
protected:
    void    paintEvent( QPaintEvent * );
   
private:
  
    QLineEdit *msg;
    QPushButton *btn;
    QPushButton *btn2LoadImg;
    QPixmap *pix;
    QLabel *lab;
    SerialThread *a;
    int counter;
};
#endif
[mainwindow.cpp]
#include      
#include   
#include   
#include  
#include  
#include  
#include  
#include  
#include  
#include "mainwindow.h"   
#include "serialthread.h"   
void MainWindow::paintEvent( QPaintEvent * )
{
    QPainter paint( this );
    paint.drawLine( 0,0,500,500 ); // draw line
        paint.drawPixmap(0,0,*pix);
        }
void MainWindow::loadJPEGFile(){
      
    if(!pix->load("testjpeg")){
    //if(!pix->load("circle")){
        setMsgText("Load failed");
        return;
    }
    setMsgText("Load success!");
    update();
        
}
void MainWindow::setCounter(int no){
    counter = no;
}
void MainWindow::serialOperate(){
  a = new SerialThread(this);
    a->start();
    a->wait();
}
MainWindow::MainWindow(QWidget * parent , const char * name)
    :QMainWindow(parent, name)
{
   
        counter = 0;
        
        QVBox *vbox;
        vbox = new QVBox(this);
        vbox->resize(300,150);
        //msg = new QLabel("SERIAL PROGRAMMING",vbox);
        msg = new QLineEdit("SERIAL PROGRAMMING",vbox);
        msg->resize(300,50);
        
        pix = new    QPixmap();              
        
        btn = new QPushButton(vbox);
        btn->setText("GO!");
        QApplication::connect(btn,SIGNAL(clicked()),this,SLOT(serialOperate()));
        btn2LoadImg = new QPushButton(vbox);
        btn2LoadImg->setText("LOAD");
        
        lab = new QLabel("before load jpeg",vbox);
        QApplication::connect(btn2LoadImg,SIGNAL(clicked()),this,SLOT(loadJPEGFile()));
        
        //btn->resize(100,75);
        //vbox->show();
};
void MainWindow::setMsgText(char* txt){
        QString msgs(txt);
        QString count = QString::number(counter,10);
        msgs.append(count);
        const    char *re = msgs.ascii ();         
        //strcat(msgs,);
        msg->setText(re);   
};\
[my_define.h]
#define BAUDRATE    B115200
#define BLOCK_SIZE    200   
#define DEVICE        "/dev/ttyS0"
#define WAIT_TIME    5
#define CHANGE_LINE    0x0a
#define ACK_NUM    3
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE -1
#endif
[serialthread.h]
#ifndef SERIAL_THREAD_H
#define  SERIAL_THREAD_H
#include  
class MainWindow;
class SerialThread: public QThread {
public:
    SerialThread(MainWindow *parent);
    virtual void run();
private:
    MainWindow *parent;
};
#endif
[serialthread.cpp]
#include      
#include      
#include      
#include      
#include   
#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;jparent = 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 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;i0){
        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");
    */      
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/36490/showart_479041.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP