FXPLC与PC的通信问题
采用oncomm事件时,为何不能接收数据啊?
Function Comm_Read(rd As String) As Boolean
Dim out As String, data As String, l As Integer
out = Chr(&H5) + rd + GetSumChk(rd)
frm1.MSComm1.InputLen = 0
frm1.MSComm1.InBufferCount = 0
frm1.MSComm1.Output = out
Debug.Print out
l = Len(out)
frm1.MSComm1.RThreshold = l
End function
Private Sub MSComm1_OnComm()
Dim data As String, rea As String, a As Integer, b As String
Debug.Print "1: " & data
data = frm1.MSComm1.Input
Debug.Print "2: " & data
a = Val(Asc(Left(data, 1)))
b = GetSumChk(Mid(data, 2, Len(data) - 3))
If a = 2 And b = Mid(data, Len(data) - 1, 2) Then
rea = Mid(data, 6, Len(data) - 8)
frm1.MSComm1.Output = Chr(&H6) + "00FF "
Else
rea = "error "
frm1.MSComm1.Output = Chr(&H15) + "00FF "
End If
Text4.Text = rea
End Sub
程序运行运行成功一次,但是不会退出还在循环,并且第二次到 a = Val(Asc(Left(data, 1)))时,提示错误,“无效的过程调用或参数”;而且以后再试均不成功,错误同上,查看 frm1.MSComm1.Input 里面有数据而,data里面却没有,导致出错 !
请问,这是哪里出问题了啊?请教各位高手帮帮忙啊!谢谢了
[解决办法]
接收的数据长度并不是固定的,那就在FORM——LOAD事件中设置RThreshold = 1
然后在接收代码中设法处理.但需有确定的数据通信协议.同时建议FXPLC的返回数据字节长度最好是一致的,这可避免接收错误.