Such as I now have a serial port for COM1
Now added two serial interface COM2, COM3, how do I know that increased these two serial port?
If has access to a serial port, that's too much trouble,
Are there any events?
CodePudding user response:
My mind only in threadCodePudding user response:
String [] ports=SerialPort. GetPortNames ();Add the timer
CodePudding user response:
Can listen WMI events is similar to the second pointBut inside Win32_LogicalDisk into Win32_SerialPort or Win32_USBHub, this depends on how you a serial port connection, have any USB transfer, so the inside of the ManagementBaseObject object values may have a look here
CodePudding user response:
The timer + GetPortNames no good solution.CodePudding user response:
I am clicking a drop-down box, first remove all the options, and then use serial testing, the test results to the drop-down boxCodePudding user response:
Ha ha, recently this fun, don't understand, but I do it://system messages constants
Public const int WM_DEVICE_CHANGE=0 x219;//device change
Public const int DBT_DEVICEARRIVAL=0 x8000;//device into
Public const int DBT_DEVICE_REMOVE_COMPLETE=0 x8004;//device to remove
//============================================================================
//message processing (sensing device, automatic refresh serial list)
//============================================================================
Protected override void WndProc (ref Message m)
{
Switch (m.M sg)//judgment message type
{
Case WM_DEVICE_CHANGE://equipment change message
{
IniPort (cmbCOM);//the enumeration serial
}
break;
}
Base. WndProc (ref m);
}
CodePudding user response:
Void IniPort ComboBox (cb){
String [] com=SerialPort. GetPortNames ();
If (com) Length & gt; 0)
{
Byte c=0, ci=0;
Cb. The Items. The Clear ();
Foreach (string cn in com)
{
Cb. The Items. The Add (cn);
If (cn=="COM3"), ci=c;//when debugging: choosing COM3
C + +;
}
Cb. SelectedIndex=ci;
}
The else
MessageBox. Show (" failed to find any serial ports!" , "serial port initialization tip:");
}
CodePudding user response:
The