Data from Arduino

  1. 4 years ago

    I would like to read some data from the serial port (from an arduino) and to plot a graph.
    Can someone suggest me an example to 'study'?
    Thank you very much and best regards

  2. Jean-Yves P

    3 Dec 2016 Testers, Xojo Pro Europe (France, Besançon)

    all the projects inside deals with the serial port.

  3. Steve K

    3 Dec 2016 Melbourne, Australia

    Hi Bruno,

    What you're wanting to do is pretty much exactly what I'm developing at the moment. That is, reading data from the serial port attached to an Arduino Uno. The data is then used to create a simple XY graph. In my case X is time and Y is force.

    It's certainly nothing new, but I have a few specific requirements, so I decided to write it myself. I'm not a Xojo expert so please take that into account.

    As Jean-Yves suggests above, have a look at the Serial examples that come with Xojo - there is enough there to get you started. Personally, I had issues with using the DataAvailable Event (ie. LF/CR being inserted in the wrong spot) so I used a different approach.

    Using the Windows OS, the general methodology that I used was to create a Timer. The timer fires at 1/2 sec intervals and reads the Serial buffer:

    //Read ALL available data from the serial port buffer
      tempBuffer = Serial1.ReadAll(encodings.ASCII)
      //Add the data to incomingData
      incomingData = incomingData + tempBuffer

    The Text String "incomingData" is then split and written to an array of type integer:

     //split the recorded data and save into array as String
      tempArray = incomingData.Trim.Split(EndOfLine.Windows)
      //read from tempArray, convert rounding to integer
      //write to new array recordedGramsArray as integer:
      redim recordedGramsArray(tempArray.Ubound)
      //read data from tempArray
      for i as Integer = 0 to tempArray.Ubound
        grams = round(val(tempArray(i)))
        //write grams to array
        recordedGramsArray(i) = grams

    The data is read off to create the graph:

    Dim totalSamples as Integer
      totalSamples = recordedGramsArray.Ubound
      Dim forceY as integer
      Dim X1,Y1,X2,Y2 as Double
      For timeX as Integer = 0 to totalSamples-1
        forceY = timeX
        X1 = timeX
        Y1 = kGraphHeight - recordedGramsArray(forceY)
        X2 = timeX+1
        Y2 = kGraphHeight - recordedGramsArray(forceY+1)
        //Draw the final graph lines
        GraphPicture.Graphics.DrawLine(X1, Y1, X2, Y2)
      Next timeX
      //Update the Canvas
      cnvGRAPH.Backdrop = GraphPicture

    As usual, please don't copy the above code and expect it to work - there are some things left out.

    As was suggested to me, you may wish to look at Roger Meier's free data plot classes, it may suit your purpose. I am not clever enough to be able to edit that code to get it to do what I wanted, and therefore have started from scratch.

    Hopefully this helps.


  4. Steve K

    3 Dec 2016 Melbourne, Australia

    Forgot to add, in the Canvas Paint Event, which actually displays the graph:

    g.DrawPicture (GraphPicture, 0, 0)
  5. Thank you very much for your help

or Sign Up to reply!