VB的问题我刚学VB,用MSComm控
InputMode设为ComInputBinary表示以二进制接收,数据类型为BYTE。发送用&H55表示16进制55:
Private Sub Command1_Click()
Dim bytData(1) As Byte
bytData(0) = &H55
bytData(1) = &H55
Call SendData(bytData) '发送
End Sub
Private Sub Form_Load()
MSComm1。 CommPort = 1 'COM端口号
MSComm1。Settings = "9600,n,8,1"
MSComm1。InputMode = comInpu...全部
InputMode设为ComInputBinary表示以二进制接收,数据类型为BYTE。发送用&H55表示16进制55:
Private Sub Command1_Click()
Dim bytData(1) As Byte
bytData(0) = &H55
bytData(1) = &H55
Call SendData(bytData) '发送
End Sub
Private Sub Form_Load()
MSComm1。
CommPort = 1 'COM端口号
MSComm1。Settings = "9600,n,8,1"
MSComm1。InputMode = comInputModeBinary '采用二进制接收
MSComm1。
InBufferCount = 0 '清空接受缓冲区
MSComm1。OutBufferCount = 0 '清空传输缓冲区
MSComm1。RThreshold = 1 '产生MSComm事件
MSComm1。
PortOpen = True '打开端口
Text1 = ""
Text3 = ""
End Sub
'发送区
Public Function SendData(ByRef bytData() As Byte) As Long
On Error Resume Next
MSComm1。
InBufferCount = 0 '清空接收缓冲区
MSComm1。Output = bytData '发送数据
Do
DoEvents
Loop Until MSComm1。OutBufferCount = 0 '等待,直到数据发送完毕
MSComm1。
OutBufferCount = 0
End Function
Private Sub MSComm1_OnComm()
Dim i As Integer
Dim a As Integer
Dim av() As Byte
Dim length As Integer
Dim inbuf() As Byte
Dim Hexinbuf(21) As String
Dim strData As String
Select Case MSComm1。
CommEvent
Case comEvReceive
'接收事件发生
MSComm1。InputLen = 0
av = MSComm1。
Input
inbuf() = av
For i = 0 To UBound(inbuf)
If Len(Hex(inbuf(i))) = 1 Then
strData = strData & "0" & Hex(inbuf(i))
Else
strData = strData & Hex(inbuf(i))
End If
Next
Text1 = strData '显示接收数据的16进制形式
End Select
End Sub
另请参考下列网址:
VB调试精灵的简化源代码。
收起