主题:[原创]通信代码
Option Explicit
Dim ary() As String
Dim buf As Variant
Dim buf1 As Variant
Dim i As Integer
Private Sub Form_Load()
MSC.CommPort = 2
MSC.Settings = "1200,n,8,1"
MSC.InBufferCount = 0
MSC.InputLen = 9
MSC.RThreshold = 9
MSC.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSC.PortOpen = True Then MSC.PortOpen = False
End Sub
Private Sub MSC_OnComm()
Select Case MSC.CommEvent
Case comEvReceive
buf = MSC.Input
ReDim ary(Len(buf) - 1)
For i = 1 To Len(buf)
ary(i - 1) = Mid(buf, i, 1)
Next
[color=FF0000]Do While ary(0) <> "="
txtreceive.Text = " "
buf = MSC.Input
ReDim ary(Len(buf) - 1)
For i = 1 To Len(buf)
ary(i - 1) = Mid(buf, i, 1)
Next
Loop [/color] For i = 0 To UBound(ary)
buf1 = buf1 + ary(i)
Next
txtreceive.Text = buf1
buf1 = ""
Case comEvSend
End Select
End Sub
如果msc.input字符串中的第一个字符为“=”号,就能运行成功!如果不是,我就用do while 语句进行循环等待,我运行了多次发现,如果第一个字符不是“=”时,就运行错误,
错误提示是:实时错误“9”,下标越界。 真的不知哪儿错了?请高手帮忙看看!
Dim ary() As String
Dim buf As Variant
Dim buf1 As Variant
Dim i As Integer
Private Sub Form_Load()
MSC.CommPort = 2
MSC.Settings = "1200,n,8,1"
MSC.InBufferCount = 0
MSC.InputLen = 9
MSC.RThreshold = 9
MSC.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSC.PortOpen = True Then MSC.PortOpen = False
End Sub
Private Sub MSC_OnComm()
Select Case MSC.CommEvent
Case comEvReceive
buf = MSC.Input
ReDim ary(Len(buf) - 1)
For i = 1 To Len(buf)
ary(i - 1) = Mid(buf, i, 1)
Next
[color=FF0000]Do While ary(0) <> "="
txtreceive.Text = " "
buf = MSC.Input
ReDim ary(Len(buf) - 1)
For i = 1 To Len(buf)
ary(i - 1) = Mid(buf, i, 1)
Next
Loop [/color] For i = 0 To UBound(ary)
buf1 = buf1 + ary(i)
Next
txtreceive.Text = buf1
buf1 = ""
Case comEvSend
End Select
End Sub
如果msc.input字符串中的第一个字符为“=”号,就能运行成功!如果不是,我就用do while 语句进行循环等待,我运行了多次发现,如果第一个字符不是“=”时,就运行错误,
错误提示是:实时错误“9”,下标越界。 真的不知哪儿错了?请高手帮忙看看!