xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/imxx11/gps/gps.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "gps.h"
2 #include "ui_gps.h"
3 #include <stdio.h>
4 #include <unistd.h>
5 
6 #define USE_BEIJING_TIMEZONE
7 
gps(QWidget * parent)8 gps::gps(QWidget *parent) :
9     QWidget(parent),
10     ui(new Ui::gps)
11 {
12     ui->setupUi(this);
13 
14     ui->cmb_com->clear();
15     ui->cmb_com->addItem("ttymxc0");
16     ui->cmb_com->addItem("ttymxc1");
17     ui->cmb_com->addItem("ttymxc2");
18     ui->cmb_com->addItem("ttymxc3");
19 
20     m_timer = new QTimer;
21     connect(m_timer, SIGNAL(timeout()), this, SLOT(read_sermios()));
22 }
23 
~gps()24 gps::~gps()
25 {
26     delete ui;
27 }
28 
on_pb_read_clicked()29 void gps::on_pb_read_clicked()
30 {
31     set_termios();
32     m_timer->start(1000);
33 }
34 
on_pb_close_clicked()35 void gps::on_pb_close_clicked()
36 {
37     m_timer->stop();
38     ui->tedit_output->clear();
39 }
40 
set_termios()41 int gps:: set_termios()
42 {
43     int bound=ui->cmb_bound->currentText().toInt();
44     QString m_com="/dev/" + ui->cmb_com->currentText();
45     const char *com_name=m_com.toUtf8().constData();
46 
47     fd = open(com_name, O_RDWR);
48     if(fd<0)
49     {
50        QMessageBox::about(NULL, "About", "com open error");
51        return -1;
52     }
53 
54     /*set nonblock*/
55     fcntl(fd,F_SETFL,O_NONBLOCK);
56 
57      struct termios terminfo;
58 
59       if (bound == 4800)
60       {
61           terminfo.c_cflag = B4800 | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
62       }
63       if (bound == 9600)
64       {
65           terminfo.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
66       }
67       if (bound == 19200)
68       {
69           terminfo.c_cflag = B19200| CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
70       }
71       //if (bound == 38400)
72       if (bound == 115200)
73       {
74           //terminfo.c_cflag = B38400 | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
75           terminfo.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
76       }
77      // if (bound == 115200)
78       //{
79        //   terminfo.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/
80       //}
81       terminfo.c_iflag = IGNPAR; /*input flag*/
82       terminfo.c_oflag = 0;		/*output flag*/
83       terminfo.c_lflag = 0;/* local flag */
84       terminfo.c_cc[VMIN]=1;
85       terminfo.c_cc[VTIME]=0;
86 
87       tcflush(fd, TCIFLUSH);/*clear */
88       tcsetattr(fd,TCSANOW,&terminfo);/*set attrib	  */
89 
90       return 1;
91 }
92 
93 
read_sermios()94 void gps::read_sermios()
95 {
96      gps_info gpsinfo;
97      int i=0,j=0;
98      char c;
99      char buf[1024];
100 
101      while(true)
102      {
103            read(fd,&c,1); /* read sermios */
104            buf[i++] = c;
105            j++;
106            if(c == '\n' )
107              {
108                  strncpy(m_buf,buf,i);
109                  i=0;
110                  j=0;
111                  break;
112               }
113             if (j>1024)
114             {
115                 ui->tedit_output->setText("The baud rate can't be read");
116                 m_timer->stop();
117                 break;
118             }
119        }
120 
121      parse_gps(m_buf,&gpsinfo);
122      if ( gpsinfo.D.year > 1900 && gpsinfo.D.year < 2100)
123      {
124        QString date = QString("%1-%2-%3").arg(gpsinfo.D.year).arg(gpsinfo.D.month).arg(gpsinfo.D.day);
125        QString time = QString("%1:%2:%3").arg(gpsinfo.D.hour).arg(gpsinfo.D.minute).arg(gpsinfo.D.second);
126        QString Latitude = QString("%1 %2").arg(gpsinfo.NS).arg(gpsinfo.latitude);
127        QString Longtitude = QString("%1 %2").arg(gpsinfo.EW).arg(gpsinfo.longitude);
128        QString high = QString("%1").arg(gpsinfo.high);
129        QString status = QString("%1").arg(gpsinfo.status);
130        QString num = QString("%1").arg(gpsinfo.num);
131 
132        QString text = "Date:          " + date +
133                   "\nTime:          " + time +
134                   "\nStatus:        " + status+
135                   "\nLatitude:      " + Latitude +
136                   "\nLongtitude:  " + Longtitude +
137                   "\nHigh:           " + high +
138                    "\nNum:           " + num;
139 
140        ui->tedit_output->setText(text + "\n\n\n" + ui->tedit_output->toPlainText() );
141       }
142 
143 
144 }
145 
parse_gps(char * mbuf,gps_info * m_gps)146 void gps:: parse_gps(char *mbuf,gps_info *m_gps)
147 {
148     GetClear(m_gps);
149     int temp=0;
150     char c;
151     char* buf=mbuf;
152     c=buf[5];
153    //printf("c=%c ,buf= %s\n",c,buf);
154 
155     if(c=='A') //"$GPGGA"
156     {
157        m_gps->high     = get_double_number(&buf[Getbufnum(9,buf)]);
158        m_gps->num      = (buf[Getbufnum(7,buf)]-'0')*10;
159     }
160 
161     if(c=='C')//"GPRMC"
162     {
163        m_gps->D.hour   =(buf[ 7]-'0')*10+(buf[ 8]-'0');
164        m_gps->D.minute =(buf[ 9]-'0')*10+(buf[10]-'0');
165        m_gps->D.second =(buf[11]-'0')*10+(buf[12]-'0');
166        temp = Getbufnum(9,buf);
167        m_gps->D.day    =(buf[temp+0]-'0')*10+(buf[temp+1]-'0');
168        m_gps->D.month  =(buf[temp+2]-'0')*10+(buf[temp+3]-'0');
169        m_gps->D.year   =(buf[temp+4]-'0')*10+(buf[temp+5]-'0')+2000;
170 
171        m_gps->status   =buf[Getbufnum(2,buf)];
172        m_gps->latitude =get_double_number(&buf[Getbufnum(3,buf)]);
173        m_gps->NS       =buf[Getbufnum(4,buf)];
174        m_gps->longitude=get_double_number(&buf[Getbufnum(5,buf)]);
175        m_gps->EW       =buf[Getbufnum(6,buf)];
176        #ifdef USE_BEIJING_TIMEZONE
177                BTC(&m_gps->D);
178        #endif
179      }
180 }
181 
182 
183 //Specified number of commas position
Getbufnum(int num,char * str)184 int gps::Getbufnum(int num,char *str)
185 {
186         int i,j=0;
187         int len=strlen(str);
188         for(i=0;i<len;i++)
189         {
190                 if(str[i]==',')
191                     j++;
192                 if(j==num)
193                     return i+1;
194         }
195         return 0;
196 }
197 
198 //clear gps_info
GetClear(gps_info * m_gps)199 void gps::GetClear(gps_info *m_gps)
200 {
201     m_gps->D.hour   =0.0;
202     m_gps->D.minute =0.0;
203     m_gps->D.second =0.0;
204     m_gps->D.day    =0.0;
205     m_gps->D.month  =0.0;
206     m_gps->D.year   =0.0;
207     m_gps->status	=' ';
208     m_gps->latitude =0.0;
209     m_gps->NS       =' ';
210     m_gps->longitude=0.0;
211     m_gps->EW       =' ';
212     m_gps->num      =0;
213 
214 }
get_double_number(char * s)215 double gps::get_double_number(char *s)
216 {
217         char buf[128];
218         int i;
219         double rev;
220         i=Getbufnum(1,s);
221         strncpy(buf,s,i);
222         buf[i]=0;
223         rev=atof(buf);
224         return rev;
225 }
226 
227 
228 //Will the world into Beijing
BTC(date_time * gps_d)229 void gps::BTC(date_time *gps_d)
230 {
231 //If the second number first, then the time data, the time data of +1 seconds
232                 gps_d->second++; //Plus a second
233                 if(gps_d->second>59)
234                 {
235                        gps_d->second=0;
236                         gps_d->minute++;
237                         if(gps_d->minute>59)
238                         {
239                                gps_d->minute=0;
240                                gps_d->hour++;
241                         }
242                 }
243 
244 //***************************************************
245                 gps_d->hour+=8;
246                 if(gps_d->hour>23)
247                 {
248                        gps_d->hour-=24;
249                         gps_d->day+=1;
250                         if(gps_d->month==2 ||
251                                       gps_d->month==4 ||
252                                       gps_d->month==6 ||
253                                       gps_d->month==9 ||
254                                       gps_d->month==11 )
255                         {
256                                 if(gps_d->day>30)
257                                 {
258                                       gps_d->day=1;
259                                       gps_d->month++;
260                                 }
261                         }
262                         else
263                         {
264                                 if(gps_d->day>31)
265                                 {
266                                        gps_d->day=1;
267                                        gps_d->month++;
268                                 }
269                         }
270                         if(gps_d->year % 4 == 0 )
271                         {//
272                                 if(gps_d->day > 29 && gps_d->month ==2)
273                                 {
274                                        gps_d->day=1;
275                                        gps_d->month++;
276                                 }
277                         }
278                         else
279                         {
280                                 if(gps_d->day>28 &&gps_d->month ==2)
281                                 {
282                                         gps_d->day=1;
283                                         gps_d->month++;
284                                 }
285                         }
286                         if(gps_d->month>12)
287                         {
288                                gps_d->month-=12;
289                                gps_d->year++;
290                         }
291                 }
292 }
293 
294 
changeEvent(QEvent * e)295 void gps::changeEvent(QEvent *e)
296 {
297     QWidget::changeEvent(e);
298     switch (e->type())
299     {
300        case QEvent::LanguageChange:
301             ui->retranslateUi(this);
302             break;
303        default:
304             break;
305     }
306 }
307 
closeEvent(QCloseEvent * e)308 void gps::closeEvent(QCloseEvent *e)
309 {
310     on_pb_close_clicked();
311     exit(0);
312 }
313 
moveEvent(QMoveEvent *)314 void gps::moveEvent(QMoveEvent *)
315 {
316     this->move(QPoint(0,0));
317 }
318 
resizeEvent(QResizeEvent *)319 void gps::resizeEvent(QResizeEvent *)
320 {
321     this->showMaximized();
322 }
323 
324