Serial Communication with Processing Sketch

edited June 2015 in Library Questions

Hi Guys, I'm using the Serial library for communication with a gps module that read the latitude and longitude for a processing sketck. The serial library shows the lat and lon in the sketck but I need to assign this data for a variable. Is this possible ??? How can I do that ??? Thanks.


  • How is the sketch showing the values you're getting? POST THE CODE.

    Can't you just create some variables and save those value?

  • And here is the code :

    import processing.serial.*;
    import de.fhpotsdam.unfolding.mapdisplay.*;
    import de.fhpotsdam.unfolding.utils.*;
    import de.fhpotsdam.unfolding.marker.*;
    import de.fhpotsdam.unfolding.tiles.*;
    import de.fhpotsdam.unfolding.interactions.*;
    import de.fhpotsdam.unfolding.ui.*;
    import de.fhpotsdam.unfolding.*;
    import de.fhpotsdam.unfolding.core.*;
    import de.fhpotsdam.unfolding.mapdisplay.shaders.*;
    import de.fhpotsdam.unfolding.geo.*;
    import de.fhpotsdam.unfolding.texture.*;
    import de.fhpotsdam.utils.*;
    import de.fhpotsdam.unfolding.providers.*;
    UnfoldingMap map;
    Serial portaserial; // objeto criado da classe serial.
    String dadosgps; // var. q ira armazenar os dados recebidos da porta serial.
    void setup() {
      String portName = Serial.list()[0]; // porta no qual o arduino ira se comunicar
      portaserial = new Serial(this, portName, 9600); 
            size(800, 600);
            map = new UnfoldingMap(this);
            MapUtils.createDefaultEventDispatcher(this, map);
            map = new UnfoldingMap(this, new Google.GoogleMapProvider());
            map.zoomAndPanTo(new Location(-31.3199163,-54.0902512), 14);
    void draw() {
      if ( portaserial.available() > 0) 
      dadosgps = portaserial.readStringUntil('\n');         
        Location location = map.getLocation(mouseX, mouseY);
        text(location.getLat() + ", " + location.getLon(), mouseX, mouseY);
        Location berlinLocation = new Location(-31.3252781,-54.0958781);
        Location dublinLocation = new Location(-31.315926,-54.107566);
    // Create point markers for locations
    SimplePointMarker berlinMarker = new SimplePointMarker(berlinLocation);
    SimplePointMarker dublinMarker = new SimplePointMarker(dublinLocation);
    // Adapt style
    berlinMarker.setColor(color(255, 0, 0, 100));
    berlinMarker.setStrokeColor(color(255, 0, 0));
    // Add markers to the map
    map.addMarkers(berlinMarker, dublinMarker);
  • On line 49 you are creating a local variable (called location). Then you are getting the Lat and Long values out of it on line 51. It appears like you already have an example of storing location data to a variable! What's not working about it for you? What do you want it to differently?

  • In the line 49, it's just shows the lat and lon on the screen, it's not so important.... I need for an example, get the string dadosgps(line 41) and assign this data for example, to line 53 ( new location(lat, lon)). that's it I need to do.

  • edited June 2015

    Ok. Variable dadosgps is a String, presumable of the format "[lat] [lon]". So start by breaking it into two strings with split(), and save them into a String[]. Then convert each String in that array into a number with float(). Try it!

  • I think it's like this for example: dadogps is 34.343434,56.565656 (comma separate):

        float lat =0; 
        float lon =0; 
        if ( portaserial.available() > 0) 
          dadosgps = portaserial.readStringUntil('\n');         
          if (dadosgps != null) {
         dadosgps = trim(dadosgps);
        float[] newLatLon = float(split(dadosgps, ","));
        if (newLatLon >=2) {
        lat = newLatLon[0] ;
        lon = newLatLon[1] ;
        Location fromSerial = new Location(lat, lon);
        SimplePointMarker fromSerialMarker = new SimplePointMarker(fromSerial);
       fromSerialMarker.setColor(color(255, 0, 0, 100));
Sign In or Register to comment.