Guest User

Matthew Savage

a guest
Apr 6th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.27 KB | None | 0 0
  1. This code was made to discover a serialport device which responds to (Data-Terminal-Ready)
  2. It will get all the serialports available and list the device which responds with "ARINIT"
  3.  
  4. This will basically allow you to quickly find the port your arduino is on and close the port
  5. ready for when you want to connect to it again by using the listed item.
  6.  
  7. Imports System.IO.Ports
  8.  
  9. Public Sub scan4arduino()
  10.         Button1.Enabled = False
  11.         ListBox1.Items.Clear()
  12.         Dim ports As String() = SerialPort.GetPortNames()
  13.         Dim response As String = ""
  14.         For i = 0 To ports.Count - 1
  15.             SerialPort1.PortName = ports(i)
  16.             SerialPort1.Open()
  17.             System.Threading.Thread.Sleep(2000) 'Give the serial port chance to respond to DTR
  18.             response = SerialPort1.ReadExisting
  19.             If response.Contains("ARINIT") Then 'Return devices that respond with certain phrase (<> "" for any device)
  20.                 ListBox1.Items.Add(ports(i) + " DTR Response: " + response)
  21.                 SerialPort1.Close()
  22.             End If
  23.             SerialPort1.Close()
  24.         Next
  25.         If ListBox1.Items.Count = 0 Then
  26.             ListBox1.Items.Add("No serial ports devices found")
  27.         End If
  28.         Button1.Enabled = True
  29.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment