{"id":23,"date":"2006-06-01T14:15:08","date_gmt":"2006-06-01T19:15:08","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/processing\/23"},"modified":"2007-08-06T23:27:38","modified_gmt":"2007-08-07T04:27:38","slug":"basic-gpsnmea-0183-reader-in-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/23\/","title":{"rendered":"basic GPS\/NMEA 0183 reader in Processing"},"content":{"rendered":"<p><P><br \/>\nThis program reads a serial string from a GPS reader.  It parses the sentence and returns latitude and longitude.\n<\/p>\n<p>\nIt&#8217;s also a simple example of how to read an ASCII-formatted, comma-delimited serial protocol.\n<\/p>\n<p><!--more--><\/p>\n<pre>\n\/* \n GPS NMEA 0183 reader\n \n reads a GPS string in the NMEA 0183 format and returns lat\/long. \n \n For more on GPS and NMEA, see the NMEA FAQ:\n http:\/\/vancouver-webpages.com\/peter\/nmeafaq.txt\n \n by Tom Igoe\n created 1 June 2006\n *\/\n\nimport processing.serial.*;\n\nint linefeed = 10;       \/\/ linefeed in ASCII\nint carriageReturn = 13; \/\/ carriage return in ASCII\nSerial myPort;           \/\/ The serial port\nint sensorValue = 0;     \/\/ the value from the sensor\n\nfloat latitude = 0.0;    \/\/ the latitude reading in degrees\nString northSouth;       \/\/ north or south?\nfloat longitude = 0.0;   \/\/ the longitude reading in degrees\nString eastWest;         \/\/ east or west?\n\n\nvoid setup() {\n  \/\/ List all the available serial ports\n  println(Serial.list());\n\n  \/\/ I know that the first port in the serial list on my mac\n  \/\/ is always my  Arduino, so I open Serial.list()[0].\n  \/\/ Open whatever port is the one you're using.\n  myPort = new Serial(this, Serial.list()[0], 4800);\n\n  \/\/ read bytes into a buffer until you get a linefeed (ASCII 13):\n  myPort.bufferUntil(carriageReturn);\n}\n\nvoid draw() {\n  \/\/ not doing anything here\n}\n\n\/*\n  serialEvent  method is run automatically by the Processing applet\n  whenever the buffer reaches the  byte value set in the bufferUntil() \n  method in the setup():\n*\/\n\nvoid serialEvent(Serial myPort) { \n  \/\/ read the serial buffer:\n  String myString = myPort.readStringUntil(linefeed);\n  \/\/ if you got any bytes other than the linefeed:\n  if (myString != null) {\n    \/\/ parse the string:\n    parseString(myString);\n  }\n} \n\n\/*\n  parseString takes the string and looks for the $GPGLL header.\n  if it finds it, it splits the string into components at the commas,\n  and stores them in appropriate variables.\n*\/\n\nvoid parseString(String serialString) {\n  \/\/ split the string at the commas\n  \/\/and convert the sections into integers:\n  String items[] = (split(serialString, ','));\n  \/\/ if the first item in the sentence is our identifier, parse the rest:\n  if (items[0].equals(\"$GPGLL\") == true) {\n    \/\/ move the items from the string into the variables:\n    latitude = float(items[1]);\n    northSouth = items[2];\n    longitude = float(items[3]);\n    eastWest = items[4];\n    println(latitude + northSouth + \",\" +longitude + eastWest);\n  }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For more on GPS and NMEA, see the NMEA FAQ: http:\/\/vancouver-webpages.com\/peter\/nmeafaq.txt by Tom Igoe created 1 June 2006 *\/ import processing.serial.*; int linefeed = 10; \/\/ linefeed in ASCII int carriageReturn = 13; \/\/ carriage return in ASCII Serial myPort; \/\/ The serial port int sensorValue = 0; \/\/ the value from the sensor float latitude = 0.0; \/\/ the latitude reading in degrees String northSouth; \/\/ north or south?&#8230;  myPort = new Serial(this, Serial.list()[0], 4800); \/\/ read bytes into a buffer until you get a linefeed (ASCII 13): myPort.bufferUntil(carriageReturn); } void draw() { \/\/ not doing anything here } \/* serialEvent method is run automatically by the Processing applet whenever the buffer reaches the byte value set in the bufferUntil() method in the setup(): *\/ void serialEvent(Serial myPort) { \/\/ read the serial buffer: String myString = myPort.readStringUntil(linefeed); \/\/ if you got any bytes other than the linefeed: if (myString != null) { \/\/ parse the string: parseString(myString); } } \/* parseString takes the string and looks for the $GPGLL header.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}