其实这个程序是参考网上修改的,是用于串口通信,用一个线程实现.
一共有5个文件:thread.h  thread.cpp  mainwindow.h  mainwindow.cpp  main.cpp
thread.h : 
#ifndef THREAD_H
#define THREAD_H
#include <qthread.h>
class Thread : public QThread
{
  Q_OBJECT  
public:
  Thread();  
  char buf[128];
  volatile bool stopped; 
  volatile bool write_rs;
  volatile bool read_rs;
protected:
  virtual void run();
  
signals:
    void finished();
};
#endif
thread.cpp : 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>    
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <qthread.h> 
#include "thread.h"
#define BAUDRATE B9600   
#define RS_DEVICE "/dev/ttySAC1"       
Thread::Thread()
{}                                               
void Thread::run()         
{
 int fd,res;
struct termios oldtio,newtio;   
printf("start...\n");
fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);     
if(fd<0)
{
perror("error");
exit(1);                           
}
printf("Open...\n");
tcgetattr(fd,&oldtio);           
bzero(&newtio,sizeof(newtio));     
newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;  
newtio.c_iflag|=IGNPAR;                
newtio.c_lflag=0;  
newtio.c_cc[VMIN]=0;      
newtio.c_cc[VTIME]=100;                   
tcflush(fd,TCIFLUSH);                    
tcsetattr(fd,TCSANOW,&newtio);           
while(stopped)                         
{
 if(write_rs)                       
{
write_rs=0;
write(fd,"QtEmbedded-4.5.1",16);
}
 if(read_rs)                          
{
read_rs=0;
res=read(fd,buf,10);
buf[res]=0;
emit finished();                  
}
}
printf("Close...\n");
tcsetattr(fd,TCSANOW,&oldtio);     
::close(fd);
}
mainwindow.h : 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <qwidget.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include "thread.h"        
class MainWindow:public QWidget
{
  Q_OBJECT
    
 public:
  MainWindow(QWidget *parent=0);
  
 public slots:
  void writeThread();
  void readThread();
  void closeThread();
  void display();
 
 private:
     QPushButton *writeButton;
     QPushButton *readButton;
     QPushButton *closeButton;
     QLabel *dis_label;
     Thread *yy;
};
#endif
mainwindow.cpp : 
#include"mainwindow.h"
#include <qpushbutton.h>
#include <qlabel.h>
MainWindow::MainWindow(QWidget *parent)
  :QWidget(parent)
{
  writeButton = new QPushButton("write",this);
  readButton = new QPushButton("read",this);
  closeButton = new QPushButton("close",this);
  dis_label = new QLabel ("none",this);
  
   dis_label->setGeometry(30, 30, 150, 30);
  writeButton->setGeometry(30, 120, 80, 30);
   readButton->setGeometry(120, 120, 80, 30);
   closeButton->setGeometry(210, 120, 80, 30);
   
  yy = new Thread;
  yy->start();         
  yy->stopped=1;       
  yy->write_rs=0;
  yy->read_rs=0;
    
  connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));     
  connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));
  connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));
  connect(yy,SIGNAL(finished()),this,SLOT(display()));     
  
     showMaximized () ;
}
void MainWindow::display()
{
dis_label->setText(yy->buf);    
}
void MainWindow::writeThread()  
  yy->write_rs=1;
}
void MainWindow::readThread()
{
  yy->read_rs=1;
}
void MainWindow::closeThread()
{
yy->stopped=0;
}
main.cpp : 
#include<qapplication.h>
#include"mainwindow.h"
int main(int argc,char *argv[])
{
  QApplication app(argc,argv);
  MainWindow mw;
  mw.show();
  return app.exec();
}