Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


 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.

index += 1;
}
while ( !((nameArray[index] == myComPortName) |
(index == nameArray.GetUpperBound(0))));

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

if ( index == nameArray.GetUpperBound( 0 ) )
{
myComPort Name= nameArray[ 0 ];
}
To change a parameter, set the property’s value:

 myComPort.PortName = "COM6"
myComPort.BaudRate = 115200
myComPort.Parity = Parity.Even
myComPort.DataBits = 7
myComPort.StopBits = StopBits.Two
myComPort.Handshake = RequestToSend


 myComPort.PortName = "COM6";
myComPort.BaudRate = 115200;
myComPort.Parity = Parity.Even;
myComPort.DataBits = 7;
myComPort.StopBits = StopBits.Two;
myComPort.Handshake = RequestToSend;

Free download pdf