小弟我用Creator除錯後並無顯示錯誤
而且可以執行出設計畫面,只是按下readcom這個Button卻發生當機問題
readcom():擷取GPS的訊號(GPGGA)並執行轉換
下列是我的程式碼,我有幾點疑問
我有宣告char b[20],然後
data_2 += (QCchar) b;
data_2 += (QString) lati;
data_2 += (QString) longi;
ui->GpsReceive->setText(data_1)
ui->GpsReceive_2->setText(data_2)
我除錯後發現上面這種語法是可以的
但是我改成下列這種語法他也可以執行,但是兩種方式都無法秀出資訊(當機),那麼到底是哪個才是正確的?
data_2 += b;
data_2 += lati;
data_2 += longi;
ui->GpsReceive->setText(data_1)
ui->GpsReceive_2->setText(data_2)
我的GPS接收部分有錯誤嗎?!希望有高手可以幫我解答問題
P.S:順帶一提我的lati 跟 longi的原先的宣告方式是char *lati, *longi
但是compiler出現錯誤我改成這種宣告方式 const char *lati, *longi,就解決了
這兩者有差異嗎?
--------------------------------------------------------------------------------------------------------------
附註:
opnecom():啟動COM與GPS接收器的連結
readcom():擷取GPS的訊號(GPGGA)並執行轉換
showmap():在地圖上標出所在位置(座標的換算)
GpsReceive、GpsReceive_2:將訊息顯示出來
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include<QGroupBox>
#include<QLabel>
#include<QLineEdit>
#include<QComboBox>
#include<QWebView>
#include<QPushButton>
#include<QFrame>
#include<QGraphicsView>
#include<QDial>
#include<QGraphicsView>
#include<qcombobox.h>
#include<qframe.h>
#include<qgroupbox.h>
#include<qlabel.h>
#include<qlineedit.h>
#include<qpushbutton.h>
#include<qlayout.h>
#include<qvariant.h>
#include<qtooltip.h>
#include<qwhatsthis.h>
#include<qimage.h>
#include<qpixmap.h>
#include<qwebview.h>
#include<termios.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<unistd.h>
//#define BAUDRATE B115200
//#define MODEMDEVICE "/dev/tty0/"
float latitude,longitude;
int fd,c=0,res,tt,mm,ss;
int num=0,i,gps=0,day,mon,year;
struct termios oldtio,newtio;
char buf[256];
char Time[3],latiplace1[3],latiplace2[8];
char longiplace1[4],longiplace2[8];
char da[3],mo[3],ye[3];
const char *lati,*longi;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Dialog::opencom()
{
const char *myCom;
long myBaud;
long myByte;
long myParity;
long ParityON;
long STOPBITS=0;
connect(ui->openBtn,SIGNAL(clicked()),this,SLOT(opencom()));
int ComIndex=ui->comCombo->currentIndex();
switch(ComIndex)
{
case 0:
myCom="/dev/tty0";
case 1:
myCom="/dev/tty1";
case 2:
myCom="/dev/tty2";
default:
myCom="/dev/tty0";
break;
}
int BaudIndex=ui->baudCombo->currentIndex();
switch(BaudIndex)
{
case 0:
myBaud=B4800;
case 1:
myBaud=B9600;
case 2:
myBaud=B19200;
case 3:
myBaud=B38400;
default:
myBaud=B4800;
break;
}
int ByteIndex=ui->byteCombo->currentIndex();
switch(ByteIndex)
{
case 0:
myByte=CS5;
case 1:
myByte=CS6;
case 2:
myByte=CS7;
case 3:
myByte=CS8;
default:
myByte=CS8;
break;
}
int ParityIndex=ui->parityCombo->currentIndex();
switch(ParityIndex)
{
case 0:
ParityON=0;
myParity=0;
case 1:
ParityON=PARENB;
myParity=PARODD;
case 2:
ParityON=PARENB;
myParity=0;
default:
ParityON=0;
myParity=0;
break;
}
if (fd < 0)
{
printf("error !!\n");
}
int fd = ::open(myCom,O_RDWR|O_NOCTTY);
tcgetattr(fd,&oldtio); // save current port settings
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = myBaud | myByte | STOPBITS | myParity | CLOCAL | CREAD;
//newtio.c_cflag = BAUDRATE |CS8 | CLOCAL | CREAD | HUPCL;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
ui->GpsReceive->setText("opencom start"); //程式執行完畢 在GpsReceive與GpsReceive_2 這兩個輸出欄印出 [opencom end]
ui->GpsReceive_2->setText("opencom start");
}
void Dialog::closecom()
{
connect(ui->closeBtn,SIGNAL(clicked()),this,SLOT(closecom()));
fd = ::close(fd);
tcsetattr(fd,TCSANOW,&oldtio);
ui->GpsReceive->setText("opencom close");
ui->GpsReceive_2->setText("opencom close");
}
void Dialog::readcom()
{
connect(ui->readBtn,SIGNAL(clicked()),this,SLOT(readcom()));
while(1)
{
gps=1;
QString data_1="",data_2="";
res=read(fd,buf,256);
buf[res]='\0';
if((buf[3]!='G')||(buf[4]!='G')||(buf[5]!='A'))
{
num=0;
for(i=0;i<res;i++)
{
data_1 += buf;
if(buf!=',')
{
continue;
}
else
{
num++;
}
switch(num)
{
case 6:
if(buf[i+1]=='0')
{
ui->GpsReceive->setText(data_1);
ui->GpsReceive_2->setText("GPA is not True");
break;
}
}
}
num=0;
if(gps!=0)
{
for(i=0;i<res;i++)
{
if(buf!=',')
{
continue;
}
else
{
num++;
}
switch(num)
{
case 1://時間
data_2 += "Taiwan Timee:";
Time[0]=buf[i+1];
Time[1]=buf[i+2];
Time[2]='\0';
char b[20];
tt = atoi(Time)+8;
sprintf(b,"%d",tt);
data_2 += b;
data_2 += ":";
Time[0]=buf[i+3];
Time[1]=buf[i+4];
Time[2]='\0';
mm = atoi(Time);
sprintf(b,"%d",mm);
data_2 += b;
data_2 += ":";
Time[0]=buf[i+5];
Time[1]=buf[i+6];
Time[2]='\0';
ss= atoi(Time);
sprintf(b,"%d",ss);
data_2 += b;
data_2 += ",";
break;
case 2://緯度
latiplace1[0] = buf[i+1];//度
latiplace1[1] = buf[i+2];//度
latiplace1[2] = '\0';
latiplace2[0] = buf[i+3];//分
latiplace2[1] = buf[i+4];//分
latiplace2[2] = buf[i+5];//buf[i+5] 是 .
latiplace2[3] = buf[i+6];//分
latiplace2[4] = buf[i+7];//分
latiplace2[5] = buf[i+8];//分
latiplace2[6] = buf[i+9];//分
latiplace2[7] = '\0';
latitude = atof(latiplace1) + atof(latiplace2)/60; //120
//char b[20];
sprintf(b,"%f",latitude);
data_2 += b;
data_2 += ",";
ui->latitudeLineEdit->setText(b);
break;
case 3://南北緯
if(buf[i+1]=='N')
{
QString lati = "North Latitude";
}
else
{
lati = "South Latitude";
}
data_2 += lati;
data_2 += ",";
break;
case 4://經度
longiplace1[0] = buf[i+1];//度
longiplace1[1] = buf[i+2];//度
longiplace1[2] = buf[i+3];//度
longiplace1[3] = '\0';
longiplace2[0] = buf[i+4];//分
longiplace2[1] = buf[i+5];//分
longiplace2[2] = buf[i+6];//buf[i+6] 是.
longiplace2[3] = buf[i+7];//分
longiplace2[4] = buf[i+8];//分
longiplace2[5] = buf[i+9];//分
longiplace2[6] = buf[i+10];//分
longiplace2[7] = '\0';
longitude = atof(longiplace1) +atof(longiplace2)/60;
sprintf(b,"%f",longitude);
data_2 += b;
data_2 += ",";
ui->longitudeLineEdit->setText(b);
break;
case 5://東西經
if(buf[i+1]=='E')
{
longi = "East Longitude";
}
else
{
longi = "West Longitude";
}
data_2 += longi;
data_2 += "。";
break;
case 6:
ui->GpsReceive->setText(data_1);
ui->GpsReceive_2->setText(data_2);
}
}
}
}
}
}
void Dialog::showmap()
{
connect(ui->showBtn,SIGNAL(clicked()),this,SLOT(showmap()));
if(gps)
{
int x = (int) ((longitude - (120.28591-0.000165))/0.00033);
int y = (int) (((22.73363+0.000165)-latitude)/0.00033);
ui->pixmapQT->setGeometry( QRect( 17+x*30, 27+y*30, 16, 16 ) );
ui->GpsReceive->setText("show map ok");
ui->GpsReceive_2->setText("can not show map");
}
else
{
ui->GpsReceive->setText("can not show map");
ui->GpsReceive_2->setText("can not show map");
}
}
void Dialog::on_mapBtn_clicked()
{
ui->webView->load(QUrl("http://maps.google.com/maps/api/staticmap?center=Kaohsiung+Nanzi+Kaohsiung_University,Taiwan,TW&zoom=16&size=240x240&maptype=roadmap&markers=color:blue%7Clabel:S%7C22.735113,120.280851&sensor=false"));
}