补充一点,以上程序为EVB程序,和VB程序略有不同。
仔细看看问题其实在这段代码中!
If (devicenum = 1) Then
SendData1
Delay (0.015) '延时10ms后收取下位机的数据
ReceiveData1 '将下位机发送来的数据收到Rbyte中
End If
If (Rbyte(1) = &H11) Then '说明数据正确!!!!
这个是ReceiveData1的代码片断SendData1
Delay (0.015) '延时10ms后收取下位机的数据
ReceiveData1 '将下位机发送来的数据收到Rbyte中
End If
If (Rbyte(1) = &H11) Then '说明数据正确!!!!
Private Sub ReceiveData1()
Rlength = Comm1.InBufferCount 'Rlength 是所有字符的数目,包括空格,与RMode无关
'写在这里的话,如果因为传输出错而进入下面的else语句,则rbyte(25)中数据全为0,从而产生“输入信号不正确”
'所以设定lostnum变量,连续10次传输出错才认为"输入信号不正确"
ReDim Rbyte(25) '考虑定义成定长数组!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If Rlength = 26 Then
Comm1.InputMode = comInputModeBinary '二进制接收,空格被当作数据发送,任一个字符都被发送
Rbyte = Comm1.Input '全部接收到Rbyte数组中
Else
'如果这里不ReDim Rbyte(25),第一次通信的时候收不到,也能够检测Rbyte(0)/(1),否则Rbyte是Variant,不是数组,程序无法运行!!!
FirstReceive = True '发送指令之前都会清空接收缓冲区,所以这里不用清空
End If
If (testArray <> Rbyte(2)) Then 'rbyte(2)=中性点幅值低位
testArray = Rbyte(2)
LostSignal = False
SameNum = 0
Else
SameNum = SameNum + 1
If (SameNum = 10) Then
SameNum = 0
LostSignal = True
End If
End If
End Sub
Rlength = Comm1.InBufferCount 'Rlength 是所有字符的数目,包括空格,与RMode无关
'写在这里的话,如果因为传输出错而进入下面的else语句,则rbyte(25)中数据全为0,从而产生“输入信号不正确”
'所以设定lostnum变量,连续10次传输出错才认为"输入信号不正确"
ReDim Rbyte(25) '考虑定义成定长数组!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If Rlength = 26 Then
Comm1.InputMode = comInputModeBinary '二进制接收,空格被当作数据发送,任一个字符都被发送
Rbyte = Comm1.Input '全部接收到Rbyte数组中
Else
'如果这里不ReDim Rbyte(25),第一次通信的时候收不到,也能够检测Rbyte(0)/(1),否则Rbyte是Variant,不是数组,程序无法运行!!!
FirstReceive = True '发送指令之前都会清空接收缓冲区,所以这里不用清空
End If
If (testArray <> Rbyte(2)) Then 'rbyte(2)=中性点幅值低位
testArray = Rbyte(2)
LostSignal = False
SameNum = 0
Else
SameNum = SameNum + 1
If (SameNum = 10) Then
SameNum = 0
LostSignal = True
End If
End If
End Sub
ReceiveData1 把数据发送到了Rbyte中,Rbyte是什么?
在看看它的定义:
Public Rbyte As Variant, Rlength As Integer
好家伙!Rbyte被定义成了Variant型,当然在一般情况下系统会自动识别为byte类型。
但假如从下位机收到的是错误类型或下位机根本没有发送数据程序就会导致系统因解析类型错误或长时间等待而崩溃!
所以需要将其重新定义为
Public RbyteL(25) As Byte
并且要对ReceiveData1和ComServer两个函数作一些必要的修改Function ComServer(ByVal devicenum As Integer)
On Error Resume Next
Dim MidV_Level As Integer
If (devicenum = 1) Then
SendData1
Delay (0.1) '延时10ms后收取下位机的数据
ReceiveData1 '将下位机发送来的数据收到Rbyte中
End If
If (RbyteL(1) = &H11) Then '说明数据正确!!!!
lostnum = 0
MidV_Level = RbyteL(4) '不同档位
If (MidV_Level = 0) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.028686 / 381.32044
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.36053
ElseIf (MidV_Level = 1) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.093878 / 1124.895397
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.11493
ElseIf (MidV_Level = 2) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.12 / 4837.049776 '1.168951
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.0261045
End If
'中性点电压频率
If (RbyteL(6) = 0 And RbyteL(7) = 0) Then
RbyteL(7) = &H12
RbyteL(6) = &H4F
End If
MidV_Freq1(0) = Round(234375# / CDbl(RbyteL(6) + RbyteL(7) * 256), 2)
'Vab_Amplitude1(0) = CDbl(Rbyte(12) + Rbyte(13) * 256) * 1.41421 * 100 / 223.872
Vab_Amplitude1(0) = CDbl(RbyteL(12) + RbyteL(13) * 256) * 0.6317
Vab_Phase1(0) = CDbl(RbyteL(14) + RbyteL(15) * 256) / 100#
'Vbc_Amplitude1(0) = CDbl(Rbyte(16) + Rbyte(17) * 256) * 1.41421 * 100 / 223.872
Vbc_Amplitude1(0) = CDbl(RbyteL(16) + RbyteL(17) * 256) * 0.6317
Vbc_Phase1(0) = CDbl(RbyteL(18) + RbyteL(19) * 256) / 100#
'Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 1.41421 * 100 / 223.872
Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 0.6317
Vca_Phase1(0) = CDbl(RbyteL(22) + RbyteL(23) * 256) / 100#
'中性点电压相位
MidV_Phase1(0) = CDbl(RbyteL(24) + RbyteL(25) * 256) / 100#
ElseIf (RbyteL(1) = 0) Then '说明收到的数据都是零!!
lostnum = lostnum + 1
If (lostnum = 10) Then
lostnum = 0
Vab_Amplitude1(0) = CDbl(RbyteL(12) + RbyteL(13) * 256) * 0.6317
Vab_Phase1(0) = CDbl(RbyteL(14) + RbyteL(15) * 256) / 100#
Vbc_Amplitude1(0) = CDbl(RbyteL(16) + RbyteL(17) * 256) * 0.6317
Vbc_Phase1(0) = CDbl(RbyteL(18) + RbyteL(19) * 256) / 100#
Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 0.6317
Vca_Phase1(0) = CDbl(RbyteL(22) + RbyteL(23) * 256) / 100#
If ((Vab_Amplitude1(0) < 4000) Or (Vbc_Amplitude1(0) < 4000) Or (Vca_Amplitude1(0) < 4000)) Then
LostSignal = True
End If
End If
End If
If Err.Number <> 0 Then
working.Visible = True
working.Show
Timer1.Enabled = False
MsgBox "comserver" & CStr(Err.Number) & Err.Description
App.End
MyLCreateProcess "\windows\pvbload.exe", "\diskc\xiaohuxianquan\XiaoHuXianQuan20051116.vb", 0, 0, 0, 0, 0, 0, 0, 0
End If
End Function
On Error Resume Next
Dim MidV_Level As Integer
If (devicenum = 1) Then
SendData1
Delay (0.1) '延时10ms后收取下位机的数据
ReceiveData1 '将下位机发送来的数据收到Rbyte中
End If
If (RbyteL(1) = &H11) Then '说明数据正确!!!!
lostnum = 0
MidV_Level = RbyteL(4) '不同档位
If (MidV_Level = 0) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.028686 / 381.32044
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.36053
ElseIf (MidV_Level = 1) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.093878 / 1124.895397
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.11493
ElseIf (MidV_Level = 2) Then
'MidV_Amplitude1(0) = CDbl(Rbyte(2) + Rbyte(3) * 256) * 1.41421 * 100 / 1.12 / 4837.049776 '1.168951
MidV_Amplitude1(0) = CDbl(RbyteL(2) + RbyteL(3) * 256) * 0.0261045
End If
'中性点电压频率
If (RbyteL(6) = 0 And RbyteL(7) = 0) Then
RbyteL(7) = &H12
RbyteL(6) = &H4F
End If
MidV_Freq1(0) = Round(234375# / CDbl(RbyteL(6) + RbyteL(7) * 256), 2)
'Vab_Amplitude1(0) = CDbl(Rbyte(12) + Rbyte(13) * 256) * 1.41421 * 100 / 223.872
Vab_Amplitude1(0) = CDbl(RbyteL(12) + RbyteL(13) * 256) * 0.6317
Vab_Phase1(0) = CDbl(RbyteL(14) + RbyteL(15) * 256) / 100#
'Vbc_Amplitude1(0) = CDbl(Rbyte(16) + Rbyte(17) * 256) * 1.41421 * 100 / 223.872
Vbc_Amplitude1(0) = CDbl(RbyteL(16) + RbyteL(17) * 256) * 0.6317
Vbc_Phase1(0) = CDbl(RbyteL(18) + RbyteL(19) * 256) / 100#
'Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 1.41421 * 100 / 223.872
Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 0.6317
Vca_Phase1(0) = CDbl(RbyteL(22) + RbyteL(23) * 256) / 100#
'中性点电压相位
MidV_Phase1(0) = CDbl(RbyteL(24) + RbyteL(25) * 256) / 100#
ElseIf (RbyteL(1) = 0) Then '说明收到的数据都是零!!
lostnum = lostnum + 1
If (lostnum = 10) Then
lostnum = 0
Vab_Amplitude1(0) = CDbl(RbyteL(12) + RbyteL(13) * 256) * 0.6317
Vab_Phase1(0) = CDbl(RbyteL(14) + RbyteL(15) * 256) / 100#
Vbc_Amplitude1(0) = CDbl(RbyteL(16) + RbyteL(17) * 256) * 0.6317
Vbc_Phase1(0) = CDbl(RbyteL(18) + RbyteL(19) * 256) / 100#
Vca_Amplitude1(0) = CDbl(RbyteL(20) + RbyteL(21) * 256) * 0.6317
Vca_Phase1(0) = CDbl(RbyteL(22) + RbyteL(23) * 256) / 100#
If ((Vab_Amplitude1(0) < 4000) Or (Vbc_Amplitude1(0) < 4000) Or (Vca_Amplitude1(0) < 4000)) Then
LostSignal = True
End If
End If
End If
If Err.Number <> 0 Then
working.Visible = True
working.Show
Timer1.Enabled = False
MsgBox "comserver" & CStr(Err.Number) & Err.Description
App.End
MyLCreateProcess "\windows\pvbload.exe", "\diskc\xiaohuxianquan\XiaoHuXianQuan20051116.vb", 0, 0, 0, 0, 0, 0, 0, 0
End If
End Function
Private Sub ReceiveData1()
Dim i As Integer
On Error Resume Next
Rlength = Comm1.InBufferCount 'Rlength 是所有字符的数目,包括空格,与RMode无关
'写在这里的话,如果因为传输出错而进入下面的else语句,则rbyte(25)中数据全为0,从而产生“输入信号不正确”
'所以设定lostnum变量,连续10次传输出错才认为"输入信号不正确"
ReDim Rbyte(25) '考虑定义成定长数组!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
For i = 0 To 25
RbyteL(i) = 0
Next
If Rlength = 26 Then
Comm1.InputMode = comInputModeBinary '二进制接收,空格被当作数据发送,任一个字符都被发送
Rbyte = Comm1.Input '全部接收到Rbyte数组中
For i = 0 To 25
RbyteL(i) = Rbyte(i)
Next
Else
'如果这里不ReDim Rbyte(25),第一次通信的时候收不到,也能够检测Rbyte(0)/(1),否则Rbyte是Variant,不是数组,程序无法运行!!!
FirstReceive = True '发送指令之前都会清空接收缓冲区,所以这里不用清空
End If
If (testArray <> RbyteL(2)) Then 'rbyte(2)=中性点幅值低位
testArray = RbyteL(2)
LostSignal = False
SameNum = 0
Else
SameNum = SameNum + 1
If (SameNum = 10) Then
SameNum = 0
LostSignal = True
End If
End If
If Err.Number <> 0 Then
Err.Clear
For i = 0 To 25
RbyteL(i) = 0
Next
End If
End Sub
Dim i As Integer
On Error Resume Next
Rlength = Comm1.InBufferCount 'Rlength 是所有字符的数目,包括空格,与RMode无关
'写在这里的话,如果因为传输出错而进入下面的else语句,则rbyte(25)中数据全为0,从而产生“输入信号不正确”
'所以设定lostnum变量,连续10次传输出错才认为"输入信号不正确"
ReDim Rbyte(25) '考虑定义成定长数组!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
For i = 0 To 25
RbyteL(i) = 0
Next
If Rlength = 26 Then
Comm1.InputMode = comInputModeBinary '二进制接收,空格被当作数据发送,任一个字符都被发送
Rbyte = Comm1.Input '全部接收到Rbyte数组中
For i = 0 To 25
RbyteL(i) = Rbyte(i)
Next
Else
'如果这里不ReDim Rbyte(25),第一次通信的时候收不到,也能够检测Rbyte(0)/(1),否则Rbyte是Variant,不是数组,程序无法运行!!!
FirstReceive = True '发送指令之前都会清空接收缓冲区,所以这里不用清空
End If
If (testArray <> RbyteL(2)) Then 'rbyte(2)=中性点幅值低位
testArray = RbyteL(2)
LostSignal = False
SameNum = 0
Else
SameNum = SameNum + 1
If (SameNum = 10) Then
SameNum = 0
LostSignal = True
End If
End If
If Err.Number <> 0 Then
Err.Clear
For i = 0 To 25
RbyteL(i) = 0
Next
End If
End Sub
这里增加了自动重启功能和小范围容错功能。
这下问题终于得到了解决!
程序已经在现场跑了3个多月了:)
但是要是再出错可能我还要继续on error resume next...........