Real time graphic from rs232

Mister Zippo

Active member
Joined
Dec 27, 2009
Messages
40
Programming Experience
1-3
Hello world.

hoping you can help me.

I'm triyng to create a form that can take the serial input from a pic (one value indicating the inclinometer value from a gyro) and make a graphic inside a picturebox where in real time is possible see the inclination status during the time (like a sinusoid graphic)

I 've already taken succesfully the value from rs232, but can someone of you help me in the construbction of this graphic?

value goes from 0 to 255 depending on inclination.
if you post me something (very helpful) you can call this value taken from serila directly "value".

waiting for your kind help.

all my best
zippo
 
The basic idea is to use the values as base for Point coordinates and draw lines from that. the values will represent the Y-axis, and for example each value be drawn one pixel apart in X-axis. Since after a while the number of values will fill up the picturebox width most likely the older values will be discarded - this leaves a problem if each Y-value has been assigned a X-value already, then all points must be shifted or similar transformation (for example TranslateTransform to first points x). The easiest solution here is to just keep the Y-values and create the points with new X-values each time the graph is drawn. Here is a sample using a Timer to generate random values 0-255 and draw them in real time in Picturebox.
VB.NET:
Private values As New List(Of Integer)
Private rnd As New Random

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    values.Add(rnd.Next(0, 256))
    If values.Count = Me.PictureBox1.ClientSize.Width Then
        values.RemoveAt(0)
    End If
    Me.PictureBox1.Refresh()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    If values.Count > 1 Then
        Dim points As New List(Of Point)
        For x As Integer = 0 To values.Count - 1
            points.Add(New Point(x, values(x)))
        Next
        e.Graphics.DrawLines(Pens.Red, points.ToArray)
    End If
End Sub
If you are using newer VB the Points transformation is even simpler using the Select extension utilizing the value and its index:
VB.NET:
Dim points = values.Select(Function(y, x) New Point(x, y))
Try out the sample first, then I'm sure you can fit in your actual value readings and possibly adjust the scaling to the needs.
 
Dear Jonh,

Many many thanks for precious help. very apreciated.

the first was good. thanks a lot.

all my best
have a nice weekend
zippo
 

Latest posts

Back
Top