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
|
Imports System.IO.Ports
Public Class Form1
Dim ArduinoConnected As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TimerConnect.Enabled = False
ArduinoConnected = False
Autoconnect()
End Sub
Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If ArduinoConnected Then
SerialPort1.Close()
btnConnect.Text = "Connect"
Else
Autoconnect()
End If
End Sub
Private Sub Autoconnect()
For Each sp As String In My.Computer.Ports.SerialPortNames
Try
SerialPort1.PortName = sp
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.Open()
SerialPort1.Write("3/")
TimerConnect.Interval = 500
TimerConnect.Start()
While TimerConnect.Enabled And ArduinoConnected = False
Application.DoEvents()
End While
If ArduinoConnected Then
btnConnect.Text = "Disconnect"
Exit For
End If
SerialPort1.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
If ArduinoConnected = False Then
MsgBox("Arduino failed to connect. Please check that it is plugged in.")
End If
End Sub
Public Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Dim str As String = SerialPort1.ReadExisting()
If str.Contains("Arduino") Then
ArduinoConnected = True
lstConsole.Items.Add("Arduino Connected")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Shell("shutdown -s -t 100")
End Sub
End Class |
Partager