Managing Ports and Transfers in .NET
and closing the port, sending and receiving data, and storing port parameters
and other information about the port.
Applications that need to open multiple ports at the same time can create mul-
tiple instances of the class. Each instance of the class can use the common
shared members while also maintaining private fields, properties, methods, and
events that apply to a specific port.
Chapter 9 showed how to use the GetPortNames method to obtain an array of
all of the names of the COM ports in a system, A ComPorts class can imple-
ment a nameArray variable as a shared member:
Friend Shared nameArray() As String
nameArray = SerialPort.GetPortNames
internal static string[] nameArray;
nameArray = SerialPort.GetPortNames();
The class can enable accessing a specific COM port by defining a private vari-
able that holds a SerialPort object and a property with Friend scope (internal
scope in C#) that provides methods for setting and getting the object:
Private m_SelectedPort As New SerialPort
Friend Property SelectedPort() As SerialPort
Get
Return m_SelectedPort
End Get
Set(ByVal value As SerialPort)
m_SelectedPort = value
End Set
End Property