-
Feb 13th, 2018, 04:35 AM
#1
Thread Starter
Member
problem in plotting graph on a picturebox in visual studio 2015 vb.net
Hello everyone,
actually i am plotting a real time graph on a picturebox of a windows form but after plotting some right values my cursor automatically drops to 0 and starts plotting graph from there. The real time values are processed by an micro controller and and communicated to pc via serial communication..... So plz help me to get out of this problem... i shall be very thankfull to all of you..... i am attaching my code and screenshots below.. [CODE]
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
conn.ConnectionString = "Data Source =C:\Users\Chauhan\Desktop\PatientDB.db;version = 3"
conn.Open()
PictureBox1.BackColor = Color.White
PictureBox2.BackColor = Color.White
myPort.PortName = "COM4"
myPort.BaudRate = 9600
myPort.Parity = Parity.None
myPort.DataBits = 8
myPort.StopBits = StopBits.One
myPort.Encoding = Encoding.ASCII
End Sub
Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
inData = myPort.ReadByte
numbers.Enqueue(inData)
' numbers1.Enqueue(inData)
' Dim myResponse As String = myPort.ReadExisting
' UpdateGraph(myResponse)
DataEnter = True
' Yval = inData
' PrintX1()
PrintX2(inData)
End Sub
Function PrintX2(ByVal Xvalue1 As Byte) As Object
' PictureBox1.Image = New Bitmap(500, 500)
' g1 = Graphics.FromImage(PictureBox1.Image)
' Dim gridPen = New Pen(Color.Black, 2)
' g1.DrawLine(gridPen, 0, 0, 100, 100)
x_start = 1
y_start = PictureBox2.Height - 82
mygraphics = PictureBox2.CreateGraphics
' mygraphics.PageUnit = GraphicsUnit.Pixel
'mygraphics.PageScale = 1
' mygraphics.DrawLine(p1, 0, CInt(PictureBox1.Height), CInt(PictureBox1.Width), CInt(PictureBox1.Height))
mygraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
' mygraphics.ScaleTransform(1, 1)
If DataEnter = True Then
mygraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
mygraphics.DrawLine(p2, x_start + k, y_start - temp_ahead1, x_start + (k + 1), y_start - Xvalue1)
temp_ahead1 = Xvalue1
DataEnter = False
Dr = True
End If
k = k + 1
If k = PictureBox2.Width Then
k = 0
mygraphics.Clear(Color.White)
Me.graph()
End If
End Function
-
Feb 13th, 2018, 08:11 AM
#2
Re: problem in plotting graph on a picturebox in visual studio 2015 vb.net
Without having looked carefully at your code, there are two major problems.
Firstly, the DataReceived event is raised on a background thread so you don't do anything from that event handler that directly affects the UI. You need to learn how to marshal a method call to the UI thread.
Secondly, you never call CreateGraphics. If you want to draw on a control then you do so in its Paint event handler.
-
Feb 13th, 2018, 09:15 AM
#3
Re: problem in plotting graph on a picturebox in visual studio 2015 vb.net
You say that the cursor drops to zero. What does the cursor have to do with anything? If you are plotting data coming from anywhere, you shouldn't be dealing with the cursor. Now, if you meant the drawing point, which isn't the cursor, then how do you know that the zero is wrong? If zero is what you got, then zero is what you should plot. That's the first thing to check (after dealing with the points that JMC made). If you are getting a zero, then the problem isn't in the code. If you are not getting a zero, then you have an excellent start to finding the problem.
My usual boring signature: Nothing
-
Feb 13th, 2018, 11:37 AM
#4
Addicted Member
Re: problem in plotting graph on a picturebox in visual studio 2015 vb.net
The updating of graphics can be improved but I will leave that for someone else to help with, what I can help with possibly is the way serial data is captured.
The first thing I would do is create a delegate that will safely handle the serial data once we have received. So first we declare the delegate and data type we want to handle:
Code:
Public Delegate Sub myDelegate(ByVal inData As Byte)
Next we create the delegate subroutine where we can safely pass the data received to the main UI thread. You may not need all the values I included here for example.
Code:
Private Sub inDataDelegate(ByVal indata As Byte)
numbers.Enqueue(indata)
DataEnter = True
PrintX2(indata)
End Sub
Finally we keep the code in the DataReceived event handler to a minimum and pass the data bytes out to the delegate subroutine with "BeginInvoke"
Code:
Private Sub myport_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myport.DataReceived
myport.ReadTimeout = 20
Do While myport.BytesToRead > 0
Try
Dim inData As Byte = myport.ReadByte
Me.BeginInvoke((New myDelegate(AddressOf inDataDelegate)), inData)
Catch
End Try
Loop
End Sub
-
Mar 22nd, 2018, 02:41 AM
#5
Thread Starter
Member
Re: problem in plotting graph on a picturebox in visual studio 2015 vb.net
Thanks For Your Support And Help... Mc_VB
-
Mar 22nd, 2018, 02:42 AM
#6
Thread Starter
Member
Re: problem in plotting graph on a picturebox in visual studio 2015 vb.net
Originally Posted by Mc_VB
The updating of graphics can be improved but I will leave that for someone else to help with, what I can help with possibly is the way serial data is captured.
The first thing I would do is create a delegate that will safely handle the serial data once we have received. So first we declare the delegate and data type we want to handle:
Code:
Public Delegate Sub myDelegate(ByVal inData As Byte)
Next we create the delegate subroutine where we can safely pass the data received to the main UI thread. You may not need all the values I included here for example.
Code:
Private Sub inDataDelegate(ByVal indata As Byte)
numbers.Enqueue(indata)
DataEnter = True
PrintX2(indata)
End Sub
Finally we keep the code in the DataReceived event handler to a minimum and pass the data bytes out to the delegate subroutine with "BeginInvoke"
Code:
Private Sub myport_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myport.DataReceived
myport.ReadTimeout = 20
Do While myport.BytesToRead > 0
Try
Dim inData As Byte = myport.ReadByte
Me.BeginInvoke((New myDelegate(AddressOf inDataDelegate)), inData)
Catch
End Try
Loop
End Sub
It Works Thanks For Your Support And Help....Mc_VB...
Thank You Very Much
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|