Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port '使用COM1端口
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary '采用二进制传输
'MSComm1.SThreshold = 1
.RThreshold = 1 '产生MSComm事件
.InBufferCount = 0 '清空接受缓冲区
'MSComm1.OutBufferCount = 0 '清空传输缓冲区
.InBufferSize = 1024
.PortOpen = True
End With
If Err Then '错误处理
MsgBox "串口通信无效"
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer(0) As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
'告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer(0) = AscB(Buffer)
'MSComm1.InputMode = comInputModeText
'Buffer = AscB(MSComm1.Input)
Text1.Text = Text1.Text & Hex(Hexbuffer(0)) '转换成十六进制
Case Else
End Select
MSComm1.PortOpen = False '关闭串口连接
End Sub
18 个解决方案
#1
刚才忘了解释,我要读取ic卡的数据,通过串口连接,vb代码如上。问题是怎么也触发不了MSComm1_OnComm事件。请各位大侠们帮助一下,不胜感激!
#2
先用串口监视软件看看串口上是否有进来的数据。
#3
你读取ic卡的命令呢,你不读哪来的返回啊,你把读的程序发上来看看!
#4
LZ的代码可接收1字节的ASCII字符,然后就将串口关闭了,这是LZ的本意吗?
可以按2楼的意见"先用串口监视软件看看串口上是否有进来的数据。"
#5
我测试过,串口可以接受数据。现在就是不能触发comEvReceive,而且MSComm1.CommEvent的值为0,所以不能触发就收事件。
#6
MSComm1.PortOpen = False '关闭串口连接
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
'告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer() = Buffer
Dim I as Integer
For I=0 To Ubound(HexBuffer)
If hex(hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
End Select
End Sub
#7
Dim a As Variant
Dim b() As Byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
a = MSComm1.Input
b() = a
Dim I As Integer
For I = LBound(b) To UBound(b)
If Len(Hex(b(I))) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(b(I)) & " "
Else
Text1.Text = Text1.Text & Hex(b(I)) & " "
End If
Next I
End Select
Dim b() As Byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
a = MSComm1.Input
b() = a
Dim I As Integer
For I = LBound(b) To UBound(b)
If Len(Hex(b(I))) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(b(I)) & " "
Else
Text1.Text = Text1.Text & Hex(b(I)) & " "
End If
Next I
End Select
#8
ding
#9
还是没有解决啊!
#10
.InBufferCount = 0 '清空接受缓冲区
将这句话换成
Dim r() as byte
r()=.Input
将这句话换成
Dim r() as byte
r()=.Input
#11
MSComm1.PortOpen = False '关闭串口连接
注释掉试下,
怎么就接收一个 byte?就要求结束 ????
注释掉试下,
怎么就接收一个 byte?就要求结束 ????
#12
MSComm1.PortOpen = False这句已经注释掉了,现在问题是Mscomm1.comEvent的值是3,是这个comEvCTS 3 clear-to-send 线变化。
不知道这个值是什么意思?
不知道这个值是什么意思?
#13
现在代码改成这样了:
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
'MSComm1.SThreshold = 1
.RThreshold = 1
.InBufferCount = 0
.OutBufferCount = 0
.InBufferSize = 1024
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer(0) As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer(0) = AscB(Buffer)
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
Case comEvCTS
Debug.Print "123456789"
Debug.Print "Mscomm1.Event = " & MSComm1.CommEvent
End Select
End Sub
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
'MSComm1.SThreshold = 1
.RThreshold = 1
.InBufferCount = 0
.OutBufferCount = 0
.InBufferSize = 1024
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer(0) As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer(0) = AscB(Buffer)
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
Case comEvCTS
Debug.Print "123456789"
Debug.Print "Mscomm1.Event = " & MSComm1.CommEvent
End Select
End Sub
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
#14
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
这段代码是为了 执行 MSComm1_OnComm 过程??
这个 MSComm1_OnComm 过程会自动执行,
因为你前面已经设置了
.RThreshold = 1
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
这段代码是为了 执行 MSComm1_OnComm 过程??
这个 MSComm1_OnComm 过程会自动执行,
因为你前面已经设置了
.RThreshold = 1
#15
是啊,如果不用定时器,MSComm1_OnComm不会执行,现在MSComm1.CommEvent的值是3啊?不知道硬件又问题没有?
#16
将这句话去掉吧
.InBufferCount = 0 '清空接受缓冲区
还有看看下面这些设置是否问题
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
只有这样一点点试着找了
#17
上述代码有句错误: If Hex(Hexbuffer) = 1 Then,漏了Hexbuffer数组的下标
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port '使用COM1端口
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary '采用二进制传输
.RThreshold = 1 '产生MSComm事件
.InBufferCount = 0 '清空接受缓冲区
.OutBufferCount = 0 '清空传输缓冲区
.InBufferSize = 1024
.PortOpen = True
End With
If Err Then '错误处理
MsgBox "串口通信无效"
End If
Text1 = ""
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
'告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer() = Buffer
Dim I As Integer
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer(I)) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
End Select
End Sub
#18
感谢各位的帮助,在此谢谢!
#1
刚才忘了解释,我要读取ic卡的数据,通过串口连接,vb代码如上。问题是怎么也触发不了MSComm1_OnComm事件。请各位大侠们帮助一下,不胜感激!
#2
先用串口监视软件看看串口上是否有进来的数据。
#3
你读取ic卡的命令呢,你不读哪来的返回啊,你把读的程序发上来看看!
#4
LZ的代码可接收1字节的ASCII字符,然后就将串口关闭了,这是LZ的本意吗?
可以按2楼的意见"先用串口监视软件看看串口上是否有进来的数据。"
#5
我测试过,串口可以接受数据。现在就是不能触发comEvReceive,而且MSComm1.CommEvent的值为0,所以不能触发就收事件。
#6
MSComm1.PortOpen = False '关闭串口连接
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
'告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer() = Buffer
Dim I as Integer
For I=0 To Ubound(HexBuffer)
If hex(hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
End Select
End Sub
#7
Dim a As Variant
Dim b() As Byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
a = MSComm1.Input
b() = a
Dim I As Integer
For I = LBound(b) To UBound(b)
If Len(Hex(b(I))) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(b(I)) & " "
Else
Text1.Text = Text1.Text & Hex(b(I)) & " "
End If
Next I
End Select
Dim b() As Byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
a = MSComm1.Input
b() = a
Dim I As Integer
For I = LBound(b) To UBound(b)
If Len(Hex(b(I))) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(b(I)) & " "
Else
Text1.Text = Text1.Text & Hex(b(I)) & " "
End If
Next I
End Select
#8
ding
#9
还是没有解决啊!
#10
.InBufferCount = 0 '清空接受缓冲区
将这句话换成
Dim r() as byte
r()=.Input
将这句话换成
Dim r() as byte
r()=.Input
#11
MSComm1.PortOpen = False '关闭串口连接
注释掉试下,
怎么就接收一个 byte?就要求结束 ????
注释掉试下,
怎么就接收一个 byte?就要求结束 ????
#12
MSComm1.PortOpen = False这句已经注释掉了,现在问题是Mscomm1.comEvent的值是3,是这个comEvCTS 3 clear-to-send 线变化。
不知道这个值是什么意思?
不知道这个值是什么意思?
#13
现在代码改成这样了:
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
'MSComm1.SThreshold = 1
.RThreshold = 1
.InBufferCount = 0
.OutBufferCount = 0
.InBufferSize = 1024
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer(0) As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer(0) = AscB(Buffer)
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
Case comEvCTS
Debug.Print "123456789"
Debug.Print "Mscomm1.Event = " & MSComm1.CommEvent
End Select
End Sub
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
'MSComm1.SThreshold = 1
.RThreshold = 1
.InBufferCount = 0
.OutBufferCount = 0
.InBufferSize = 1024
.PortOpen = True
End With
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer(0) As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer(0) = AscB(Buffer)
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
Case comEvCTS
Debug.Print "123456789"
Debug.Print "Mscomm1.Event = " & MSComm1.CommEvent
End Select
End Sub
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
#14
Private Sub Timer1_Timer()
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
这段代码是为了 执行 MSComm1_OnComm 过程??
这个 MSComm1_OnComm 过程会自动执行,
因为你前面已经设置了
.RThreshold = 1
Me.Timer1.Enabled = True
Call MSComm1_OnComm
End Sub
这段代码是为了 执行 MSComm1_OnComm 过程??
这个 MSComm1_OnComm 过程会自动执行,
因为你前面已经设置了
.RThreshold = 1
#15
是啊,如果不用定时器,MSComm1_OnComm不会执行,现在MSComm1.CommEvent的值是3啊?不知道硬件又问题没有?
#16
将这句话去掉吧
.InBufferCount = 0 '清空接受缓冲区
还有看看下面这些设置是否问题
.CommPort = port
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary
只有这样一点点试着找了
#17
上述代码有句错误: If Hex(Hexbuffer) = 1 Then,漏了Hexbuffer数组的下标
Private Sub Form_Load()
Dim port As Integer
port = 1
With MSComm1
.CommPort = port '使用COM1端口
.Settings = "1200,O,7,1"
.InputMode = comInputModeBinary '采用二进制传输
.RThreshold = 1 '产生MSComm事件
.InBufferCount = 0 '清空接受缓冲区
.OutBufferCount = 0 '清空传输缓冲区
.InBufferSize = 1024
.PortOpen = True
End With
If Err Then '错误处理
MsgBox "串口通信无效"
End If
Text1 = ""
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim Hexbuffer() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
'告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Buffer = MSComm1.Input
Hexbuffer() = Buffer
Dim I As Integer
For I = 0 To UBound(Hexbuffer)
If Hex(Hexbuffer(I)) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Hexbuffer(I))
Else
Text1.Text = Text1.Text & Hex(Hexbuffer(I))
End If
Next I
End Select
End Sub
#18
感谢各位的帮助,在此谢谢!