string [] STR=SerialPort. GetPortNames ();
If (STR==null)
{
MessageBox. Show (" the machine is no serial port!" , the "message",
MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}
//add a serial port project
Foreach (string s in System. IO. Ports. SerialPort. GetPortNames ())
{
//to get how many COM port
CbSerial. Items. The Add (s);
}
//a serial port to set default options
//set the default option for cbSerial
CbSerial. SelectedIndex=0;
SerialPort. BaudRate=9600;
Serial port check there's no way, no serial port when not prompted, also complains
The cbSerial. SelectedIndex=0 to 1 can run, but, it is not the default location, automatically check the COM to manually adjustable,
The main question is why it can't detect not plugged into a serial port?
CodePudding user response:
String [] STR=SerialPort. GetPortNames (); Don't return null, you should judge STR. Length<1CodePudding user response:
Before using a method, can have time to see the source code
CodePudding user response:
String [] STR=SerialPort. GetPortNames ();If (STR==null)
{
MessageBox. Show (" the machine is no serial port!" , the "message",
MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}
//add a serial port project
Foreach (string s in System. IO. Ports. SerialPort. GetPortNames ())
{
//to get how many COM port
CbSerial. Items. The Add (s);
}
If (cbSerial. Items. Count & gt; 0)
{
//a serial port to set default options
//set the default option for cbSerial
CbSerial. SelectedIndex=0;
SerialPort. BaudRate=9600;
}