Home > Net >  Vb.net a serial port communication how to realize plug usb port
Vb.net a serial port communication how to realize plug usb port

Time:09-30

How vb.net do PC, real-time update Windows serial port information, is I want to know if there is any usb port, real-time or to reconnect, as if to call Windows message function, there is no other way,

CodePudding user response:

USB plug will be a news broadcast
Can cover wndproc receives the message
This is the most simple

CodePudding user response:

Use libusb or cyusb library

CodePudding user response:

Hello, I am also do the vb PC, could you please ask you some questions, this is my qq: 1812775970, could you add as a friend, thank you

CodePudding user response:

Directly in a window to define a message filter or overloading wndproc, USB devices will be broadcast to all Windows when there is a change a message, it is ok to deal with the message, use a timer to make a sample, through the WMI device enumeration, with some characters of the device name (CH340) to distinguish, you look at:
 Dim WM_DEVICECHANGE As Integer=& amp; H219 
Dim DBT_DEVICEREMOVECOMPLETE As Integer=& amp; H8004
Dim DBT_DEVICEARRIVAL As Integer=& amp; H8000

Protected Overrides Sub WndProc (ByRef m As Message)
MyBase. WndProc (m)
If m.M sg=WM_DEVICECHANGE Then
If m. Param. ToInt32=DBT_DEVICEARRIVAL Then usb insert
'TmrAutoSetPort. Enabled=True
ElseIf m. Param. ToInt32=DBT_DEVICEREMOVECOMPLETE Then
TmrAutoSetPort. Enabled=True
End the If
End the If
Private Sub tmrAutoSetPort_Tick (sender As Object, As EventArgs e) Handles tmrAutoSetPort. Tick
Dim b=New ManagementObjectSearcher (" select * from Win32_PnPEntity "). Get 'test plug and play device
Dim LST As New List (Of String)
Try
For Each c In b
Try
Be sad etPropertyValue chtistina georgina rossetti.british poetess If (" Name "). The ToString. The Contains (" CH340 ") Then
Be sad etPropertyValue chtistina georgina rossetti.british poetess LST. Add ((" Name "))
End the If
Catch the ex As Exception
End the Try
Next
Catch the ex As Exception
End the Try
End Sub
End Sub

In the code to detect the CH340 equipment and added to a list, pull out the handle didn't write, write their own.

CodePudding user response:

Oww, stick together, the two function End Sub position is wrong, change yourself,