03-11-2016 04:08 AM
Hello, I was wondering if it is possible to graph the data from a serial port in VB.NET using a measurement studio scatter graph . So far, I am able to send a message to an Arduino microcontrollers serial port and when it receives this it will send its current analog input reading to a label in VB.NET. This only happens each time I click write and I am not sure how to make it happen regularly.
This is my VB.NET code-
Imports NationalInstruments.Analysis.SignalGeneration
Imports NationalInstruments.UI.WindowsForms
Imports NationalInstruments.UI
Imports System.IO.Ports
Public Class Form1
Dim i As Integer
Dim x() As Double
Dim y() As Double
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Arduino.Close()
Arduino.PortName = "COM9" 'change com port to match your Arduino port
Arduino.BaudRate = 9600
Arduino.DataBits = 8
Arduino.Parity = Parity.None
Arduino.StopBits = StopBits.One
Arduino.Handshake = Handshake.None
Arduino.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Btnwrite_Click(sender As Object, e As EventArgs) Handles btnwrite.Click
Dim j As Integer
Arduino.Open()
Label5.Text = Arduino.ReadLine(j)
Arduino.Close()
j = j + 1
End Sub
Private Sub tmrRealtimeData_Tick(sender As Object, e As EventArgs) Handles tmrRealtimeData.Tick
x(i) = i
y(i) = Arduino.ReadLine()
Label3.Text = y(i)
scg1.PlotXYAppend(x(i), y(i))
i = i + 1
End Sub
Private Sub Btngraph_Click(sender As Object, e As EventArgs) Handles Btnquit.Click
Dim r As Integer
r = MsgBox("Are you sure you want to exit the application?", MsgBoxStyle.YesNo, "Flowmeter Calibration Experiment")
If r = vbYes Then
Me.Dispose()
End If
End Sub
End Class
Any help is greatly appreciated since this is a college final year project, thank you!!
03-15-2016 08:52 AM
Hello,
I am Ed from National Instruments Applications Engineering. I have been forwarded your query with regards to reading COM ports in visual basic which I am happy to assist you with.
Since this is a college project I will not be able to modify your code, however I can provide some direction as to how you can achieve what you would like. From what I have found thus far you will need to modify your code such that the code within the BtnWrite function is within it's own class and function. Then encapsulate this code within a while loop and have a wait function to control the regularity of the label being updated.
You can then call to start the thread using the Form1_Load function or by having a start button on your user interface. The thread will then run alongside other threads within the application, I would also additionally include a Stop button so that you may also then end the execution of the thread when needed so that the COM ports are released. Please find linked below two articles which I believe should be helpful in providing you with some methodologies to achieve this:
1. https://msdn.microsoft.com/en-us/library/aa289496(v=vs.71).aspx
2. https://support.microsoft.com/en-us/kb/212667
With regards to plotting the data to a graph, I believe the below linked example by Microsoft should be helpful in illustrating the methodology required:
https://support.microsoft.com/en-us/kb/178719
I hope that this is helpful and you are successful in your application.
Best regards,
Ed
03-15-2016 03:41 PM
I would think about triggering a method on serial port data received event to plot the data. If you set up your Arduino and PC correctly then you should only get the event when the complete line is in the serial port receive buffer (rather than sitting in the readline for the CR to come in).