Serial Port Complete - Latest Microcontroller projects

(lily) #1
Using .NET’s SerialPort Class

The object’s PortName property should match a name in the array returned by
the GetPortNames method described above. An application that wants to use a
specific port can search for a match in the array:

 Dim index As Integer = -1
Dim nameArray() As String
Dim myComPortName As String


' Specify the port to look for.

myComPortName = "COM5"

' Get an array of names of installed ports.

nameArray = SerialPort.GetPortNames

Do
' Look in the array for the desired port name.

index += 1

Loop Until ((nameArray(index) = myComPortName) Or _
(index = nameArray.GetUpperBound(0)))

' If the desired port isn't found, select the first port in the array.

If (index = nameArray.GetUpperBound(0)) Then
myComPortName = nameArray(0)
End If
Free download pdf