Home > Net >  C # serial communication how to monitor a serial port status?
C # serial communication how to monitor a serial port status?

Time:09-16

What do you want to be a c # serial communication program, but don't know how to monitor the state of the serial port,
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 thread

CodePudding user response:

String [] ports=SerialPort. GetPortNames ();

Add the timer

CodePudding user response:

Can listen WMI events is similar to the second point
But 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 box

CodePudding 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
reference 7 floor Gujianda 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:");
}


Insert a new serial port, this message function in many times to know what the reason?
But every time I go in is clear about the combobox effect or a pile of repeated serial

CodePudding user response:

Positive solution to the second floor, run to add new com port, had better open a thread, the timer is the main program running, don't forget to add the lock
  •  Tags:  
  • C#
  • Related