Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3155|回復: 2
打印 上一主題 下一主題

講解程式碼...

[複製鏈接]
跳轉到指定樓層
1#
發表於 2011-9-20 21:37:08 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include <string.h>
#include <ctype.h>

int ledPin = 13;                  // LED test pin
int rxPin = 0;                    // RX PIN
int txPin = 1;                    // TX TX
int byteGPS=-1;
char linea[300] = "";
char comandoGPR[7] = "$GPRMC";
int cont=0;
int bien=0;
int conta=0;
int indices[13];

void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(9600);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }   
}

void loop() {
   digitalWrite(ledPin, HIGH);
   byteGPS=Serial.read();         // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100);
   } else {
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                     
     Serial.print(byteGPS, BYTE);
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       digitalWrite(ledPin, LOW);
       cont=0;
       bien=0;
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i]==comandoGPR[i-1]){
           bien++;
         }
       }
       if(bien==6){               // If yes, continue and process the data
         for (int i=0;i<300;i++){
           if (linea[i]==','){    // check for the position of the  "," separator
             indices[cont]=i;
             cont++;
           }
           if (linea[i]=='*'){    // ... and the "*"
             indices[12]=i;
             cont++;
           }
         }
         Serial.println("");      // ... and write to the serial port
         Serial.println("");
         Serial.println("---------------");
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]);
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';            
       }                 
     }
   }
}

看著上面的註解
還有些看不太懂

int byteGPS=-1;-----------------為什麼是-1
char linea[300] = "";------------linea[300]是什麼意思
char comandoGPR[7] = "$GPRMC";------------------comandoGPR[7]是什麼意思
int cont=0;
int bien=0;
int conta=0;
int indices[13];
2#
發表於 2011-9-20 21:56:46 | 只看該作者
int byteGPS=-1;-----------------為什麼是-1

=> 因為 loop() 裏有一行  byteGPS=Serial.read(); 如果 serial port 沒有資料 [url=http://arduino.cc/en/Serial/Read]Serial.read()[/url] 會回傳 -1,byteGPS 為 -1 就代表沒資料,所以就把啟始值設成 -1 囉。

char linea[300] = "";------------linea[300]是什麼意思

=> 字元陣列啊,可以容納 300 個 char 的陣列。

char comandoGPR[7] = "$GPRMC";------------------comandoGPR[7]是什麼意思

=> 同上類推,要注意 Arduino 的語言是簡化的 C/C++,不過字串的表示仍然是 null-terminated,也就是說 "$GPRMC" 這個字串包括 "\0" 總共是 7 個字元,所以才將 comandoGPR 的 size 設成 7
3#
 樓主| 發表於 2011-9-22 15:54:52 | 只看該作者
如果想要把經緯度傳到立一台電腦
要如何從這一大串資料截取下來?
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-5-24 20:16 , Processed in 0.132185 second(s), 9 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表