VB60可使用MsComm控件实现串口通信。按通信协议和数据帧规约接收和处理数据。
MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行通讯功能。
MSComm 控件提供下列两种处理通讯的方式:
事件驱动通讯是处理串行端口交互作用的一种非常有效的方法。在许多情况下,在事件发生时需要得到通知,例如,在 Carrier Detect (CD) 或
Request To Send (RTS) 线上一个字符到达或一个变化发生时。在这些情况下,可以利用 MSComm 控件的 OnComm
事件捕获并处理这些通讯事件。OnComm 事件还可以检查和处理通讯错误。所有通讯事件和通讯错误的列表,参阅 CommEvent
属性。
在程序的每个关键功能之后,可以通过检查 CommEvent 属性的值来查询事件和错误。如果应用程序较小,并且是自保持的,这种方法可能是更可取的。例如,如果写一个简单的电话拨号程序,则没有必要对每接收一个字符都产生事件,因为唯一等待接收的字符是调制解调器的“确定”响应。
尽管 MSComm 控件有很多重要的属性,但首先必须熟悉几个属性。
以下代码可以实现按下CTRL+M,运行记事本,稍作修改就能实现你的要求
模块部分:
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hword As Integer
End Type
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lpll = lParam
LSet i2 = lp
If (i2lWord = Modifiers) And i2hword = uVirtKey Then
Shell "notepad", vbNormalFocus '这里就是执行记事本的代码,你可以改成执行你的B软件
End If
End If
End If
'如果不是热键信息则调用原来的程序
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
窗体部分:
Sub Form_Load()
Dim ret As Long
'记录原来的window程序地址
preWinProc = GetWindowLong(Mehwnd, GWL_WNDPROC)
'用自定义程序代替原来的window程序
ret = SetWindowLong(Mehwnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_CONTROL
uVirtKey = vbKeyM
'注册热键
ret = RegisterHotKey(Mehwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,使之送往原来的window程序
ret = SetWindowLong(Mehwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Mehwnd, uVirtKey)
End Sub
以上就是关于vb怎样读取串口数据并进行处理全部的内容,包括:vb怎样读取串口数据并进行处理、vb 两个程序如何调用快捷键、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)