• 5731阅读
  • 7回复

qt如何加入串口程序,报错~ [复制链接]

上一主题 下一主题
离线sun23xiang23
 
只看楼主 倒序阅读 楼主  发表于: 2008-07-22
— 本帖被 XChinux 执行加亮操作(2008-07-22) —
我qt界面和linux的串口程序都做好了。我仿照网上串口例子,用QThread创建一个线程来读取串口数据,但编译时报错,已经改了很久了,谢谢高手指点下~~~
如何在qt中加入已写好的串口程序,并读出数据显示????
谢谢!!

[root@redhat new]# make
g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions  -DQT_NO_DEBUG -I/usr/lib/qt-3.1/mkspecs/default -I. -I. -I/usr/lib/qt-3.1/include -o main.o main.cpp
In file included from main.cpp:3:
m.h:4: parse error before `{' token
m.h:7: destructors must be member functions
m.h:8: virtual outside class declaration
m.h:11: parse error before `protected'
make: *** [main.o] Error 1
离线mecland
只看该作者 1楼 发表于: 2008-07-22
代码, 看不出
离线sun23xiang23
只看该作者 2楼 发表于: 2008-07-22
这是报错的头文件:
#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);

}
离线foxyz

只看该作者 3楼 发表于: 2008-07-22
代码也不贴全阿?你的main呢
离线foxyz

只看该作者 4楼 发表于: 2008-07-22
你的错误可以看出来,不是逻辑错误,是基本语法错误!
离线sun23xiang23
只看该作者 5楼 发表于: 2008-07-22
#include<qapplication.h>
#include"t.h"
#include"m.h"
int main( int argc, char **argv )
{
QApplication a(argc, argv);
Form1 hehe;
a.setMainWidget(&hehe);
hehe.show();
return a.exec();
}

语法错误??

报错的是那个头文件的问题吧,我不知道是哪的语法出问题了。。
离线sun23xiang23
只看该作者 6楼 发表于: 2008-07-22
这个错误解决了,要在工程文件中加入CONFIG += thread
但是又出现新的问题了。。。。。。



[root@redhat new]# make
g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions -D_REENTRANT  -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.1/mkspecs/default -I. -I. -I/usr/lib/qt-3.1/include -o t.o t.cpp
t.cpp: In member function `void Form1::serialOperate()':
t.cpp:71: `a' undeclared (first use this function)
t.cpp:71: (Each undeclared identifier is reported only once for each function
  it appears in.)
t.cpp:73: calling type `wait' like a method
t.cpp: In member function `virtual void SerialThread::run()':
t.cpp:108: `read' undeclared (first use this function)
t.cpp:84: warning: unused variable `int c'
make: *** [t.o] Error 1

read是linux的函数啊,包含了头文件的,怎么没定义呢???
离线huzhiwen28

只看该作者 7楼 发表于: 2008-08-22
学习了
http://blog.sina.com.cn/tonyhuzhiwen
https://github.com/huzhiwen28/
专注于工业嵌入设备开发
快速回复
限100 字节
 
上一个 下一个