一个简单接收的程序
Dim inputstr As String
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
End Sub
Private Sub Command2_Click()
List1.Clear
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
With MSComm1
.CommPort = 1 ’使用COM2
.Settings = "9600,N,8,1" ’设置通信口参数
.InBufferSize = 40
’设置MSComm1接收缓冲区为40字节
.OutBufferSize = 2
’设置MSComm1发送缓冲区为2字节
’.InputMode = comInputModeBinary
’设置接收数据模式为二进制形式
.InputLen = 1
’设置Input 一次从接收缓冲读取字节数为1
.SThreshold = 1
’设置Output 一次从发送缓冲读取字节数为1
InBufferCount = 0 ’清除接收缓冲区
.OutBufferCount = 0 ’清除发送缓冲区
.RThreshold = 1
’设置接收一个字节产生OnComm事件
If .PortOpen = False Then
’判断通信口是否打开
.PortOpen = True ’打开通信口
If Err Then ’错误处理
MsgBox "串口通信无效"
Exit Sub
End If
End If
End With
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
av = MSComm1.Input
Text2.Text = Text2.Text & " " & Asc(av)
’Timer1.Enabled = True
If av >= 0 And av <= 9 Then
inputstr = inputstr & av
Else
Text3.Text = Text3.Text & inputstr & "*"
Text1.Text = Asc(av)
If Len(inputstr) <> 14 Then
response = MsgBox("条码格式不正确,取消格式限制请按“是”,否则按“否”,退出程序按“取消”数据长度=" & Len(inputstr) & " " & inputstr, vbYesNoCancel, "条码格式")
List1.AddItem (inputstr)
inputstr = ""
MSComm1.InBufferCount = 0
Else
List1.AddItem (inputstr)
inputstr = ""
MSComm1.InBufferCount = 0
End If
End If
End select
End Sub
Private Sub Timer1_Timer()
Dim inputstr
Timer1.Enabled = False
inputstr = MSComm1.Input
List1.AddItem (inputstr)
End Sub
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/