welcome to erwinproject, this time the admin will discuss the Tutorial for Making VBNET Arduino Part 2 Software. Here I will explain starting from compiling forms in visual basic to coding to connecting to Arduino using serial. let's look at the tutorial below.
- The first step is to build a GUI (graphical user interface) or arrange the components needed.
- make sure to know the address (name) of the component.
- Add Component (drag and drop) "SerialPort" in toolbox . Now press F7 to write program or code.
' CODE BY ERWINPROJECT
' thanks theme :
' - Valley Theme by Earn
' - Mono Theme by Vengfull
' - Aether Theme made by AeroRev9
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.Diagnostics
Public Class Form1
Dim receivedData As String = ""
Function ReceiveSerialData() As String
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then
Return "nothing" & vbCrLf
Else
Return Incoming
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To _
My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add( _
My.Computer.Ports.SerialPortNames(i))
Next
End Sub
Private Sub AetherButton1_Click(sender As Object, e As EventArgs) Handles AetherButton1.Click
If ComboBox1.Text = "" Then
MsgBox("SELECT PORT !")
ElseIf AetherTextbox1.Text = "" Then
AetherTextbox1.Text = 9600
Else
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = AetherButton1.Text
.ReadBufferSize = 500
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
SerialPort1.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub AetherButton2_Click(sender As Object, e As EventArgs) Handles AetherButton2.Click
Try
SerialPort1.Close()
MsgBox("Disconnect from COM")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub MonoFlat_Toggle1_ToggledChanged() Handles MonoFlat_Toggle1.ToggledChanged
If MonoFlat_Toggle1.Toggled = False And SerialPort1.IsOpen Then
SerialPort1.Write("LED 1 OFF")
ElseIf MonoFlat_Toggle1.Toggled = True And SerialPort1.IsOpen Then
SerialPort1.Write("LED 1 ON")
End If
End Sub
Private Sub MonoFlat_Toggle2_ToggledChanged() Handles MonoFlat_Toggle2.ToggledChanged
If MonoFlat_Toggle2.Toggled = False And SerialPort1.IsOpen Then
SerialPort1.Write("LED 2 OFF")
ElseIf MonoFlat_Toggle2.Toggled = True And SerialPort1.IsOpen Then
SerialPort1.Write("LED 2 ON")
End If
End Sub
Private Sub MonoFlat_Toggle3_ToggledChanged() Handles MonoFlat_Toggle3.ToggledChanged
If MonoFlat_Toggle3.Toggled = False And SerialPort1.IsOpen Then
SerialPort1.Write("LED 3 OFF")
ElseIf MonoFlat_Toggle3.Toggled = True And SerialPort1.IsOpen Then
SerialPort1.Write("LED 3 ON")
End If
End Sub
Private Sub MonoFlat_Toggle4_ToggledChanged() Handles MonoFlat_Toggle4.ToggledChanged
If MonoFlat_Toggle4.Toggled = False And SerialPort1.IsOpen Then
SerialPort1.Write("LED 4 OFF")
ElseIf MonoFlat_Toggle4.Toggled = True And SerialPort1.IsOpen Then
SerialPort1.Write("LED 4 ON")
End If
End Sub
End Class - Press Start to run your aplication .
0 Komentar