Before accessing a port or setting port parameters, the application must create a
SerialPort object:
Friend myComPort as New SerialPort
internal SerialPort myComPort = new SerialPort();
The SerialPort object has properties for setting port parameters. The default
parameters are 9600 bps, no parity, one Stop bit, and no flow control.
Using .NET??™s SerialPort Class
157
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
Chapter 9
158
int index = -1;
string[] nameArray = null;
string myComPortName = null;
// 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.
Pages:
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195