看看简单的通信代码?
Option ExplicitDim ary() As String
Dim buf As Variant
Dim i As Integer
Private Sub Form_Load()
= 2
MSC.Settings = "1200,n,8,1"
MSC.InBufferCount = 0
MSC.OutBufferCount = 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
Case comEvReceive
buf = MSC.Input
Do While (Left(buf, 1) <> "=" And Len(buf) <> 9)
txtReceive.Text = ""
buf = ""
buf = MSC.Input
Loop
txtReceive.Text = buf
Case comEvSend
End Select
End Sub
仪表所发送过来的数据是以“=”开始的9位数据,比如=51.07000=51.07000=51.07000=……,在这里用的do while … loop语句是为了防止出现乱码,如果(Left(buf, 1) <> "=") And (Len(buf) <> 9)将进行循环等待,是为了保证给txtReceive.Text所赋值是以“=”开始的9位数据!
以上这两种方法都不行,不知那儿错了,是不是我哪儿有逻辑方面的错误?请您指正,如果您有更好的办法,请您提醒一下!谢谢!