[pre lang="arduino" line="1" file="sketch_dec07a.ino"]
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
int ledPin = 13; // LED test pin
int byteGPS = -1;
char linea[300];
char tmp[10];
char ew;
char ns;
char av;
float Latitude;
float Longitude;
char comandoGPR[7] = "$GPRMC";
int cont=0;
int bien=0;
int conta=0;
int indices[13];
void setup() {
LcdInit();
LcdClear();
memset(tmp, 0x0, 10 * sizeof(char));
pinMode(ledPin, OUTPUT); // Initialize LED pin
//pinMode(rxPin, INPUT);
//pinMode(txPin, OUTPUT);
Serial1.begin(9600);
Serial.begin(115200);
memset(linea, 0x0, 300 * sizeof(char));
}
void loop() {
digitalWrite(ledPin, HIGH);
byteGPS=Serial1.read(); // Read a byte of the serial port
if (byteGPS == -1) { // See if the port is empty yet
delay(100);
} else { // note: there is a potential buffer overflow here!
linea[conta]=byteGPS; // If there is serial port data, it is put in the buffer
conta++;
//Serial.write(byteGPS);
if (byteGPS==13){ // If the received byte is = to 13, end of transmission
// note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
digitalWrite(ledPin, LOW);
cont=0;
bien=0;
// The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
for (int i=1;i<7;i++){ // Verifies if the received command starts with $GPR
if (linea==comandoGPR[i-1]){
bien++;
}
}
if(bien==6){ // If yes, continue and process the data
for (int i=0;i<300;i++){
if (linea==','){ // check for the position of the "," separator
// note: again, there is a potential buffer overflow here!
indices[cont]=i;
cont++;
}
if (linea=='*'){ // ... 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;j<(indices[i+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.println("");
}
Serial.println("---------------");
*/
for (int j=indices[1];j<(indices[2]-1);j++){
av = linea[j+1];
}
for (int j=indices[2],k=0;j<(indices[3]-1);j++,k++){
tmp[k] = linea[j+1];
}
Latitude = GPSTofloat(tmp);
memset(tmp, 0x0, 10 * sizeof(char));
for (int j=indices[3];j<(indices[4]-1);j++){
ns = linea[j+1];
}
for (int j=indices[4],k=0;j<(indices[5]-1);j++,k++){
tmp[k] = linea[j+1];
}
Longitude = GPSTofloat(tmp);
memset(tmp, 0x0, 10 * sizeof(char));
for (int j=indices[5];j<(indices[6]-1);j++){
ew = linea[j+1];
}
sprintf(tmp,"%f",Latitude);
Serial.print(tmp);
Serial.print(ns);
Serial.print(",");
sprintf(tmp,"%f",Longitude);
Serial.print(tmp);
Serial.print(ew);
Serial.println();
}
conta=0; // Reset the buffer
memset(linea, 0x0, 300 * sizeof(char));
}
}
}
float GPSTofloat(char[] *gps){
float x = atof(gps);
return floor(x/100) + floor(floor(x)/100-floor(x/100))*5/3 + (x-floor(x))/600000;
}[/code]
编译时报错
[pre lang="error" line="1"]Arduino: 1.0.6 (Mac OS X), Board: "Arduino Mega 2560 or Mega ADK"
/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/mega /var/folders/p6/xr61pncx78783l_m56dr3brw0000gn/T/build5653185780976774174.tmp/sketch_dec07a.cpp -o /var/folders/p6/xr61pncx78783l_m56dr3brw0000gn/T/build5653185780976774174.tmp/sketch_dec07a.cpp.o
sketch_dec07a:9: error: expected ',' or '...' before '*' token
sketch_dec07a.ino: In function 'void setup()':
sketch_dec07a:22: error: 'LcdInit' was not declared in this scope
sketch_dec07a:23: error: 'LcdClear' was not declared in this scope
sketch_dec07a.ino: In function 'void loop()':
sketch_dec07a.ino:116: warning: format '%f' expects type 'double', but argument 3 has type 'float'
sketch_dec07a.ino:120: warning: format '%f' expects type 'double', but argument 3 has type 'float'
sketch_dec07a.ino: At global scope:
sketch_dec07a:132: error: expected ',' or '...' before '*' token
sketch_dec07a.ino: In function 'float GPSTofloat(char*)':
sketch_dec07a:133: error: 'gps' was not declared in this scope[/code]
不明白为什么
error: expected ',' or '...' before '*' token
error: 'gps' was not declared in this scope |