当前位置:编程学习 > wap >>

關於Qt-Creator設計GPS接收按鈕沒反應(直接當機)

小弟我是初次使用Qt來做設計GPS結合地圖來做定位
但是我對於Qt4程式碼的撰寫語法和規則實在不瞭解
而這以下是我的設計程式碼(使用QtCreator除錯後執行沒顯示錯誤,只有顯示Exit code with 0)
我在QtCreator確認可執行後轉成Mini2440 ARM的執行檔
但是點擊readcom這個Button時發現他會直接當機沒有擷取到訊號並顯示出來
希望有高手可以幫助我決問題!!
還是說我有哪邊的語法寫的是錯誤的?

附註:
opnecom():啟動COM與GPS接收器的連結
readcom():擷取GPS的訊號(GPGGA)並執行轉換
showmap():在地圖上標出所在位置(座標的換算)
GpsReceive、GpsReceive_2:將訊息顯示出來


#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[i];
        if(buf[i]!=',')
        {
            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[i]!=',')
            {
                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"));
}
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,