这是报错的头文件:
#include <qthread.h>
class Form1;
class SerialThread: public QThread {
public:
SerialThread(Form1 *parent);
~SerialThread();
virtual void run();
private:
protected:
Form1 *parent;
};
这是c++的文件:
/****************************************************************************
** Form implementation generated from reading ui file 't.ui'
**
** Created: 四 7月 17 12:02:29 2008
** by: The User Interface Compiler ($Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "t.h"
#include "m.h"
#include <qvariant.h>
#include <qpushbutton.h>
#include <qtextview.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE B115200 /*定义串口速率,前面加个B然后是你所需要的速率。*/
#define MODEMDEVICE "/dev/ttyS0" /*使用COM1进行通讯*/
#define _POSIX_SOURCE 1 /*符合POSIX标准的代码*/
/*
* Constructs a Form1 as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
Form1::Form1( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "Form1" );
TextView1 = new QTextView( this, "TextView1" );
TextView1->setGeometry( QRect( 60, 130, 131, 104 ) );
languageChange();
resize( QSize(282, 353).expandedTo(minimumSizeHint()) );
// signals and slots connections
serialOperate();
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
TextView1->setText( tr( "mm m/ndfdf/n" ) );
}
void Form1::serialOperate(){
a = new SerialThread(this);
a->start();
a->wait();
}
SerialThread::SerialThread(Form1 *parent){
this->parent = parent;
}
SerialThread::~SerialThread(){}
void SerialThread::run()
{
int fd,c,res,i=1;
struct termios oldios,newios;
char buf[1024];
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); /*以读写方式打开串口。使用不要MODEM的直接连接方式。 */
if (fd < 0) {
perror(MODEMDEVICE);
exit(0);
}
tcgetattr(fd,&oldios); /*保存当前串口设置*/
bzero(&newios,sizeof(newios)); /*清空当前串口设置*/
newios.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; /*设置为1200bps、7位数据位、无需MODEM连接和允许读取*/
newios.c_iflag = IGNPAR; /*忽略奇偶校验错的输入数据*/
newios.c_oflag = 0; /*这个不大清楚 ;P*/
newios.c_lflag = 0; /*设置成非阻塞模式,read()函数调用根据设置的字符数返回。常规模式时为返回一行。*/
newios.c_cc[VTIME] = 0; /*不使用超时等待*/
newios.c_cc[VMIN] = 1; /*read()到一个char时就返回。*/
tcflush(fd,TCIFLUSH); /*端口复位*/
tcsetattr(fd,TCSANOW,&newios); /*使端口属性设置生效*/
while (1) {
res = read(fd,buf,512);
buf[res] = 0; /*设置穿结束标志 */
while(i<=res)
{
if(buf=='h'){ printf("1ceng!\n");
if(buf[i+1]=='a'){ printf("%s",buf); }}
i++;
}
i=1;
}
tcsetattr(fd,TCSANOW,&oldios);
// close(fd); exit(0);
}