




已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
QT串口編程.txt都是一個(gè)山的狐貍,你跟我講什么聊齋,站在離你最近的地方,眺望你對(duì)別人的微笑,即使心是百般的疼痛只為把你的一舉一動(dòng)盡收眼底刺眼的白色,讓我明白什么是純粹的傷害。QT串口編程serial.cpp#include #include #include mainwindow.hint 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; ; #endifserialthread.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_ccVTIME = WAIT_TIME; options.c_ccVMIN = 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 (;i0;i-) for(j=0;jparent = parent; void SerialThread:run() int fd,c, res; int block_num,last_block; int i; char bufBLOCK_SIZE; char file_name32; 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 moden); /* res=read(fd,( char *)file_name,32); parent-setCounter(res); parent-setMsgText(res is :); */ /* if(res0) file_nameres-1=0; printf(Received the file name:%sn,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 sizen,file_name); /set_nc_mode(fd); /printf(Changed to nc moden); res=read(fd,buf,4); printf(res=%dn,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=%dn,res); if(res!=last_b
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 兒童心理發(fā)展的關(guān)鍵階段與護(hù)理
- 詳盡工作經(jīng)歷及職位證明書(shū)(5篇)
- 動(dòng)物們的森林生活童話類(lèi)作文11篇
- 夜空中的星星秘密抒情作文(8篇)
- 數(shù)字化轉(zhuǎn)型助力公路貨運(yùn)行業(yè)效率革命研究報(bào)告
- 大型物流配送中心建設(shè)對(duì)城市能源消耗風(fēng)險(xiǎn)分析報(bào)告
- 2025年可持續(xù)發(fā)展目標(biāo)(SDGs)在虛擬數(shù)字人技術(shù)中的應(yīng)用與發(fā)展報(bào)告
- 共享出行平臺(tái)信用積分體系設(shè)計(jì)與應(yīng)用報(bào)告
- 2025年海上風(fēng)力發(fā)電場(chǎng)運(yùn)維管理與技術(shù)創(chuàng)新策略深度報(bào)告
- 2025年智慧公交系統(tǒng)實(shí)施方案評(píng)估報(bào)告:智能調(diào)度與運(yùn)營(yíng)優(yōu)化分析
- 炸雞店的產(chǎn)品創(chuàng)新與口味調(diào)研
- 風(fēng)機(jī)吊裝安全培訓(xùn)
- 陜西省銅川市2025年八下英語(yǔ)期末監(jiān)測(cè)試題含答案
- 社區(qū)工作者綜合能力考試基礎(chǔ)知識(shí)試題及答案
- 山西焦煤集團(tuán)所屬煤炭子公司招聘筆試題庫(kù)2025
- 墊付醫(yī)療費(fèi)協(xié)議書(shū)
- 2025年福建省廈門(mén)市中考物理模擬試卷
- 2024年陜西省普通高中學(xué)業(yè)水平合格性考試語(yǔ)文試題(原卷版+解析版)
- (高清版)DG∕TJ 08-9-2023 建筑抗震設(shè)計(jì)標(biāo)準(zhǔn)
- DB44-T 2605-2025 生活垃圾焚燒發(fā)電設(shè)施能源消耗計(jì)算與限額
- 代謝相關(guān)脂肪性肝病防治指南2024年版解讀
評(píng)論
0/150
提交評(píng)論