Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


 private SerialPort m_SelectedPort = new SerialPort();


internal SerialPort SelectedPort
{
get
{
return m_SelectedPort;
}
set
{
m_SelectedPort = value;
}
}
To access a specific COM port, an application creates an instance of the Com-
Ports class:

 Friend UserPort1 As ComPorts
UserPort1 = New ComPorts


 internal ComPorts UserPort1;
UserPort1 = new ComPorts();
The application can select a port name from the array retrieved with GetPort-
Names and set SelectedPort’s PortName property to the selected name. The
application can then set port parameters and open, read, write to, and close the
port. The application can call routines in the ComPorts class to set port param-
eters and open, read, write to, and close the port as needed.


 Dim comPortIndex as Integer
Dim dataToWrite as String = "hello"
Dim newReceivedData As String


' The index of the selected COM port in the name array.

comPortIndex = 1

UserPort1.SelectedPort.PortName = nameArray(comPortIndex)

UserPort1.SelectedPort.BaudRate = 115200
UserPort1.SelectedPort.Open()
newReceivedData = UserPort1.SelectedPort.ReadExisting
UserPort1.SelectedPort.Write(dataToWrite)
UserPort1.SelectedPort.Close()
Free download pdf