qt串列埠程式設計

2021-05-01 19:39:18 字數 4920 閱讀 2993

本帖被 xchinux 執行加亮操作(2008-04-22)

[serial.cpp]

int main(int argc, char *argv)

[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 ;

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 "serialthread.h"   

void mainwindow::paintevent( qpaintevent * )

void mainwindow::loadjpegfile()

setmsgtext("load success!");

update(); }

void mainwindow::setcounter(int no)

void mainwindow::serialoperate()

mainwindow::mainwindow(qwidget * parent , const char * name)

:qmainwindow(parent, name) ;

void mainwindow::setmsgtext(char* txt);/

[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 ;

#endif

[serialthread.cpp]

#include

#include

#include

#include

#include

#include "my_define.h"

#include "serialthread.h"

#include "mainwindow.h"

int set_nc_mode(int fd)

/* 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)

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)

return(true); }

void send_ack(int fd) ;

write(fd,buf,sizeof(buf)); }

void resend(int fd) ;

write(fd,buf,sizeof(buf)); }

void delay(int i)

serialthread::serialthread(mainwindow *parent)

void serialthread::run()

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)

else

printf("the received file name is error./n");

fp=fopen(file_name,"wb");

if(fp==null)

else

//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++)

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++)   

printf("the last block size is %d/n",last_block);

send_ack(fd);   

printf("starting receive blocks/n");

for(i=0;i0)

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

*/      }

qt串列埠程式設計

開啟串列埠後要重新設定一遍波特率,資料位,校驗位等,否則這些都是預設的。連線串列埠 void mainwindow on opencomn clicked ui opencomn settext 開啟串列埠 return qstring comnarg ui comnarg currenttext ...

QT串列埠程式設計

最近開發乙個專案,需要串列埠程式設計,是在qt windows 環境下進行開發。在qt5.0的版本之前都沒有支援串列埠,需要使用第三方類庫qserialport,網上的qt串列埠程式設計的例項也是基於此編寫的。最近安裝5.2 和5.4的版本,在瀏覽其幫助文件的時候,發現qt已經整合了串列埠的類庫,果...

自學Qt之路 串列埠程式設計(使用自帶Qt庫)

之前學習嵌入式驅動,做乙個紅外攝像頭的專案,在作業系統之上直接操作 lcd顯示屏,顯示影象。後來聽說 qt能夠跨平台,跨 window android linux 三個平台,而且做專案必須有個圖形介面,看起來高大上。之後根據這個專案需要,自學 qt5.6 寫個部落格記錄一下自己學習過程。之前看過 q...