扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
VB可用MsComm控件读取来自USB转RS232接口的数据。或者使用Winsock 控件接收来自USB网卡的数据。
为李沧等地区用户提供了全套网页设计制作服务,及李沧网站建设行业解决方案。主营业务为网站制作、成都网站设计、李沧网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行通讯功能。
利用 WinSock 控件可以与远程计算机建立连接,并通过用户数据文报协议 (UDP)或者传输控制协议
(TCP)进行数据交换。这两种协议都可以用来创建客户与服务器应用程序。
窗体上添加两个标签:Label1和Label2
下面代码经过VB 2005测试通过。
Imports System.IO
Public Class Form1
Public Const WM_DEVICECHANGE = H219
Public Const DBT_DEVICEARRIVAL = H8000
Public Const DBT_CONFIGCHANGECANCELED = H19
Public Const DBT_CONFIGCHANGED = H18
Public Const DBT_CUSTOMEVENT = H8006
Public Const DBT_DEVICEQUERYREMOVE = H8001
Public Const DBT_DEVICEQUERYREMOVEFAILED = H8002
Public Const DBT_DEVICEREMOVECOMPLETE = H8004
Public Const DBT_DEVICEREMOVEPENDING = H8003
Public Const DBT_DEVICETYPESPECIFIC = H8005
Public Const DBT_DEVNODES_CHANGED = H7
Public Const DBT_QUERYCHANGECONFIG = H17
Public Const DBT_USERDEFINED = HFFFF
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE
Case DBT_DEVICEARRIVAL 'U盘插入
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
Label2.Text = "U盘已经插入!盘符为:" + drive.Name.ToString()
End If
Next
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盘卸载
Label2.Text = "U盘已经拔出!"
Case DBT_DEVICEREMOVEPENDING
Case DBT_DEVICETYPESPECIFIC
Case DBT_DEVNODES_CHANGED
Case DBT_QUERYCHANGECONFIG
Case DBT_USERDEFINED
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "请操作U盘!"
Label2.Text = ""
End Sub
End Class
在我的XP+VB2010环境下,测试U盘插入拔出,没有问题:
Private WM_DEVICECHANGE As Integer = H219
Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
DBT_CONFIGCHANGECANCELED = H19
DBT_CONFIGCHANGED = H18
DBT_CUSTOMEVENT = H8006
DBT_DEVICEARRIVAL = H8000
DBT_DEVICEQUERYREMOVE = H8001
DBT_DEVICEQUERYREMOVEFAILED = H8002
DBT_DEVICEREMOVECOMPLETE = H8004
DBT_DEVICEREMOVEPENDING = H8003
DBT_DEVICETYPESPECIFIC = H8005
DBT_DEVNODES_CHANGED = H7
DBT_QUERYCHANGECONFIG = H17
DBT_USERDEFINED = HFFFF
End Enum
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
MsgBox("USB device is just plugged in!")
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
MsgBox("USB device is just taken out")
End Select
End If
MyBase.WndProc(m)
End Sub
看下这个异常的类型 在捕捉异常的时候处理为关闭并重新打开端口监听(这个看你自己需求) 然后在socketException下面再catch其它exception 进行弹出错误提示之类的操作 就可以了
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流