📄 frmmain.frm
字号:
Dim wCheckSum1 As Integer
Dim wCheckSum2 As Integer
Dim wCommandID As Byte 'bCommandID 改为 wCommandID
Dim wLoop As Integer
Dim szInbuffer() As Byte
Dim wInBuffer As Integer
Dim AckCode As Long
Dim Command As Byte
Dim temp1() As Byte
Dim temp2() As Byte
Dim ret%
On Error Resume Next
AnalyzeCommand = True
AckCode = CMDOK
If szAnalyzeBuffer(0) <> SYNBYTE Then
AnalyzeCommand = False
AckCode = CMDERROR
' Exit Function
End If
If AckCode <> CMDERROR And wAnalyzeBuffer >= 12 Then
wCommandSize = szAnalyzeBuffer(10) * &H100 + szAnalyzeBuffer(11)
If wCommandSize + 15 > wAnalyzeBuffer Then
AnalyzeCommand = False
AckCode = CMDERROR
' Exit Function
End If
If AckCode <> CMDERROR Then
wCheckSum1 = szAnalyzeBuffer(wCommandSize + 12) * &H100 + szAnalyzeBuffer(wCommandSize + 13)
For wLoop = 1 To wCommandSize + 11 Step 1
wCheckSum2 = (wCheckSum2 + szAnalyzeBuffer(wLoop)) Mod &H10000
Next wLoop
If wCheckSum1 <> wCheckSum2 Then
AnalyzeCommand = False
AckCode = CMDERROR
' Exit Function
End If
End If
End If
If AckCode = CMDERROR Then
Call HandleOutput("Communication Data Error.")
End If
wCommandID = szAnalyzeBuffer(1)
Select Case wCommandID
Case READDATA
If SysDefine.CommType = COMMSERVER Then
If AckCode = CMDOK Then
Call BuildReadCommand(szInbuffer, wInBuffer)
Else
Call BuildCommand(szInbuffer, wInBuffer, READDATA, CMDERROR, 0, 0, 0, temp1, temp2, 0)
End If
Call HexToAsc(szInbuffer, szOutBuffer, wInBuffer, wOutBuffer)
'Call Sleep(SysDefine.SampleTime)
'MSComm.RTSEnable = True
'SetRTSTimer.Enabled = True
ret = sio_RTS(SysDefine.CommPort, 1)
If ret = SIO_OK Then
TimeDelay 100
Call HandleOutput("[" + Format(CommCounter) + "]" + "Write " + Format(UBound(szOutBuffer) - LBound(szOutBuffer) + 1) + " Bytes >>>>")
CommCounter = (CommCounter + 1) Mod &H80000000
If bMonitor Then
Call Display(szOutBuffer)
End If
Call sio_write(SysDefine.CommPort, szOutBuffer(0), UBound(szOutBuffer) + 1)
TimeDelay 20
ret = sio_RTS(SysDefine.CommPort, 0)
If ret <> SIO_OK Then
Debug.Print "RTS置低出错"
Exit Function
End If
Else
Debug.Print "RTS置高出错"
Exit Function
End If
End If
Case WRITEDATA
If SysDefine.CommType = COMMCLIENT Then
' CommTimer.Enabled = False
If AckCode <> CMDERROR Then
If AnalyzeReadCommand(szAnalyzeBuffer, wAnalyzeBuffer) Then
Call BuildCommand(szInbuffer, wInBuffer, READDATA, CMDOK, 0, 0, 0, temp1, temp2, 0)
Else
Call BuildCommand(szInbuffer, wInBuffer, READDATA, CMDERROR, 0, 0, 0, temp1, temp2, 0)
End If
End If
' Call HexToAsc(szInbuffer, szOutBuffer, wInBuffer, wOutBuffer)
' ret = sio_RTS(SysDefine.CommPort, 1)
' If ret = SIO_OK Then
' TimeDelay 100
' Call HandleOutput("[" + Format(CommCounter) + "]" + "Write " + Format(UBound(szOutBuffer) - LBound(szOutBuffer) + 1) + " Bytes >>>>")
' CommCounter = (CommCounter + 1) Mod &H80000000
' If bMonitor Then
' Call Display(szOutBuffer)
' End If
' Call sio_write(SysDefine.CommPort, szOutBuffer(0), UBound(szOutBuffer) + 1)
' TimeDelay 20
' ret = sio_RTS(SysDefine.CommPort, 0)
' If ret <> SIO_OK Then
' Debug.Print "RTS置低出错"
' Exit Function
' End If
' Else
' Debug.Print "RTS置高出错"
' Exit Function
' End If
' CommTimer.Enabled = True
End If
End Select
End Function
'
' 建立采样命令 : 获取数据,存入数组,调用BuildCommand()
'
Private Function BuildReadCommand(szOutputBuffer() As Byte, _
wOutputBuffer As Integer) _
As Boolean
Dim wLoop As Integer
Dim szDigitalBuffer() As Byte
Dim szAnalogBuffer() As Byte
Dim dfValue As Double
Dim btFlag As Byte
Dim Number As Integer
Dim AnalogNumber As Integer
Dim DigitalNumber As Integer
Dim TotalSize As Integer
On Error Resume Next
Number = 0
TotalSize = 0
AnalogNumber = 0
DigitalNumber = 0
If wReadDigitalNodePoint > 0 Then
For wLoop = 0 To wReadDigitalNodePoint - 1 Step 1
dfValue = CDbl(GetDataText(wLoop).Text)
'If ReadMem(pndReadDigitalCommandPoint(wLoop).TagTab, pndReadDigitalCommandPoint(wLoop).TagID, dfValue, btFlag) Then
If dfValue <> pndReadDigitalCommandPoint(wLoop).Value Or Number > 0 Then
Number = Number + 1
If Number > 8 Then
Number = 0
If TotalSize >= SysDefine.Number \ 2 Then
Exit For
End If
Else
If Number = 1 Then
ReDim Preserve szDigitalBuffer(DigitalNumber * 3 + 2) As Byte
szDigitalBuffer(DigitalNumber * 3) = pndReadDigitalCommandPoint(wLoop).ID \ &H100
szDigitalBuffer(DigitalNumber * 3 + 1) = pndReadDigitalCommandPoint(wLoop).ID Mod &H100
szDigitalBuffer(DigitalNumber * 3 + 2) = 0
TotalSize = TotalSize + 3
DigitalNumber = DigitalNumber + 1
End If
If dfValue < 0.000001 Then
szDigitalBuffer(DigitalNumber * 3 - 1) = ResetBits(szDigitalBuffer(DigitalNumber * 3 - 1), Number - 1)
pndReadDigitalCommandPoint(wLoop).Value = 0#
Else
szDigitalBuffer(DigitalNumber * 3 - 1) = SetBits(szDigitalBuffer(DigitalNumber * 3 - 1), Number - 1)
pndReadDigitalCommandPoint(wLoop).Value = 1#
End If
End If
End If
'End If
Next wLoop
End If
Number = 0
If wReadAnalogNodePoint > 0 Then
For wLoop = 0 To wReadAnalogNodePoint - 1 Step 1
'If TotalSize >= SysDefine.Number \ 2 Then
' Exit Function
'End If
'dfValue = CDbl(GetDataText(wLoop + StartID).Text)
dfValue = CDbl(GetDataText(wLoop).Text)
' If ReadMem(pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).TagTab, pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).TagID, dfValue, btFlag) Then
If Abs(dfValue - pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).Value) > Abs(pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).DeadArea * pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).Value / 100) Then
Select Case pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).DataType
Case Asc("R")
ReDim Preserve szAnalogBuffer(Number + 7) As Byte
dfValue = dfValue * (10 ^ pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).Power)
szAnalogBuffer(Number + 0) = (((dfValue \ &H10000) \ &H10000) \ &H10000) \ &H100
szAnalogBuffer(Number + 1) = (((dfValue \ &H10000) \ &H10000) \ &H10000) Mod &H100
szAnalogBuffer(Number + 2) = (((dfValue \ &H10000) \ &H10000) Mod &H10000) \ &H100
szAnalogBuffer(Number + 3) = (((dfValue \ &H10000) \ &H10000) Mod &H10000) Mod &H100
szAnalogBuffer(Number + 4) = ((dfValue \ &H10000) Mod &H10000) \ &H100
szAnalogBuffer(Number + 5) = ((dfValue \ &H10000) Mod &H10000) Mod &H100
szAnalogBuffer(Number + 6) = (dfValue Mod &H10000) \ &H100
szAnalogBuffer(Number + 7) = (dfValue Mod &H10000) Mod &H100
Number = Number + 8
TotalSize = TotalSize + 8
Case Asc("F")
ReDim Preserve szAnalogBuffer(Number + 3) As Byte
dfValue = dfValue * (10 ^ pndReadAnalogCommandPoint((wLoop + StartID) Mod wReadAnalogNodePoint).Power)
szAnalogBuffer(Number + 0) = (dfValue \ &H10000) \ &H100
szAnalogBuffer(Number + 1) = (dfValue \ &H10000) Mod &H100
szAnalogBuffer(Number + 2) = (dfValue Mod &H10000) \ &H100
szAnalogBuffer(Number + 3) = (dfValue Mod &H10000) Mod &H100
Number = Number + 4
TotalSize = TotalSize + 4
Case Asc("L")
ReDim Preserve szAnalogBuffer(Number + 3) As Byte
szAnalogBuffer(Number + 0) = (dfValue \ &H10000) \ &H100
szAnalogBuffer(Number + 1) = (dfValue \ &H10000) Mod &H100
szAnalogBuffer(Number + 2) = (dfValue Mod &H10000) \ &H100
szAnalogBuffer(Number + 3) = (dfValue Mod &H10000) Mod &H100
Number = Number + 4
TotalSize = TotalSize + 4
Case Asc("I")
ReDim Preserve szAnalogBuffer(Number + 1) As Byte
szAnalogBuffer(Number + 0) = (dfValue Mod &H10000) \ &H100
szAnalogBuffer(Number + 1) = (dfValue Mod &H10000) Mod &H100
Number = Number + 2
TotalSize = TotalSize + 2
End Select
Else
ReDim Preserve szAnalogBuffer(Number) As Byte
szAnalogBuffer(Number) = Asc("=")
TotalSize = TotalSize + 1
Number = Number + 1
End If
' Else
' ReDim Preserve szAnalogBuffer(Number) As Byte
' szAnalogBuffer(Number) = Asc("=")
' TotalSize = TotalSize + 1
' Number = Number + 1
' End If
AnalogNumber = AnalogNumber + 1
Next wLoop
End If
BuildReadCommand = BuildCommand(szOutputBuffer, wOutputBuffer, WRITEDATA, CMDOK, DigitalNumber, StartID, AnalogNumber, szDigitalBuffer, szAnalogBuffer, TotalSize)
If wReadAnalogNodePoint > 0 And AnalogNumber > 0 Then
StartID = (StartID + AnalogNumber) Mod wReadAnalogNodePoint
End If
End Function
'
' 分析采样结果 : 写入MACAM
'
Private Function AnalyzeReadCommand(szInputBuffer() As Byte, _
wInputBuffer As Integer) _
As Boolean
Dim DINumber As Integer
Dim AINumber As Integer
Dim StartID As Integer
Dim StartDI As Integer
Dim EndDI As Integer
Dim wLoop As Integer
Dim wStep As Integer
Dim wSize As Integer
Dim wLocation As Integer
Dim wLength As Integer
Dim Power As Long
Dim dfValue As Double
Dim btFlag As Byte
On Error Resume Next
DINumber = szInputBuffer(4) * &H100 + szInputBuffer(5)
StartID = szInputBuffer(6) * &H100 + szInputBuffer(7)
AINumber = szInputBuffer(8) * &H100 + szInputBuffer(9)
wSize = 12
If DINumber > 0 And wReadDigitalNodePoint > 0 Then
For wLoop = 1 To DINumber Step 1
StartDI = szInputBuffer(wSize + 0) * &H100 + szInputBuffer(wSize + 1)
EndDI = IIf(StartDI + 7 > wReadDigitalNodePoint, wReadDigitalNodePoint, StartDI + 7)
For wStep = StartDI To EndDI Step 1
dfValue = GetBits(szInputBuffer(wSize + 2), wStep - StartDI) 'Step 改为 wStep
Call WriteMem(pndReadDigitalCommandPoint(wStep).TagTab, pndReadDigitalCommandPoint(wStep).TagID, dfValue, btFlag)
Next wStep
wSize = wSize + 3
Next wLoop
Else
wSize = wSize + DINumber * 3
End If
If AINumber > 0 Then
For wLoop = StartID To StartID + AINumber - 1 Step 1
wLocation = wLoop Mod wReadAnalogNodePoint
If szInputBuffer(wSize) <> Asc("=") Then
Select Case pndReadAnalogCommandPoint(wLocation).DataType
Case Asc("R")
wLength = 8
Power = 10 ^ pndReadAnalogCommandPoint(wLocation).Power
Case Asc("F")
wLength = 4
Power = 10 ^ pndReadAnalogCommandPoint(wLocation).Power
Case Asc("L")
wLength = 4
Power = 1
Case Asc("I")
wLength = 2
Power = 1
End Select
dfValue = 0#
For wStep = 1 To wLength Step 1
dfValue = dfValue * &H100 + szInputBuffer(wSize)
wSize = wSize + 1
Next wStep
dfValue = dfValue / Power
Call WriteMem(pndReadAnalogCommandPoint(wLocation).TagTab, pndReadAnalogCommandPoint(wLocation).TagID, dfValue, btFlag)
Else
wSize = wSize + 1
End If
Next wLoop
End If
AnalyzeReadCommand = True
End Function
'
' 建立命令 : 填充szOutputBuffer()
'
Private Function BuildCommand(szOutputBuffer() As Byte, _
wOutputBuffer As Integer, _
Command As Byte, _
AckCode As Long, _
DINumber As Integer, _
StartID As Integer, _
AINumber As Integer, _
DIBuffer() As Byte, _
AIBuffer() As Byte, _
TotalSize As Integer) _
As Boolean
' AckCode As Intege 改为 AckCode As Long
Dim wLoop As Integer
Dim wSize As Integer
Dim wCheckSum As Integer
wCheckSum = 0
ReDim szOutputBuffer(TotalSize + 14) As Byte
szOutputBuffer(0) = SYNBYTE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -