Thanks a lot for your post
Thanks a lot for your post MrDan. It gave me some good research starting points
I think I get the threading and delegates stuff now. I used yours and some other similar code from several places and now have a line tracking across the screen in relation to the serial data, sent in byte form from the arduino.
I have a 100ms delay between serial writes on the arduino side and no read delay VB and it works great. Only thing is when I try lowering the arduinos delay to 10ms the VB program cant keep up and the data lags far behind. With the raw values being printed to the debug console, apart from the lag it also looks like the last digit of every number gets chopped too.</p><p>Here
s some of the code. Can you see anywhere it could be sped up?
Thread to continuously update until ended
Private Sub DrawGraph()
Try
Dim y As Integer = m_Y
Do
If SerialPort.BytesToRead > 0 Then
m_Y = SerialPort.ReadByte()
'Debug.Print(m_Y)
’ Plot the new value.
PlotPoint(y, m_Y)
y = m_Y
End If
'Delay a bit before calculating the value.
’ Dim stop_time As Date = Now.AddMilliseconds(10)
'Do While Now < stop_time
'Loop
Loop
Catch ex As Exception
AddStatus("[Thread] " & ex.Message)
End Try
End Sub
Method to move bitmap and draw new line
Private Sub PlotPoint(ByVal lastDataPoint As Integer, ByVal newDataPoint As Integer)
If picGraph.InvokeRequired() Then 'If we arent running in the GUI thread, then call this method again but on the GUI thread
picGraph.Invoke(New PlotPoint_Delegate(AddressOf PlotPoint), New Object() {lastDataPoint, newDataPoint})
Else
Dim wid As Integer = picGraph.ClientSize.Width
Dim hgt As Integer = picGraph.ClientSize.Height
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
’ Move the old data one pixel to the left.
gr.DrawImage(picGraph.Image, -1, 0)
’ Erase the right edge and draw guide lines.
gr.DrawLine(Pens.Black, wid - 1, 0, wid - 1, hgt - 1)
gr.DrawLine(majorAxis, wid - 2, hgt - 1, wid - 1, hgt - 1)
For i As Integer = 0 To hgt Step GRID_STEP
gr.DrawLine(minorAxis, wid - 2, hgt - i, wid - 1, hgt - i)
Next i
’ Plot a new pixel.
gr.DrawLine(wfPen, wid - 2, lastDataPoint, wid - 1, newDataPoint)
’ Display the result.
picGraph.Image = bm
picGraph.Refresh()
gr.Dispose()
End If
End Sub