-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoManager.vb
More file actions
91 lines (77 loc) · 3.15 KB
/
ArduinoManager.vb
File metadata and controls
91 lines (77 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Imports System.IO.Ports
Imports System.Threading
Public Class ArduinoManager
Private Shared WithEvents arduinoPort As New SerialPort()
Private Shared cancellationTokenSource As CancellationTokenSource
Public Shared Event ConnectionStatusChanged(sender As Object, status As String)
' Connect to Arduino
Public Shared Sub Connect()
Try
cancellationTokenSource = New CancellationTokenSource()
Dim availablePorts As String() = SerialPort.GetPortNames()
If availablePorts.Length > 0 Then
For Each port As String In availablePorts
If cancellationTokenSource.IsCancellationRequested Then Exit For
arduinoPort.PortName = port
arduinoPort.BaudRate = 9600
arduinoPort.ReadTimeout = 1000
arduinoPort.WriteTimeout = 1000
Try
arduinoPort.Open()
Thread.Sleep(1000)
arduinoPort.WriteLine("Hello")
Dim response As String = arduinoPort.ReadLine()
If response.Contains("Hello") Then
RaiseEvent ConnectionStatusChanged(Nothing, "Connected to " & arduinoPort.PortName)
Exit For
Else
arduinoPort.Close()
End If
Catch ex As Exception
arduinoPort.Close()
End Try
Next
If Not arduinoPort.IsOpen Then
RaiseEvent ConnectionStatusChanged(Nothing, "Unable to connect to any COM ports.")
End If
Else
RaiseEvent ConnectionStatusChanged(Nothing, "No COM ports available!")
End If
Catch ex As Exception
RaiseEvent ConnectionStatusChanged(Nothing, "Error: " & ex.Message)
Finally
cancellationTokenSource.Dispose()
End Try
End Sub
' Disconnect
Public Shared Sub Disconnect()
If arduinoPort.IsOpen Then
arduinoPort.Close()
RaiseEvent ConnectionStatusChanged(Nothing, "Disconnected.")
End If
End Sub
' Send string to Arduino
Public Shared Sub SendToArduino(message As String)
Try
If arduinoPort.IsOpen Then
arduinoPort.WriteLine(message)
Else
RaiseEvent ConnectionStatusChanged(Nothing, "Port is not open.")
End If
Catch ex As Exception
RaiseEvent ConnectionStatusChanged(Nothing, "Send error: " & ex.Message)
End Try
End Sub
' Read string from Arduino
Public Shared Function ReadFromArduino() As String
Try
If arduinoPort.IsOpen Then
Return arduinoPort.ReadLine()
Else
Return "Port is not open."
End If
Catch ex As Exception
Return "Read error: " & ex.Message
End Try
End Function
End Class