Shop OBEX P1 Docs P2 Docs Learn Events
is there any C code for GPS — Parallax Forums

is there any C code for GPS

grapple53grapple53 Posts: 2
edited 2014-06-11 19:43 in Accessories
I am more familiar with C++ so i am using the activity board and want to get GPS capabilities for accurate time and location readings. If anyone has any suggestions please let me know i could really use the help.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2013-11-20 05:01
    I've written a generic NMEA0183 string parser here which can be used to "chunk" an input stream into NMEA strings. It's in C++ but would be easy to convert to C. On top of that you'd have to add code to parse the string into ints, but that's pretty easy. I also have an extension for the MTK3339 gps here which defines a few things like baud and output sentences.
  • grapple53grapple53 Posts: 2
    edited 2013-11-20 09:37
    Thank you for the help, looks like a great point for me too start my project.
  • mnastasimnastasi Posts: 9
    edited 2014-05-19 19:51
    //can someone help; i am using GPS hardware BPN1513 // http://www.parallax.com/sites/default/files/downloads/28501-PMB-688-v0.1.pdf with propeller c;
    // i am getting some nmea text... however, getting error when it prints this line: $GPGGA,024749.000,3944.6649,N,07500.3298,W,1,06,1.1,54.6,M,-34.0,M,


    //MY CODE BELOW
    /**
    * This is the main serial_inout program file.
    */
    #include "simpletools.h"
    #include "fdserial.h"


    #define NONE 0
    #define DEC 1
    #define HEX 2
    #define BIN 3
    #define CHR 4


    void serout(text_t *ser, int delay, char* string, int type, int val, int end);
    void serin(text_t *ser, int wait, void (*function)(), int type, void* value);


    void printnewline() {
    print("\n");
    }


    int main(void)
    {
    int inpin = 3;
    int outpin= 2;
    int baud = 9600;
    int value = 0x55;


    text_t *ser = fdserial_open(inpin, outpin, 0, baud);


    int number, i;
    char location[512];
    i=-1;


    pause(200);
    print("SEROUT/SERIN DEMO\n");


    char c;

    while(1)
    {
    //ser = fdserial_open(inpin, outpin, 0, baud);
    //serout(ser, 100, "HELLO", 0, value, CR);
    //serout(ser, 1, "DEC VAL ", DEC, value, CR);
    //serout(ser, 1, "HEX VAL ", HEX, value, CR);
    //serout(ser, 1, "BIN VAL ", BIN, value, CR);
    //serout(ser, 1, "CHR VAL ", CHR, value, CR);

    //print("Enter DEC: ");
    // serin(ser, 5000, printnewline, DEC, &value);
    // print("Got DEC %d\n", value);

    //print("Enter HEX: ");
    // serin(ser, 5000, printnewline, HEX, &value);
    //print("Got HEX %x\n", value);

    //print("Enter BIN: ");
    // serin(ser, 5000, printnewline, BIN, &value);
    //print("Got BIN %b\n", value);
    //http://www.parallax.com/sites/default/files/downloads/28501-PMB-688-v0.1.pdf
    //print("Enter CHR: ");
    // value="";
    serin(ser, 5000, printnewline, CHR, &value);

    print("Got Char %c\n", value);
    //http://www.parallax.com/sites/default/files/downloads/28506-VPN1513Datasheet.pdf

    //c = fdserial_rxChar(ser);
    //if(c != -1)
    //{
    // print("You typed: %c\n", c);
    //}
    // print("value %c\n",text_t);

    if(value =='$')
    {

    // print("gps: \n");

    // do
    // {
    //i++;
    // location = readChar(ser);
    //i = i+1;
    // print("loc num %d\n" ,i);

    //}while(location[i-1]!='\n');//while(location[i-1]!='*');//while(i<512);// while(location!='\n');

    i = 0;
    location = readChar(ser);
    while(location!='*')
    {
    location = readChar(ser);
    i++;
    }

    // if(charindex("$GPRMC",location)>0)
    //{
    //print("gps coord");
    //}
    // location=0;
    print("location %s",location);
    // return;
    pause(100);
    }
    else
    {
    // location[] = "";
    // print("no $$ \n\n");
    serin(ser, 5000, printnewline, CHR, &value);
    // text_t *ser = fdserial_open(inpin, outpin, 0, baud);
    // print("%s",text_t);
    // pause(100);
    }
    // ser = "";
    //serin(ser, 1000, printnewline, CHR, &value);
    }


    return 0;
    }


    void serout(text_t *ser, int delay, char* string, int type, int val, int end)
    {
    int n = 0;
    int len = strlen(string);


    for(n = 0; n < len; n++) {
    writeChar(ser, string[n]);
    pause(delay);
    }


    if(DEC == type) {
    writeDec(ser, val);
    pause(delay);
    writeChar(ser, end);
    }
    else if(HEX == type) {
    writeHex(ser, val);
    pause(delay);
    writeChar(ser, end);
    }
    else if(BIN == type) {
    writeBin(ser, val);
    pause(delay);
    writeChar(ser, end);
    }
    else if(CHR == type) {
    writeChar(ser, val);
    pause(delay);
    writeChar(ser, end);
    }
    else {
    writeChar(ser, end);
    }
    }




    void serin(text_t *ser, int wait, void (*function)(), int type, void* value)
    {
    //print("dddd\n");
    char buf[33]; // big enough for hex, dec, bin, or char


    while(--wait > -1) {
    if(fdserial_rxReady(ser))
    break;
    pause(1);
    }
    if(wait < 0) {
    // writeLine(ser, "No input");
    //print("nodata \n");
    function();
    return;
    }


    readStr(ser, buf, 33);


    if(DEC == type) {
    sscan(buf, "%d", value);
    }
    else if(HEX == type) {
    sscan(buf, "%x", value);
    }
    else if(BIN == type) {
    sscan(buf, "%b", value);
    }
    else if(CHR == type) {
    sscan(buf, "%c", value);
    }
    }
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-19 21:10
    mnastasi wrote: »
    // i am getting some nmea text... however, getting error when it prints this line: $GPGGA,024749.000,3944.6649,N,07500.3298,W,1,06,1.1,54.6,M,-34.0,M,

    What do you mean by "getting error"? Is the above line the output from the program? What where you expecting?

    If you use code tags when you post code, it will be much easier for us to read.

    [noparse]
    // Your code here.
    // Indented code.
    
    [/noparse]

    The above will show up as:
      // Your code here.
        // Indented code.
    

    Without code tags the forum software removes indentations which destroys the formatting of the code.
  • mnastasimnastasi Posts: 9
    edited 2014-05-20 19:42
    sorry here is my code
    /**
     * This is the main serial_inout program file.
     */
    #include "simpletools.h"
    #include "fdserial.h"
    
    
    #define NONE 0
    #define DEC 1
    #define HEX 2
    #define BIN 3
    #define CHR 4
    
    
    void serout(text_t *ser, int delay, char* string, int type, int val, int end);
    void  serin(text_t *ser, int wait, void (*function)(), int type, void* value);
    
    
    void printnewline() {
      print("\n");
    }
    
    
    int main(void)
    {
      int inpin = 3;
      int outpin= 2;
      int baud  = 9600;
      int value = 0x55;
    
    
      text_t *ser = fdserial_open(inpin, outpin, 0, baud);
    
    
      int number, i;
      char location[512];
      i=-1;
    
    
      pause(200);
      print("SEROUT/SERIN DEMO\n");  
      
      
      char c;
     
      while(1)
        {
        //  char location[512];
          int value;
          //ser = fdserial_open(inpin, outpin, 0, baud);
          //serout(ser, 100, "HELLO", 0, value, CR);
          //serout(ser, 1, "DEC VAL ", DEC, value, CR);
          //serout(ser, 1, "HEX VAL ", HEX, value, CR);
          //serout(ser, 1, "BIN VAL ", BIN, value, CR);
          //serout(ser, 1, "CHR VAL ", CHR, value, CR);
      
          //print("Enter DEC: ");
        //  serin(ser, 5000, printnewline, DEC, &value);
         // print("Got DEC %d\n", value);
      
          //print("Enter HEX: ");
         // serin(ser, 5000, printnewline, HEX, &value);
          //print("Got HEX %x\n", value);
      
          //print("Enter BIN: ");
        //  serin(ser, 5000, printnewline, BIN, &value);
          //print("Got BIN %b\n", value);
      //http://www.parallax.com/sites/default/files/downloads/28501-PMB-688-v0.1.pdf
          //print("Enter CHR: ");
        //  value="";
          serin(ser, 3000, printnewline, CHR, &value);
      
          print("Got Char %c\n", value);
      //http://www.parallax.com/sites/default/files/downloads/28506-VPN1513Datasheet.pdf
         
          //c = fdserial_rxChar(ser);
          //if(c != -1)
          //{
           // print("You typed: %c\n", c);
          //}
         // print("value %c\n",text_t);
      
          if(value =='$')
          {
             
         //   print("gps: \n");
          
           // do
           // { 
              //i++;
             // location[i] = readChar(ser);          
              //i = i+1;
             // print("loc num %d\n" ,i);
              
            //}while(location[i-1]!='\n');//while(location[i-1]!='*');//while(i<512);// while(location[i]!='\n');
            
            i = 0;
            location[i] = readChar(ser);
            while((location[i] != '*') && (location[i]!='\n') && (location[i]!='\r') && (location[i]!='?') )
            {          
              location[i] = readChar(ser);
              i++;
              if(i>=512)
              {
                print("count is %d\n",i);
                i=0;
              }
            //  if(location[i]=="?")
              //{
                //print("-------block====");
                //i=0;
                //break;
              //}
              if(location[i]=="*")
              {
                print("-------***************====");
                i=0;
                //break;
              }
              //if(location[i]=="*")
              //{
                //break;
              //}     
            }
            
          
         // if(charindex("$GPRMC",location)>0)
          //{
            //print("gps coord");
          //}
         //  location[i]=0;
           
           // return;
              if(strstr(location, "GPGGA") == NULL)
              {
                   print("Not a GPGGA string\n"); 
                   // char location[512];
                   // break;  
                   //EDIT
                  // strcpy(globalString, "");//if failed, do not want globalString populated
              }
              else
              {    //EDIT
                    print("---is  gpgga\n");
                    print("location %s",location);
                    //char location[512];
          
                 //  strcpy(globalString, myString);
              }
               i=0;   
        //      if(strstr(location, "*") != NULL )
        //    {
          //    char location[512];
            //  serin(ser, 5000, printnewline, CHR, &value);
            
            //}
     
           pause(100);
          }
          else
          {
             // location[] = "";
         //   print("no $$ \n\n");
            i=0;
                    //char location[512];
            serin(ser, 3000, printnewline, CHR, &value);
            //break;
           // text_t *ser = fdserial_open(inpin, outpin, 0, baud);
         //   print("%s",text_t);
           // pause(100);
          }
        // ser = ""; 
          //serin(ser, 1000, printnewline, CHR, &value);
        }
    
    
      return 0;
    }
    
    
    void serout(text_t *ser, int delay, char* string, int type, int val, int end)
    {
      int n = 0;
      int len = strlen(string);
    
    
      for(n = 0; n < len; n++) {
        writeChar(ser, string[n]);
        pause(delay);
      }
    
    
      if(DEC == type) {
        writeDec(ser, val);
        pause(delay);
        writeChar(ser, end);
      }
      else if(HEX == type) {
        writeHex(ser, val);
        pause(delay);
        writeChar(ser, end);
      }
      else if(BIN == type) {
        writeBin(ser, val);
        pause(delay);
        writeChar(ser, end);
      }
      else if(CHR == type) {
        writeChar(ser, val);
        pause(delay);
        writeChar(ser, end);
      }
      else {
        writeChar(ser, end);
      }
    }
    
    
    
    
    void  serin(text_t *ser, int wait, void (*function)(), int type, void* value)
    {
     //print("dddd\n");
      char buf[33]; // big enough for hex, dec, bin, or char
    
    
      while(--wait > -1) {
        if(fdserial_rxReady(ser))
          break;
        pause(1);
      }
      if(wait < 0) {
       writeLine(ser, "No input");
        //print("nodata \n");
        function();
        return;
      }
    
    
      readStr(ser, buf, 33);
    
    
      if(DEC == type) {
        sscan(buf, "%d", value);
      }
      else if(HEX == type) {
        sscan(buf, "%x", value);
      }
      else if(BIN == type) {
        sscan(buf, "%b", value);
      }
      else if(CHR == type) {
        sscan(buf, "%c", value);
      }
    }
    
  • mnastasimnastasi Posts: 9
    edited 2014-06-11 19:43
    Is there any progress on propeller c code for GPS?
Sign In or Register to comment.