Results 1 to 5 of 5

Thread: Help reading serial data into chart

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Help reading serial data into chart

    Hi, Ive got a windows form made in vb2010 and i want to use the chart feature.
    at the minute Ive got a string coming in from the serial port that is a temperature in this format "20.0" etc where it is a floating point to one place of decimal.
    I am not sure how to get it to display properly, im using this code which Ive tried to adapt, with no success.
    The graph moves along but wont show the correct temperature values probably because it is a string im converting to. I have tried converting to intger and actually changing the temperature format to an integer value but i get an error about being unable to cast when the value is greater than infinity.
    Any help would be greatly appreciated. Thanks

    Dim temperature As String = SerialPort1.ReadExisting
    Chart1.Series("Series1").Points.AddY(temperature)

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help reading serial data into chart

    Firstly, have you confirmed you are reading accurate data from the port even though it is a string? If not, start there. make sure that if you should read 20.0 degrees you serial port is returning "20.0"
    <edit>
    After rereading your post, it sounds like you are.
    </edit>

    Normally when you are charting real world data you do so with the x-axis having time unit of some sort; either an absolute time being when the point was taken, or a relative time like "how long has it been since the first point was taken". Either way, you should be adding an x and y value rather than just a y value.

    The AddY method adds a new Y value to the end of the points collection and in some cases is meaningless to the chart. You should probably be using the AddXY method instead.

    Try this...
    Code:
    dim temperatureString as string = SerialPort1.ReadExisting
    dim temperature as double
    if double.tryparse(temperatureString,temperature) then
         Chart1.Series("Series1").Points.AddXY(Now,temperature)
    endif
    Even with a chart series configured to default settings, that code should display your data provided you have good data.
    kevin
    Last edited by kebo; Mar 7th, 2016 at 07:22 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Re: Help reading serial data into chart

    Quote Originally Posted by kebo View Post
    Firstly, have you confirmed you are reading accurate data from the port even though it is a string? If not, start there. make sure that if you should read 20.0 degrees you serial port is returning "20.0"
    <edit>
    After rereading your post, it sounds like you are.
    </edit>

    Normally when you are charting real world data you do so with the x-axis having time unit of some sort; either an absolute time being when the point was taken, or a relative time like "how long has it been since the first point was taken". Either way, you should be adding an x and y value rather than just a y value.

    The AddY method adds a new Y value to the end of the points collection and in some cases is meaningless to the chart. You should probably be using the AddXY method instead.

    Try this...
    Code:
    dim temperatureString as string = SerialPort1.ReadExisting
    dim temperature as double
    if double.tryparse(temperatureString,temperature) then
         Chart1.Series("Series1").Points.AddXY(Now,temperature)
    endif
    Even with a chart series configured to default settings, that code should display your data provided you have good data.
    kevin

    Hi Kvin,
    Thank you for your reply and help.
    The values are correctly read in by the serial port as they are displayed in a text box. The code you posted seems to work, it seems as though the update interval for the chart is really slow. it takes forever to plot a change.
    i have this code so far.
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ' Dim temperature As Int16 = SerialPort1.ReadExisting()

    'Dim temp_out As Integer
    'temp_out = CInt(temperature)

    ' Dim temperature As String = SerialPort1.ReadExisting
    Dim temperatureString As String = SerialPort1.ReadExisting
    Dim temperature As Double
    If Double.TryParse(temperatureString, temperature) Then
    Chart1.Series("Series1").Points.AddXY(Now, temperature)
    End If
    rtbReceived.Text = temperatureString

    Once again, thank you for your help with this issue. I appreciate it

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help reading serial data into chart

    what is the timer interval?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Help reading serial data into chart

    at what rate is your hardware sending temperature updates?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width