Advertisement
Guest User

gpstime.sh

a guest
Sep 24th, 2016
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.81 KB | None | 0 0
  1. # !bin/sh
  2. #
  3. # getloc - read GPS location and echo status, latitude and longitude
  4. #          separated by a space. Status 0 is success.
  5.  
  6. # set the GPS port and speed
  7. gpsport=/dev/ttyS1
  8. gpsspeed=9600
  9. # define the file for storing the current position (maybe useful for other things ;-)
  10. posdata=/tmp/position.gps
  11. locdata=/tmp/location.gps
  12.  
  13. closePort() {
  14.   exec 5<&-
  15. }
  16.  
  17. openPort() {
  18.   stty -F $gpsport $gpsspeed
  19.   exec 5<$gpsport
  20. }
  21.  
  22. # Pass the GPS value and direction (N/S/E/W) to get the
  23. # decimal latitude/longitude.
  24. gpsDecimal() {
  25.     gpsVal=$1
  26.     gpsDir="$2"
  27.     # Integer part of the lat/long
  28.     gpsInt=`echo "scale=0;$gpsVal/100" | bc`
  29.     # Minutes part of the lat/long
  30.     gpsMin=`echo "scale=3;$gpsVal-100*$gpsInt" | bc`
  31.     # Convert minutes to a full decimal value
  32.     gpsDec=`echo "scale=5;$gpsInt+$gpsMin/60" | bc`
  33.     # South and West are negative
  34.     if [ $gpsDir = "W" -o $gpsDir = "S" ]
  35.     then
  36.       gpsDec="-$gpsDec"
  37.     fi
  38.     echo $gpsDec
  39. }
  40.  
  41. # Return statuses
  42. STATUS_OK=0
  43. STATUS_NOFIX_SATDATA=1
  44. STATUS_TIMEOUT=2
  45. STATUS_NOTFOUND=3
  46. STATUS_NOFIX_LOCDATA=4
  47.  
  48. # Status and counter values
  49. foundReliability='false'
  50. foundLocation='false'
  51. linesRead=0
  52.  
  53. openPort
  54. while [ $linesRead -le 100 ]
  55. do
  56.   # Read the next line from the GPS port, with a timeout of 10 seconds.
  57.   read -t 10 RESPONSE <&5
  58.   if [ $? -eq 1 ]
  59.   then
  60.     # Read timed out so bail with error
  61.     closePort
  62.     echo "$STATUS_TIMEOUT 0 0"
  63.     exit 1
  64.   fi
  65.  
  66.   # Fallthrough: line was read. Count it because we have a threshhold
  67.   # for the number of sentences to process before giving up.
  68.   linesRead=`expr $linesRead + 1`
  69.  
  70.   # Get the sentence type.
  71.   sentenceType=`echo $RESPONSE | cut -d',' -f1`
  72.  
  73.   #echo $sentenceType
  74.  
  75.   if [ $sentenceType = '$GPGSA' -a $foundReliability = 'false' ]
  76.   then
  77.     #echo "$RESPONSE"
  78.  
  79.     # Found the "fix information" sentence; see if the reliability
  80.     # is at least 2.
  81.     fixValue=`echo $RESPONSE | cut -d',' -f3`
  82.     #echo "$fixValue"
  83.     if [ $fixValue -ne 2 -a $fixValue -ne 3 ]
  84.     then
  85.       # Insufficient fix quality so bail with error
  86.       closePort
  87.       echo "$STATUS_NOFIX_SATDATA 0 0"
  88.       exit 1
  89.     fi
  90.     # Fallthrough: reliability is sufficient
  91.     foundReliability='true'
  92.     #echo "foundReliability=$foundReliability"
  93.   fi # GPGSA sentence
  94.  
  95.   # $GPRMC,141220.00,A,xxxx.06666,N,xxxxx.26833,W,0.064,,100916,,,D*65
  96.   if [ $sentenceType = '$GPRMC' -a $foundLocation = 'false' ]
  97.   then
  98.     # Found the "recommended minimum data" (GPRMC) sentence;
  99.     # determine if it's "active", which means "valid".
  100.     #
  101.     statusValue=`echo $RESPONSE | cut -d',' -f3`
  102.     if [ $statusValue = 'V' ]
  103.     then
  104.       # Void status; can't use the reading so bail
  105.       closePort
  106.       echo "$STATUS_NOFIX_LOCDATA 0 0"
  107.       exit 1
  108.     fi
  109.  
  110.     #echo "$RESPONSE"
  111.     #echo "$statusValue"
  112.  
  113.     # extract the components of the date/time out of the GPRMC sentence
  114.     #   1      2       3     4      5      6      7    8  9  10 11 12 13
  115.     # $GPRMC,141220.00,A,xxxx.06666,N,xxxxx.26833,W,0.064,,100916,,,D*65
  116.     gpsdate=`echo $RESPONSE | cut -d',' -f10`
  117.     gpstime=`echo $RESPONSE | cut -d',' -f2`
  118.     gpsdate_DD=$(expr substr "$gpsdate" 1 2)
  119.     gpsdate_MM=$(expr substr "$gpsdate" 3 2)
  120.     gpsdate_YY=$(expr substr "$gpsdate" 5 2)
  121.     gpstime_hh=$(expr substr "$gpstime" 1 2)
  122.     gpstime_mm=$(expr substr "$gpstime" 3 2)
  123.     gpstime_ss=$(expr substr "$gpstime" 5 2)
  124.     # execute the set date command with the data from GPRMC sentence
  125.     # date -u -s $gpsdate_MM$gpsdate_DD$gpstime_hh$gpstime_mm"20"$gpsdate_YY"."$gpstime_ss
  126.     #echo "date -u -s 20$gpsdate_YY$gpsdate_MM$gpsdate_DD$gpstime_hh$gpstime_mm"."$gpstime_ss"
  127.     date -u -s 20$gpsdate_YY$gpsdate_MM$gpsdate_DD$gpstime_hh$gpstime_mm"."$gpstime_ss >/dev/null
  128.     #date
  129.  
  130.     # Fallthrough: active status, so we can use the reading.
  131.     foundLocation='true'
  132.     #echo "foundLocation=$foundLocation"
  133.     latitudeValue=`echo $RESPONSE | cut -d',' -f4`
  134.     latitudeNS=`echo $RESPONSE | cut -d',' -f5`
  135.     latitudeDec=$(gpsDecimal $latitudeValue "$latitudeNS")
  136.     longitudeValue=`echo $RESPONSE | cut -d',' -f6`
  137.     longitudeEW=`echo $RESPONSE | cut -d',' -f7`
  138.     longitudeDec=$(gpsDecimal $longitudeValue "$longitudeEW")
  139.   fi # $GPRMC sentence
  140.  
  141.   if [ $foundReliability = 'true' -a $foundLocation = 'true' ]
  142.   then
  143.       linesRead=`expr $linesRead + 200`
  144.   fi
  145.  
  146. done # read-line loop
  147.  
  148. closePort
  149.  
  150. # If we get to here and location and reliability were OK, we
  151. # have a fix.
  152. if [ $foundReliability = 'true' -a $foundLocation = 'true' ]
  153. then
  154.   echo "$STATUS_OK $latitudeDec $longitudeDec"
  155.   echo "$latitudeDec, $longitudeDec" > $locdata
  156.   exit 0
  157. fi
  158.  
  159. # Fallthrough to here means too many lines were read without
  160. # finding location information. Return failure.
  161. echo "$STATUS_NOTFOUND 0 0"
  162. exit 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement