VB接收自由口通讯数据处理

[复制链接]
查看2811 | 回复4 | 2007-11-16 08:55:00 | 显示全部楼层 |阅读模式

Public Sub Rece_Treat(ByVal Indata As Integer, comm_port As Integer) ''''kwj gprs needOn Error GoTo CloseReceIf comm_port = 0 Then 'If comm_port = 0 Then '网络通信接收处理程序 If nSwitch0 = 0 Then '''''''kwj nSwitch0 = Net_StartByte1 End If Select Case nSwitch0 Case Net_StartByte1 RxFmHead0 = False '接收帧头失败 If Indata = &H7E Then nSwitch0 = Net_STARTBYTE2 End If Case Net_STARTBYTE2 If (Indata = &H7E) Then RxDataLen0 = 0 RxCheckSum0 = 0 nSwitch0 = Net_RxFrameLen1 Else nSwitch0 = Net_StartByte1 End If Case Net_RxFrameLen1 RxFrameLen0 = Indata If RxFrameLen0 > 2 Then RxFmHead0 = False '接收帧头失败 ReceFHed0 = False '接收帧失败 nSwitch0 = Net_StartByte1 Else nSwitch0 = Net_RxFrameLen2 End If Case Net_RxFrameLen2 RxFrameLen0 = RxFrameLen0 * 256# + Indata If RxFrameLen0 < 4 Or RxFrameLen0 > 512 Then RxFmHead0 = False '接收帧头失败 ReceFHed0 = False '接收帧失败 nSwitch0 = Net_StartByte1 Else nSwitch0 = Net_RTUADD1 End If RxFmHead0 = True '接收帧头成功 Case Net_RTUADD1 RxAddress0 = Indata RxDataLen0 = RxDataLen0 + 1 RxCheckSum0 = RxCheckSum0 Xor Indata nSwitch0 = Net_RTUADD2 Case Net_RTUADD2 RxAddress0 = (RxAddress0 And &HF0) / 16 * 1000 + (RxAddress0 And &HF) * 100 + (Indata And &HF0) / 16 * 10 + (Indata And &HF) RxDataLen0 = RxDataLen0 + 1 RxCheckSum0 = RxCheckSum0 Xor Indata nSwitch0 = Net_COMMAND
Case Net_COMMAND RxCommand0 = Indata 'RxCommCtr_f = RxCommCtr And &HF RxDataLen0 = RxDataLen0 + 1 RxCheckSum0 = RxCheckSum0 Xor Indata nSwitch0 = Net_RECEDATA 'RxRAC_Fe1 = InData If RxFrameLen0 = (RxDataLen0 + 1) Then '接收二级调度命令判断 nSwitch0 = Net_CHECKSUM End If Case Net_RECEDATA RxBuff0(RxDataLen0 3) = Indata RxDataLen0 = RxDataLen0 + 1 RxCheckSum0 = RxCheckSum0 Xor Indata If RxFrameLen0 = (RxDataLen0 + 1) Then nSwitch0 = Net_CHECKSUM End If Case Net_CHECKSUM RxCheckSum0 = RxCheckSum0 Xor Indata If (RxCheckSum0 = &H0) Then nSwitch0 = Net_STOPBYTE1 Else RxFmHead0 = False '接收帧头失败 nSwitch0 = Net_StartByte1 End If Case Net_STOPBYTE1 If (Indata = &HFF) Then
' nSwitch0 = Net_STOPBYTE2 ReceFHed0 = True '接收帧成功,修改by yyw nSwitch0 = Net_StartByte1 Call ResponeCommCMD(0, RxAddress0, RxFrameLen0, RxCommand0) Else nSwitch0 = Net_StartByte1 End If Case Net_STOPBYTE2 If (Indata = &HFF) Then ReceFHed0 = True '接收帧成功 nSwitch0 = Net_StartByte1 Call ResponeCommCMD(0, RxAddress0, RxFrameLen0, RxCommand0) Else RxFmHead0 = False '接收帧头失败 ReceFHed0 = False '接收帧失败 nSwitch0 = Net_StartByte1 End If End SelectElseIf comm_port = ComMonitor.Comm(0).CommPort Then '1口通信接收处理程序 Select Case nSwitch1 Case One_StartByte1 RxFmHead1 = False '接收帧头失败 If Indata = &H7E Then nSwitch1 = ONE_STARTBYTE2 End If Case ONE_STARTBYTE2 If (Indata = &H7E) Then 'RxFmHead1 = True '接收帧头成功 RxDataLen1 = 0 RxCheckSum1 = 0 nSwitch1 = ONE_RxFrameLen1 Else nSwitch1 = One_StartByte1 End If Case ONE_RxFrameLen1 RxFrameLen1 = Indata If RxFrameLen1 > 2 Then RxFmHead1 = False '接收帧头失败 ReceFHed1 = False '接收帧失败 nSwitch1 = One_StartByte1 Else nSwitch1 = ONE_RxFrameLen2 End If Case ONE_RxFrameLen2 RxFrameLen1 = RxFrameLen1 * 256# + Indata If RxFrameLen1 < 4 Or RxFrameLen1 > 256 Then RxFmHead1 = False '接收帧头失败 ReceFHed1 = False '接收帧失败 nSwitch1 = One_StartByte1 Else nSwitch1 = ONE_RTUADD1 End If RxFmHead1 = True '接收帧头成功 Case ONE_RTUADD1 RxAddress1 = Indata RxDataLen1 = RxDataLen1 + 1 RxCheckSum1 = RxCheckSum1 Xor Indata nSwitch1 = ONE_RTUADD2 Case ONE_RTUADD2 RxAddress1 = (RxAddress1 And &HF0) / 16 * 1000 + (RxAddress1 And &HF) * 100 + (Indata And &HF0) / 16 * 10 + (Indata And &HF) RxDataLen1 = RxDataLen1 + 1 RxCheckSum1 = RxCheckSum1 Xor Indata nSwitch1 = ONE_COMMAND
Case ONE_COMMAND RxCommand1 = Indata 'RxCommCtr_f = RxCommCtr And &HF RxDataLen1 = RxDataLen1 + 1 RxCheckSum1 = RxCheckSum1 Xor Indata nSwitch1 = ONE_RECEDATA 'RxRAC_Fe1 = InData If RxFrameLen1 = (RxDataLen1 + 1) Then '接收二级调度命令判断 nSwitch1 = ONE_CHECKSUM End If Case ONE_RECEDATA RxBuff1(RxDataLen1 3) = Indata RxDataLen1 = RxDataLen1 + 1 RxCheckSum1 = RxCheckSum1 Xor Indata If RxFrameLen1 = (RxDataLen1 + 1) Then nSwitch1 = ONE_CHECKSUM End If Case ONE_CHECKSUM RxCheckSum1 = RxCheckSum1 Xor Indata If (RxCheckSum1 = &H0) Then nSwitch1 = ONE_STOPBYTE1 Else RxFmHead1 = False '接收帧头失败 nSwitch1 = One_StartByte1 End If Case ONE_STOPBYTE1 If (Indata = &HFF) Then
' nSwitch1 = ONE_STOPBYTE2 ReceFHed1 = True '接收帧成功,修改by yyw nSwitch1 = One_StartByte1 Call ResponeCommCMD(1, RxAddress1, RxFrameLen1, RxCommand1) Else nSwitch1 = One_StartByte1 End If Case ONE_STOPBYTE2 If (Indata = &HFF) Then ReceFHed1 = True '接收帧成功 nSwitch1 = One_StartByte1 Call ResponeCommCMD(1, RxAddress1, RxFrameLen1, RxCommand1) 'If (Remote_control_now = False) And (Load_PassData_Fg = False) Then ' Call DDE_SEND_RT 'DDE交换 'End If Else RxFmHead1 = False '接收帧头失败 ReceFHed1 = False '接收帧失败 nSwitch1 = One_StartByte1 End If End SelectElseIf comm_port = ComMonitor.Comm(1).CommPort Then '2口通信接收处理程序 Select Case nSwitch2 Case Two_StartByte1 RxFmHead2 = False '接收帧头失败 If Indata = &H7E Then nSwitch2 = TWO_STARTBYTE2 End If Case TWO_STARTBYTE2 If (Indata = &H7E) Then 'RxFmHead2 = True '接收帧头成功 RxDataLen2 = 0 RxCheckSum2 = 0 nSwitch2 = TWO_RxFrameLen1 Else nSwitch2 = Two_StartByte1 End If Case TWO_RxFrameLen1 RxFrameLen2 = Indata If RxFrameLen2 > 2 Then RxFmHead2 = False '接收帧头失败 ReceFHed2 = False '接收帧失败 nSwitch2 = Two_StartByte1 Else nSwitch2 = TWO_RxFrameLen2 End If Case TWO_RxFrameLen2 RxFrameLen2 = RxFrameLen2 * 256# + Indata If RxFrameLen2 < 4 Or RxFrameLen2 > 256 Then RxFmHead2 = False '接收帧头失败 ReceFHed2 = False '接收帧失败 nSwitch2 = Two_StartByte1 Else nSwitch2 = TWO_RTUADD1 End If RxFmHead2 = True '接收帧头成功 Case TWO_RTUADD1 RxAddress2 = Indata RxDataLen2 = RxDataLen2 + 1 RxCheckSum2 = RxCheckSum2 Xor Indata nSwitch2 = TWO_RTUADD2 Case TWO_RTUADD2 RxAddress2 = (RxAddress2 And &HF0) / 16 * 1000 + (RxAddress2 And &HF) * 100 + (Indata And &HF0) / 16 * 10 + (Indata And &HF) RxDataLen2 = RxDataLen2 + 1 RxCheckSum2 = RxCheckSum2 Xor Indata nSwitch2 = TWO_COMMAND
Case TWO_COMMAND RxCommand2 = Indata 'RxCommCtr_f = RxCommCtr And &HF RxDataLen2 = RxDataLen2 + 1 RxCheckSum2 = RxCheckSum2 Xor Indata nSwitch2 = TWO_RECEDATA 'RxRAC_F2 = InData If RxFrameLen2 = (RxDataLen2 + 1) Then '接收二级调度命令判断 nSwitch2 = TWO_CHECKSUM End If Case TWO_RECEDATA RxBuff2(RxDataLen2 3) = Indata RxDataLen2 = RxDataLen2 + 1 RxCheckSum2 = RxCheckSum2 Xor Indata If RxFrameLen2 = (RxDataLen2 + 1) Then nSwitch2 = TWO_CHECKSUM End If Case TWO_CHECKSUM RxCheckSum2 = RxCheckSum2 Xor Indata If (RxCheckSum2 = &H0) Then nSwitch2 = TWO_STOPBYTE1 Else RxFmHead2 = False '接收帧头失败 nSwitch2 = Two_StartByte1 Call MSgbar("校验错", 1000) End If Case TWO_STOPBYTE1 If (Indata = &HFF) Then
' nSwitch2 = TWO_STOPBYTE2 ReceFHed2 = True '接收帧成功,修改by yyw nSwitch2 = Two_StartByte1 Call ResponeCommCMD(2, RxAddress2, RxFrameLen2, RxCommand2) Else nSwitch2 = Two_StartByte1 End If Case TWO_STOPBYTE2 If (Indata = &HFF) Then ReceFHed2 = True '接收帧成功 nSwitch2 = Two_StartByte1 Call ResponeCommCMD(2, RxAddress2, RxFrameLen2, RxCommand2) 'If (Remote_control_now = False) And (Load_PassData_Fg = False) Then ' Call DDE_SEND_RT 'DDE交换 'End If Else RxFmHead2 = False '接收帧头失败 ReceFHed2 = False '接收帧失败 nSwitch2 = Two_StartByte1 End If End SelectElseIf comm_port = ComMonitor.Comm(2).CommPort Or _ comm_port = ComMonitor.Comm(3).CommPort Or _ comm_port = ComMonitor.Comm(4).CommPort Or _ comm_port = ComMonitor.Comm(5).CommPort Or _ comm_port = ComMonitor.Comm(6).CommPort Or _ comm_port = ComMonitor.Comm(7).CommPort Or _ comm_port > 8 Then '3,4,5,6,7,8口通信接收处理程序,comm_port>8表示使用winsock Select Case nSwitch3 Case Three_StartByte1 RxFmHead3 = False '接收帧头失败 If Indata = &H7E Then nSwitch3 = Three_STARTBYTE2 End If Case Three_STARTBYTE2 If (Indata = &H7E) Then 'RxFmHead1 = True '接收帧头成功 RxDataLen3 = 0 RxCheckSum3 = 0 nSwitch3 = Three_RxFrameLen1 Else nSwitch3 = Three_StartByte1 End If Case Three_RxFrameLen1 RxFrameLen3 = Indata If RxFrameLen3 > 2 Then RxFmHead3 = False '接收帧头失败 ReceFHed3 = False '接收帧失败 nSwitch3 = Three_StartByte1 Else nSwitch3 = Three_RxFrameLen2 End If Case Three_RxFrameLen2 RxFrameLen3 = RxFrameLen3 * 256# + Indata If RxFrameLen3 < 4 Or RxFrameLen3 > 256 Then RxFmHead3 = False '接收帧头失败 ReceFHed3 = False '接收帧失败 nSwitch3 = Three_StartByte1 Else nSwitch3 = Three_RTUADD1 End If RxFmHead3 = True '接收帧头成功 Case Three_RTUADD1 RxAddress3 = Indata RxDataLen3 = RxDataLen3 + 1 RxCheckSum3 = RxCheckSum3 Xor Indata nSwitch3 = Three_RTUADD2 Case Three_RTUADD2 RxAddress3 = (RxAddress3 And &HF0) / 16 * 1000 + (RxAddress3 And &HF) * 100 + (Indata And &HF0) / 16 * 10 + (Indata And &HF) RxDataLen3 = RxDataLen3 + 1 RxCheckSum3 = RxCheckSum3 Xor Indata nSwitch3 = Three_COMMAND
Case Three_COMMAND RxCommand3 = Indata 'RxCommCtr_f = RxCommCtr And &HF RxDataLen3 = RxDataLen3 + 1 RxCheckSum3 = RxCheckSum3 Xor Indata nSwitch3 = Three_RECEDATA 'RxRAC_Fe1 = InData If RxFrameLen3 = (RxDataLen3 + 1) Then '接收二级调度命令判断 nSwitch3 = Three_CHECKSUM End If Case Three_RECEDATA RxBuff3(RxDataLen3 3) = Indata RxDataLen3 = RxDataLen3 + 1 RxCheckSum3 = RxCheckSum3 Xor Indata If RxFrameLen3 = (RxDataLen3 + 1) Then nSwitch3 = Three_CHECKSUM End If Case Three_CHECKSUM RxCheckSum3 = RxCheckSum3 Xor Indata If (RxCheckSum3 = &H0) Then nSwitch3 = Three_STOPBYTE1 Else RxFmHead3 = False '接收帧头失败 nSwitch3 = Three_StartByte1 End If Case Three_STOPBYTE1 If (Indata = &HFF) Then nSwitch3 = Three_STOPBYTE2 Else nSwitch3 = Three_StartByte1 End If Case Three_STOPBYTE2 If (Indata = &HFF) Then ReceFHed3 = True '接收帧成功 nSwitch3 = Three_StartByte1 Call ResponeCommCMD(comm_port, RxAddress3, RxFrameLen3, RxCommand3) 'If (Remote_control_now = False) And (Load_PassData_Fg = False) Then ' Call DDE_SEND_RT 'DDE交换 'End If Else RxFmHead3 = False '接收帧头失败 ReceFHed3 = False '接收帧失败 nSwitch3 = Three_StartByte1 End If End SelectElseIf comm_port = Modem_Comm Then '2口通信接收处理程序 Select Case MnSwitch Case M_StartByte1 RxFmHeadM = False '接收帧头失败 If Indata = &H7E Then MnSwitch = M_STARTBYTE2 End If Case M_STARTBYTE2 If (Indata = &H7E) Then RxFmHeadM = True '接收帧头成功 RxDataLenM = 0 RxCheckSumM = 0 MnSwitch = M_RxFrameLen1 Else MnSwitch = M_StartByte1 End If Case M_RxFrameLen1 RxFrameLenM = Indata If RxFrameLenM > 2 Then RxFmHeadM = False '接收帧头失败 ReceFHedM = False '接收帧失败 MnSwitch = M_StartByte1 Else MnSwitch = M_RxFrameLen2 End If Case M_RxFrameLen2 RxFrameLenM = RxFrameLenM * 256# + Indata If RxFrameLenM < 4 Or RxFrameLenM > 256 Then RxFmHeadM = False '接收帧头失败 ReceFHedM = False '接收帧失败 MnSwitch = M_StartByte1 Else MnSwitch = M_RTUADD1 End If RxFmHead2 = True '接收帧头成功 Case M_RTUADD1 RxAddressM = Indata RxDataLenM = RxDataLenM + 1 RxCheckSumM = RxCheckSumM Xor Indata MnSwitch = M_RTUADD2 Case M_RTUADD2 RxAddressM = (RxAddressM And &HF0) / 16 * 1000 + (RxAddress2 And &HF) * 100 + (Indata And &HF0) / 16 * 10 + (Indata And &HF) RxDataLenM = RxDataLenM + 1 RxCheckSumM = RxCheckSumM Xor Indata MnSwitch = M_COMMAND
Case M_COMMAND RxCommandM = Indata 'RxCommCtr_f = RxCommCtr And &HF RxDataLenM = RxDataLenM + 1 RxCheckSumM = RxCheckSumM Xor Indata MnSwitch = M_RECEDATA 'RxRAC_F2 = InData If RxFrameLenM = (RxDataLenM + 1) Then '接收二级调度命令判断 MnSwitch = M_CHECKSUM End If Case M_RECEDATA RxBuffM(RxDataLenM 3) = Indata RxDataLenM = RxDataLenM + 1 RxCheckSumM = RxCheckSumM Xor Indata If RxFrameLenM = (RxDataLenM + 1) Then MnSwitch = M_CHECKSUM End If Case M_CHECKSUM RxCheckSumM = RxCheckSumM Xor Indata If (RxCheckSumM = &H0) Then MnSwitch = M_STOPBYTE1 Else RxFmHeadM = False '接收帧头失败 MnSwitch = M_StartByte1 Call MSgbar("校验错", 1000) End If Case M_STOPBYTE1 If (Indata = &HFF) Then
' MnSwitch = M_STOPBYTE2 ReceFHedM = True '接收帧成功,修改by yyw MnSwitch = M_StartByte1 Call ResponeCommCMD(Modem_Comm, RxAddressM, RxFrameLenM, RxCommandM) Else MnSwitch = M_StartByte1 End If Case M_STOPBYTE2 If (Indata = &HFF) Then ReceFHedM = True '接收帧成功 MnSwitch = M_StartByte1 Call ResponeCommCMD(Modem_Comm, RxAddressM, RxFrameLenM, RxCommandM) 'If (Remote_control_now = False) And (Load_PassData_Fg = False) Then ' Call DDE_SEND_RT 'DDE交换 'End If Else RxFmHead2 = False '接收帧头失败 ReceFHed2 = False '接收帧失败 MnSwitch = M_StartByte1 End If End Select

End IfExit Sub
CloseRece:Exit Sub
End Sub

此函数在自定义协议中使用,首先去掉桢头,进行校验,留有用数据处理

liuzhyb | 2009-6-1 16:30:00 | 显示全部楼层
好难懂呀
fschs | 2009-12-2 09:31:00 | 显示全部楼层
传说中的天书,谢谢分享
bretch | 2009-12-8 23:57:00 | 显示全部楼层

是不是格式不太对,可怕

huoxing_99 | 2010-8-6 12:11:00 | 显示全部楼层
传说中的天书,谢谢分享 看的人费劲了
您需要登录后才可以回帖 登录 | 注册哦

本版积分规则