Hello all i am trying to interface my arduino project with visual basic via serial communication.
I have gotten my visual basic program to write to my ardunio successfully. However, i want to allow my
visual basic program to read the incoming serial data from the arduino this i am having problems on.
I have searched the internet for about 5 days looking for a solution or good tutorial to visual basic and serial communication
but have been unfortunately unable to find anything good.
My visual basic code
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Arduino.IsOpen Then
Dim bob As String = Arduino.ReadLine()
If bob = 9 Then
maintankhigh.Visible = False
Else
Arduino.Open()
End If
End If
End Sub
Private Sub Feed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Feed.Click
If Arduino.IsOpen Then
Arduino.Write(7)
End If
End Sub
//other unrelated code does here
End Class
My arduino is currently setup to send a one digit number like "3" when a switch is activated aka put in the closed position
you don't have to search all the internet, just look in these forums, everything is here. i might make a youtube video tutorial on how to set it up, good idea.
here's a way to receive data, it's an event that gets called when data arrives at the com port:
Sub EventHandler(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Arduino.DataReceived
Dim datareceived As Boolean = False
Dim recStr as String = ""
While Not datareceived
Dim recVal As Integer
recVal = Arduino.ReadByte()
If recVal = 10 Or recVal = 13 Then
datareceived = True
Else
recStr = recStr + Chr(recVal)
End If
End While
End Sub
at the end, recStr contains the string sent by Serial.println(), it MUST be println() and not print(), otherwise the receiving will get caught in endlessly receiving (untill you send ascii 10 or 13 (NL and CR))
I am still trying to get this to work with not luck is there anyway someone can make a example script for the arduino with a simple switch and visual basic to know when that switch is pushed
thank you that would greatly help me and then i can just mold my project based on that
Here is a pretty simple one. When you push the button the input from the Arduino shows up in the immediate window. just make a form with a button
Public Class Form1
Public readstr As String
Function getCom() As String
readstr = ""
If SerialPort1.IsOpen = True Then
SerialPort1.Close()
SerialPort1.Open()
readstr = SerialPort1.ReadLine
Debug.Print(readstr)
getCom = readstr
End If
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SerialPort1.BaudRate = 9600
SerialPort1.Parity = 0
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.PortName = "com7"
SerialPort1.Open()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dim bob as string
bob = Me.getCom()
Debug.Print(bob)
'put your code to translate to an integer here
End Sub
End Class