Author Topic: How to get GPS sentences to variable numbers  (Read 5235 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to get GPS sentences to variable numbers
« Reply #15 on: October 21, 2019, 06:41:31 pm »
Hi acjacques
just to summarize all good tips coming from community...
here an idea to manage byte for byte the getting and the parsing of NMEA0183 sentence...

looking at here we can get more informations  https://en.wikipedia.org/wiki/NMEA_0183

Code: QB64: [Select]
  1. ' Steve's suggestion
  2. 'whole$ = "": byte$ = " "
  3. 'DO
  4. '   GET #1, , byte$
  5. '   whole$ = whole$ + byte$
  6. 'LOOP UNTIL byte$ = CHR$10)
  7. 'whole$ = LEFT$(whole$, LEN(whole$) - 2)
  8.  
  9. 'But if we GET one byte$ at time we can just separate each element of the sentence now!
  10.  
  11. 'But since the NMEA sentences are a stream of characters with no fixed length how capture the whole sentence  ?
  12.  
  13. 'so
  14. 'A tipical GPS sentence is like the following serial stream:
  15. '$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A   (followed by CR +LF)
  16. 'CONST CR = CHR$(13), LF = CHR$(10), Start = CHR$(36), CheckSum = CHR$(42), Comma = CHR$(44)
  17. CONST CR = "
  18. ", LF = "
  19. ", Start = "$", CheckSum = "*", Comma = ","
  20. DIM Byte AS STRING * 1, Sentence AS STRING, StringGPS(1 TO 100) AS STRING
  21. DIM Index AS INTEGER
  22.  
  23. IniT
  24. OPEN "I.txt" FOR BINARY AS #1
  25. SearchStartSentence
  26. GettingFields
  27. ShowResults
  28.  
  29.  
  30.  
  31. SUB IniT
  32.     IF _FILEEXISTS("I.txt") THEN KILL "I.txt" 'EXIT SUB
  33.     SHARED Sentence AS STRING, Byte AS STRING * 1
  34.     Sentence = "$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A" + CR + LF
  35.     OPEN "I.txt" FOR OUTPUT AS #1
  36.     PRINT #1, Sentence
  37.     CLOSE #1
  38.     OPEN "I.txt" FOR INPUT AS #1
  39.     LINE INPUT #1, Sentence
  40.     PRINT Sentence
  41.     CLOSE #1
  42.     Byte = ""
  43.     Sentence = ""
  44.     PRINT "press a key to elaborate NMEA 0183 sentence..": SLEEP
  45.  
  46. SUB ShowResults
  47.     SHARED Index AS INTEGER, StringGPS() AS STRING, Sentence AS STRING
  48.     DIM A AS INTEGER
  49.     LOCATE 3, 1: COLOR 14, 1: PRINT Sentence
  50.     COLOR 12, 1
  51.     FOR A = 1 TO Index
  52.         LOCATE , 10: PRINT StringGPS(A), "Index "; A
  53.     NEXT
  54.  
  55.  
  56. SUB GettingFields
  57.     SHARED Byte AS STRING * 1, Index AS INTEGER, StringGPS() AS STRING, Sentence AS STRING
  58.     DIM A AS INTEGER
  59.     WHILE Byte <> LF AND Byte <> CR
  60.         Index = Index + 1
  61.         WHILE Byte <> Comma AND Byte <> CheckSum AND Byte <> CR
  62.             StringGPS(Index) = StringGPS(Index) + Byte
  63.             Byte = TakeByte$
  64.         WEND
  65.         Byte = TakeByte$ ' read next character after delimitator "," or "*"  or CR
  66.     WEND
  67.     FOR A = 1 TO Index
  68.         Sentence = Sentence + "\" + StringGPS(A)
  69.     NEXT
  70.  
  71. SUB SearchStartSentence
  72.     SHARED Byte AS STRING * 1
  73.     WHILE Byte <> Start
  74.         Byte = TakeByte$
  75.     WEND
  76.  
  77. FUNCTION TakeByte$
  78.     DIM Byte AS STRING * 1
  79.     GET #1, , Byte
  80.     TakeByte$ = Byte
  81.  

this code example creates a txt file containing the example sentence posted by you here and terminated by CF+LF, if file doesn't exist and then go on to read byte for byte and translate the sentence in single fields until the checksum.
Naturally output is on the screen and the fields are in string so you can get value converting string to number using the right variable on the basis of the nature of the number got from sentence.

Thanks to read and try

PS Please don't rem CONST activated and don't unrem CONST deactivated... CHR$ is a internal function and QB64 doesn't like for now to use functions with CONST except some type of color functions if used in a choose manner
Programming isn't difficult, only it's  consuming time and coffee