Im having trouble receiving/displaying the data sent by my arduino sketch in viual basic, was wondering if someone could give me a bit of help in this issue, heres my arduino and VB codes:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared serialportused As SerialPort
Dim looop As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With serialportused
'Temporarily closes port
.Close()
'Set the serial port to port 6
.PortName = "COM6"
'Set baud rate, parity, data bits, and stop bits.
.BaudRate = 115200
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
'Set number of bytes before DataRecieved event is raised.
.ReceivedBytesThreshold = 1
'Open serial port.
.Open()
'Sets loop true
looop = True
'Loops reading serial data and displaying in textbox
Do While looop = True
tbxSerial.Text = serialportused.ReadLine() + vbCrLf
Loop
End With
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Closes program
Me.Close()
End Sub
End Class
Kept everything as simple as i could for now, and would expand on it later. I have confirmed the arduino is using COM6 on the PC and that it is sending serial data.
The Serial instance uses pins 0 and 1. DO NOT DIDDLE WITH THEM.
float inputReading = analogRead(1);
The analogRead function does not return a float.
float voltage = (inputReading);
The parentheses are unnecessary. The white space at the end is unnecessary. In fact, the whole statement is unnecessary.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With serialportused
'Temporarily closes port
.Close()
The port wasn't opened. Don't close something you didn't open.
'Sets loop true
looop = True
'Loops reading serial data and displaying in textbox
Do While looop = True
tbxSerial.Text = serialportused.ReadLine() + vbCrLf
Loop
An infinite loop in the Form_Load event is a really bad idea. Don't even consider it.
The VB app runs an event loop. Let that event loop get started, and let it do the infinite looping.
Actually implementing the DataReceived event would be a good idea.
Fixed my arduino program accordingly, also coincidentally i did change to using the data received handler as i got some similar advice from a colleague, my new VB code is listed below:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel
Public Class Form1
Private Sub spu_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles spu.DataReceived
'Opens port
spu.Open()
'Defines data holder as string, and moves in serial data
Dim indata As String = spu.ReadExisting()
'Displays string in textbox
tbxSerial.Text = indata
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Closes program
Me.Close()
End Sub
End Class
Still no serial data, also not sure if im supposed to loop my read/write function or if thats done automatically, or if i need to open the port or if thats done simply by placing the component. Note: i have a serial port component layed out with all the same specs i listed before, and i named it as "spu".
Also tried without the serial port component, and still no luck:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Defines port
Dim spu As New SerialPort("COM6")
'Sets port details
spu.BaudRate = 115200
spu.Parity = Parity.None
spu.StopBits = StopBits.One
spu.DataBits = 8
spu.Handshake = Handshake.None
'Opens port
spu.Open()
End Sub
Private Sub spu_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles spu.DataReceived
'Moves data into string holder
Dim indata As String = spu.ReadExisting()
'Moves string from holder into textbox
tbxSerial.Text = indata
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Closes program
Me.Close()
End Sub
End Class