http://blog.sina.com.cn/s/blog_7da13b510100xtgr.html
前几天发现手里还闲着一块GPS 佳明的15W 也不知道是好的还是坏的呵呵一时兴起我就趁周六日没什么事情
用qt做了一个界面 现在已经调试完毕 可以再2440的arm板子上正确运行 环境fedora9+qt4.5.3+arm-linux-gcc4.3.3
界面如下
定位正常
未定位
主要代码实现如下:
GPS.h文件
#ifndef KTEST01_H
#define KTEST01_H
#include <QWidget>
#include "ui_Gps.h"
#include<QDebug>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <pthread.h>
class Gps: public QWidget, private Ui::Form
{
Q_OBJECT
public:
Gps(QWidget* parent = 0, Qt::WFlags fl = 0 );
~Gps();
int i;
int j;
bool openflag;
int file_fd;
int GpsYes;
public slots:
protected:
protected slots:
void paintEvent(QPaintEvent *event);
void updategps(void);
void updatepaint(void );
};
class GPRMC
{
public:
char UTCtime[7];
char effect[2];
char weidu[10];
char NorS[21];
char jingdu[11];
char EorW[2];
char speed[6];
char direct[6];
char UTCcal[7];
char cipianjiao[6];
char pjfx[2];
char mod[2];
};
#endif
*******************************GPS.cpp文件***********************************************
#include "Gps.h"
#include <QtGui/QPainter>
Gps::Gps(QWidget* parent, Qt::WFlags fl)
: QWidget( parent, fl ), Ui::Form()
{ i=50;
j=0;
setupUi(this);
GpsYes=0;
gpslabel->setText(QApplication::translate("Form", "\346\234\252\345\256\232\344\275\215", 0, QApplication::UnicodeUTF8));
qDebug()<<QObject::tr("open button pressed");
file_fd=open("/dev/ttySAC1",O_RDWR|O_NONBLOCK|O_NOCTTY); //打开串口1
if(file_fd>=0)
{
openflag=1;
qDebug()<<QObject::tr("open succeed");
struct termios Srpt;
tcgetattr(file_fd,&Srpt);
cfsetispeed(&Srpt,B4800);
cfsetospeed(&Srpt,B4800);
Srpt.c_cflag &= ~PARENB;
Srpt.c_cflag &= ~CSTOPB;
Srpt.c_cflag |= CS8;
Srpt.c_lflag &= ~(ICANON |ISIG);
tcsetattr(file_fd,TCSANOW,&Srpt);
}
else
{
openflag=0;
qDebug()<<QObject::tr("open failed");
}
this->setAttribute(Qt::WA_PaintOutsidePaintEvent);
}
void Gps::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
i=(i+9)%360;
QConicalGradient gradient(260, 51,i);
gradient.setSpread(QGradient::ReflectSpread);
if(GpsYes)
{
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(0.1, Qt::darkGray);
gradient.setColorAt(0.2, Qt::gray);
gradient.setColorAt(0.3, Qt::lightGray);
gradient.setColorAt(0.4, Qt::red);
gradient.setColorAt(0.5, Qt::green);
gradient.setColorAt(0.6, Qt::blue);
gradient.setColorAt(0.7, Qt::yellow);
gradient.setColorAt(0.8, Qt::magenta);
gradient.setColorAt(0.9, Qt::yellow);
gradient.setColorAt(1, Qt::green);
}
else
{
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(1, Qt::black);
}
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.setBrush(gradient);
painter.drawEllipse(210, 1, 100, 100);
QPainter painter1(this);
painter1.setRenderHint(QPainter::Antialiasing, true);
painter1.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
if(GpsYes)
{
painter1.setBrush(QBrush(Qt::red, Qt::SolidPattern));
gpslabel->setText(QApplication::translate("Form", "\345\256\232\344\275\215\344\270\255", 0, QApplication::UnicodeUTF8));
}
else
{
painter1.setBrush(QBrush(Qt::black, Qt::SolidPattern));
gpslabel->setText(QApplication::translate("Form", "\346\234\252\345\256\232\344\275\215", 0, QApplication::UnicodeUTF8));
}
painter1.drawEllipse(190, 30, 10, 10);
}
Gps::~Gps()
{
}
void Gps::updategps(void)
{
qDebug()<<QObject::tr("enter timer");
char buff[2048]="0";
char Rmcbuff[80];
int counttemp;
GPRMC RPM;
lseek(file_fd,0,SEEK_SET);
int count=0;
count=read(file_fd,buff,2048); //从串口中读取数据
count=512;
int rcvflag=0;
if(count>0)
{
char *starp;
for(starp=buff;starp<buff+count;starp++) //解析出RMC包
{
if((*starp)=='$')
{
starp++;
if((*starp)=='G')
{
starp++;
if((*starp)=='P')
{
starp++;
if((*starp)=='R')
{
starp++;
if((*starp)=='M')
{
starp++;
if((*starp)=='C')
{
starp++;
starp++;
for(counttemp=0;(starp<buff+count)||counttemp>=70;starp++)
{
Rmcbuff[counttemp++]=*starp;
if(((*starp)==0x0d)&&((*(starp-1))==0x0A))
{
break;
qDebug()<<"break1";
}
if(((*starp)=='\n'))
{
break;
qDebug()<<"break2";
}
// qDebug()<<*starp;
}
if(counttemp>=63)
{
rcvflag=1;
}
else
{
rcvflag=0;
}
qDebug()<<Rmcbuff<<"counttemp"<<counttemp;
break;
}
}
}
}
}
}
}
}
if(rcvflag)
{
for(counttemp=0;counttemp<=5;counttemp++) //根据包的内容填充结构体
{
RPM.UTCtime[counttemp]=Rmcbuff[counttemp];
}
RPM.UTCtime[counttemp]='\0';
RPM.effect[0]=Rmcbuff[7];
RPM.effect[1]='\0';
for(counttemp=9;counttemp<=17;counttemp++)
{
RPM.weidu[counttemp-9]=Rmcbuff[counttemp];
}
RPM.weidu[counttemp-9]='\0';
RPM.NorS[0]=Rmcbuff[19];
RPM.NorS[1]='\0';
for(counttemp=21;counttemp<=30;counttemp++)
{
RPM.jingdu[counttemp-21]=Rmcbuff[counttemp];
}
RPM.jingdu[counttemp-21]='\0';
RPM.EorW[0]=Rmcbuff[32];
RPM.EorW[1]='\0';
for(counttemp=34;counttemp<=38;counttemp++)
{
RPM.speed[counttemp-34]=Rmcbuff[counttemp];
}
RPM.speed[counttemp-34]='\0';
for(counttemp=40;counttemp<=44;counttemp++)
{
RPM.direct[counttemp-40]=Rmcbuff[counttemp];
}
RPM.direct[counttemp-40]='\0';
for(counttemp=46;counttemp<=51;counttemp++)
{
RPM.UTCcal[counttemp-46]=Rmcbuff[counttemp];
}
RPM.UTCcal[counttemp-46]='\0';
for(counttemp=53;counttemp<=57;counttemp++)
{
RPM.cipianjiao[counttemp-53]=Rmcbuff[counttemp];
}
RPM.cipianjiao[counttemp-53]='\0';
RPM.pjfx[0]=Rmcbuff[59];
RPM.pjfx[1]='\0';
RPM.mod[0]=Rmcbuff[61];
RPM.mod[1]='\0';
}
if(RPM.EorW[0]=='E') //设定显示
{
jingdu->setText(QApplication::translate("Form", "\344\270\234\347\273\217", 0, QApplication::UnicodeUTF8));
}
else
{
jingdu->setText(QApplication::translate("Form", "\350\245\277\347\273\217", 0, QApplication::UnicodeUTF8));
}
if(RPM.NorS[0]=='S')
{
weidu->setText(QApplication::translate("Form", "\345\215\227\345\215\212\347\220\203", 0, QApplication::UnicodeUTF8));
}
else
{
weidu->setText(QApplication::translate("Form", "\345\214\227\345\215\212\347\220\203", 0, QApplication::UnicodeUTF8));
}
weiduline->clear();
jingduline->clear();
gaoduline->clear();
speedline->clear();
directline->clear();
weiduline->insert(RPM.weidu);
jingduline->insert(RPM.jingdu);
gaoduline->insert("can't receive");
speedline->insert(RPM.speed);
directline->insert(RPM.direct);
if(RPM.effect[0]=='A')
{
GpsYes=1;
}
else
{
GpsYes=0;
}
dateTime->setTime(QTime((int)((RPM.UTCtime[0]-'0')*10+RPM.UTCtime[1]-'0'),
(int)((RPM.UTCtime[2]-'0')*10+RPM.UTCtime[3]-'0'),
(int)((RPM.UTCtime[4]-'0')*10+RPM.UTCtime[5]-'0'),0));
dateTime->setDate(QDate((RPM.UTCcal[4]-'0')*10+RPM.UTCcal[5]-'0'+2000,
(RPM.UTCcal[2]-'0')*10+RPM.UTCcal[3]-'0',
(RPM.UTCcal[0]-'0')*10+RPM.UTCcal[1]-'0'));
}
void Gps::updatepaint(void )
{
update();
}
*************************************主程序main.cpp**********************************
#include <QApplication>
#include "Gps.h"
#include<QtGui>
#include <QTimer>
int main(int argc, char *argv[])
{
// Q_INIT_RESOURCE(application);
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); //added by song
Gps * mw = new Gps();
QTimer *timer = new QTimer(mw);
QObject::connect(timer, SIGNAL(timeout()), mw, SLOT(updategps()));
QTimer *timer1 = new QTimer(mw);
QObject::connect(timer1, SIGNAL(timeout()), mw, SLOT(updatepaint()));
// QObject::connect(timer, SIGNAL(timeout()), mw, SLOT(update()));
timer->start(1000);
timer1->start(25);
mw->show();
return app.exec();
}
呵呵 拿出来和大家交流交流 刚开始学习qt水平有限 希望大家指出不足
说明:如有人转载请注明出处 谢谢
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)