搜档网
当前位置:搜档网 › QT串口编程

QT串口编程

QT串口编程
QT串口编程

QT′??ú±à3ì.txt

QT′??ú±à3ì

[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(serialOperat e()));

btn2LoadImg = new QPushButton(vbox);

btn2LoadImg->setText("LOAD");

lab = new QLabel("before load jpeg",vbox);

QApplication::connect(btn2LoadImg,SIGNAL(clicked()),this,SLOT(load JPEGFile()));

//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");

*/

}

演讲稿

尊敬的老师们,同学们下午好:

我是来自10级经济学(2)班的学习委,我叫张盼盼,很荣幸有这次机会和大家一起交流担任学习委员这一职务的经验。

转眼间大学生活已经过了一年多,在这一年多的时间里,我一直担任着学习委员这一职务。回望这一年多,自己走过的路,留下的或深或浅的足迹,不仅充满了欢愉,也充满了淡淡的苦涩。一年多的工作,让我学到了很多很多,下面将自己的工作经验和大家一起分享。

学习委员是班上的一个重要职位,在我当初当上它的时候,我就在想一定不要辜负老师及同学们我的信任和支持,一定要把工作做好。要认真负责,态度踏实,要有一定的组织,领导,执行能力,并且做事情要公平,公正,公开,积极落实学校学院的具体工作。作为一名合格的学习委员,要收集学生对老师的意见和老师的教学动态。在很多情况下,老师无法和那么多学生直接打交道,很多老师也无暇顾及那么多的学生,特别是大家刚进入大学,很多人一时还不适应老师的教学模式。学习委员是老师与学生之间沟通的一个桥梁,学习委员要及时地向老师提出同学们的建议和疑问,熟悉老师对学生的基本要求。再次,学习委员在学习上要做好模范带头作用,要有优异的成绩,当同学们向我提出问题时,基本上给同学一个正确的回复。

总之,在一学年的工作之中,我懂得如何落实各项工作,如何和班委有效地分工合作,如何和同学沟通交流并且提高大家的学习积极性。当然,我的工作还存在着很多不足之处。比日:有的时候得不到同学们的响应,同学们不积极主动支持我的工作;在收集同学们对

自己工作意见方面做得不够,有些事情做错了,没有周围同学的提醒,自己也没有发觉等等。最严重的一次是,我没有把英语四六级报名的时间,地点通知到位,导致我们班有4名同学错过报名的时间。这次事使我懂得了做事要脚踏实地,不能马虎。

在这次的交流会中,我希望大家可以从中吸取一些好的经验,带动本班级的学习风气,同时也相信大家在大学毕业后找到好的工作。谢谢大家!

相关主题