1. DB块打开后,选择generate sources功能,完成后会在S7资源管理器sources下看到生成的source文件
2. 打开source文件,复制到excel里面做处理,trim前面的空格,下面VBA脚本即为针对纯struct块的处理过程
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub CreateTag()
Dim a As Variant
Dim b As Variant
Dim szGroup, szTagname, szDBzone, szDatatype, szDBlock As String
Dim iRow, iColumn, iCurrentrow, iDatalength, iEndrow, iAddress, iFlag, iBit, iLastrow, iFlag2, i As Integer
Dim bInstruct As Boolean
bInstruct = True
szDBlock = "DB20"
iCurrentrow = 1
iEndrow = 364
iAddress = -100
iFlag = 1
iFlag2 = 1
iBit = 0
For i = iCurrentrow To iEndrow
If InStr(Cells(i, 1), "STRUCT") > 0 And Cells(i, 1) <> "END_STRUCT ;" Then
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
szGroup = b
If iFlag2 = 1 Then
iAddress = iAddress + 100
iFlag2 = -1
Else
iAddress = Int(iAddress / 100) * 100 + 100
End If
iFlag = 1
iBit = 0
ElseIf Cells(i, 1) = "END_STRUCT ;" Then
bInstruct = False
ElseIf InStr(Cells(i, 1), "ARRAY") > 0 Then
bInstruct = False
Else
Cells(i, 3) = "NewConnection"
Cells(i, 6) = 0
Cells(i, 9) = 4
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
szTagname = szGroup & "_" & b
Cells(i, 2) = szTagname
Cells(i, 4) = szGroup
If InStr(Cells(i, 1), "REAL") > 0 Then
If iFlag = 1 Then
Cells(i, 5) = szDBlock & "," & "DD" & iAddress
iFlag = -1
Cells(i, 7) = 8
Cells(i, 8) = 4
Else
If InStr(Cells(iLastrow, 1), "BOOL") > 0 Then
iAddress = iAddress + 2
Else
iAddress = iAddress + 4
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
Cells(i, 5) = szDBlock & "," & "DD" & iAddress
Cells(i, 7) = 8
Cells(i, 8) = 4
End If
ElseIf InStr(Cells(i, 1), "BOOL") > 0 Then
If iFlag = 1 Then
Cells(i, 5) = szDBlock & "," & "D" & iAddress & "." & iBit
Cells(i, 7) = 1
Cells(i, 8) = 1
iFlag = -1
Else
iBit = iBit + 1
Cells(i, 5) = szDBlock & "," & "D" & iAddress & "." & iBit
Cells(i, 7) = 1
Cells(i, 8) = 1
End If
Else
iAddress = iAddress + 2
Cells(i, 5) = szDBlock & "," & "DBW" & iAddress
Cells(i, 7) = 5
Cells(i, 8) = 2
End If
End If
iLastrow = i
Next i
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果DB的struct块里面还有struct块,则用如下代码:20150521 update
Sub CreateTag()
Dim a As Variant
Dim b As Variant
'szTagname = szOutergroup_szIntergroup_元素
Dim szOutergroup, szIntergroup, szTagname, szDBzone, szDatatype, szDBlock, szLastdatatype As String
Dim iRow, iColumn, iCurrentrow, iDatalength, iEndrow, iAddress, iFlag, iBit, iLastrow, iFlag2, iStructflag, iPosition, iArrayLength, iCount, i As Integer
'iStrucflag表示结构体的开始,结束以及中间结构体,0表示结束,1表示开始,-1表示外层结构体结束
'iPosition indicate the ".." position
Dim bInstruct As Boolean
bInstruct = True
szDBlock = "DB10"
iCurrentrow = 1
iEndrow = 254
iAddress = -200
iFlag = 1
iFlag2 = 1
iBit = 0
iStructflag = -1
iCount = -1
For i = iCurrentrow To iEndrow
If InStr(Cells(i, 1), "STRUCT") > 0 And Cells(i, 1) <> "END_STRUCT ;" Then
iStructflag = iStructflag + 1
If iStructflag = 0 Then
iCount = iCount + 1
End If
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
If iStructflag = 0 Then
szOutergroup = b
Else
szIntergroup = b
End If
If iFlag2 = 1 Then
iAddress = iAddress + 200
iFlag2 = -1
Else
If iStructflag = 0 Then
iAddress = iCount * 200
iFlag = 1
End If
End If
'iFlag = 1
'iBit = 0
ElseIf Cells(i, 1) = "END_STRUCT ;" Then
iStructflag = iStructflag - 1
bInstruct = False
ElseIf InStr(Cells(i, 1), "ARRAY") > 0 Then
'spare : ARRAY [1 .. 6 ] OF BYTE ;
iPosition = InStr(Cells(i, 1), "..")
iPosition = iPosition + 3
iArrayLength = Trim(Mid(Cells(i, 1), iPosition, 2))
If szLastdatatype = "REAL" Then
iAddress = iAddress + 4
ElseIf szLastdatatype = "BOOL" Then
iAddress = iAddress + 1
End If
If iAddress Mod 2 <> 0 Then
iAddress = iAddress + 1
End If
If InStr(Cells(i, 1), "BYTE") > 0 Then
iAddress = iAddress + iArrayLength
Else
iAddress = iAddress + iArrayLength / 8
End If
szLastdatatype = "ARRAY"
Else
Cells(i, 3) = "UnderGroundTank" 'cells(i,3) indicates the group name below TCP/IP driver folder
Cells(i, 6) = 0
Cells(i, 9) = 4
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
If iStructflag = 0 Then
szTagname = szOutergroup & "_" & b
Else
szTagname = szOutergroup & "_" & szIntergroup & "_" & b
End If
Cells(i, 2) = szTagname
Cells(i, 4) = szOutgroup
If InStr(Cells(i, 1), "REAL") > 0 Then
If iFlag = 1 Then
iFlag = -1
Else
If szLastdatatype <> "REAL" Then
iAddress = iAddress + 1
Else
iAddress = iAddress + 4
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
End If
Cells(i, 5) = szDBlock & "," & "DD" & iAddress
Cells(i, 7) = 8
Cells(i, 8) = 4
szLastdatatype = "REAL"
ElseIf InStr(Cells(i, 1), "BOOL") > 0 Then
If szLastdatatype <> "BOOL" Then
iAddress = iAddress + 1
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
iBit = 0
Else
iBit = iBit + 1
If iBit > 7 Then
iBit = 0
iAddress = iAddress + 1
End If
End If
Cells(i, 5) = szDBlock & "," & "D" & iAddress & "." & iBit
Cells(i, 7) = 1
Cells(i, 8) = 1
szLastdatatype = "BOOL"
Else
If szLastdatatype <> "other" Then
If szLastdatatype = "REAL" Then
iAddress = iAddress + 4
End If
If szLastdatatype = "BOOL" Then
iAddress = iAddress + 1
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
Else
iAddress = iAddress + 2
End If
Cells(i, 5) = szDBlock & "," & "DBW" & iAddress
Cells(i, 7) = 5
Cells(i, 8) = 2
szLastdatatype = "other"
End If
End If
Next i
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20150521 update struct 2
Sub CreateTag()
Dim a As Variant
Dim b As Variant
'szTagname = szOutergroup_szIntergroup_元素
Dim szOutergroup, szIntergroup, szTagname, szDBzone, szDatatype, szDBlock, szLastdatatype As String
Dim iRow, iColumn, iCurrentrow, iDatalength, iEndrow, iAddress, iFlag, iBit, iLastrow, iFlag2, iStructflag, iPosition, iArrayLength, iCount, i As Integer
'iStrucflag表示结构体的开始,结束以及中间结构体,0表示结束,1表示开始,-1表示外层结构体结束
'iPosition indicate the ".." position
Dim bInstruct As Boolean
bInstruct = True
szDBlock = "DB15"
iCurrentrow = 1
iEndrow = 328
iAddress = -200
iFlag = 1
iFlag2 = 1
iBit = 0
iStructflag = -1
iCount = -1
szLastdatatype = "BOOL" '数据db开始的第一个数据类型,运行过程中记录上一次数据类型。
For i = iCurrentrow To iEndrow
If InStr(Cells(i, 1), "STRUCT") > 0 And Cells(i, 1) <> "END_STRUCT ;" Then
iStructflag = iStructflag + 1
If iStructflag = 0 Then
iCount = iCount + 1
End If
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
If iStructflag = 0 Then
szOutergroup = b
Else
szIntergroup = b
End If
If iFlag2 = 1 Then
iAddress = iAddress + 200
iFlag2 = -1
Else
If iStructflag = 0 Then
'iAddress = iCount * 100
iFlag = 1
End If
End If
'iFlag = 1
'iBit = 0
ElseIf Cells(i, 1) = "END_STRUCT ;" Then
iStructflag = iStructflag - 1
bInstruct = False
ElseIf InStr(Cells(i, 1), "ARRAY") > 0 Then
'spare : ARRAY [1 .. 6 ] OF BYTE ;
iPosition = InStr(Cells(i, 1), "..")
iPosition = iPosition + 3
iArrayLength = Trim(Mid(Cells(i, 1), iPosition, 2))
If szLastdatatype = "REAL" Then
iAddress = iAddress + 4
ElseIf szLastdatatype = "BOOL" Then
iAddress = iAddress + 1
Else
iAddress = iAddress + 2
End If
If iAddress Mod 2 <> 0 Then '如果地址不从偶数开始,则加1设定为偶数
iAddress = iAddress + 1
End If
If InStr(Cells(i, 1), "BYTE") > 0 Then
iAddress = iAddress + iArrayLength - 1
Else
iAddress = iAddress + iArrayLength / 8 - 1
End If
szLastdatatype = "ARRAY"
Else
Cells(i, 3) = "HalfLiquid" 'cells(i,3) indicates the group name below TCP/IP driver folder
Cells(i, 6) = 0
Cells(i, 9) = 4
a = Split(Cells(i, 1), ":")
b = a(0)
b = Mid(b, 1, Len(b) - 1)
If iStructflag = 0 Then
szTagname = szOutergroup & "_" & b
Else
szTagname = szOutergroup & "_" & szIntergroup & "_" & b
End If
Cells(i, 2) = szTagname
Cells(i, 4) = szOutergroup
If InStr(Cells(i, 1), "REAL") > 0 Then
If iFlag = 1 Then
iFlag = -1
Else
If szLastdatatype <> "REAL" Then
iAddress = iAddress + 1
Else
iAddress = iAddress + 4
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
End If
Cells(i, 5) = szDBlock & "," & "DD" & iAddress
Cells(i, 7) = 8
Cells(i, 8) = 4
szLastdatatype = "REAL"
ElseIf InStr(Cells(i, 1), "BOOL") > 0 Then
If szLastdatatype <> "BOOL" Then
If szLastdatatype = "ARRAY" Or szLastdatatype = "BYTE" Then
iAddress = iAddress + 1
ElseIf szLastdatatype = "REAL" Then
iAddress = iAddress + 4
Else
iAddress = iAddress + 2
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
iBit = 0
Else
If iFlag = 1 Then
iBit = 0
iFlag = -1
Else
iBit = iBit + 1
If iBit > 7 Then
iBit = 0
iAddress = iAddress + 1
End If
End If
End If
Cells(i, 5) = szDBlock & "," & "D" & iAddress & "." & iBit
Cells(i, 7) = 1
Cells(i, 8) = 1
szLastdatatype = "BOOL"
Else
If szLastdatatype <> "other" Then
If szLastdatatype = "REAL" Then
iAddress = iAddress + 4
End If
If szLastdatatype = "BOOL" Then
iAddress = iAddress + 1
End If
If (iAddress Mod 2 <> 0) Then
iAddress = iAddress + 1
End If
Else
iAddress = iAddress + 2
End If
Cells(i, 5) = szDBlock & "," & "DBW" & iAddress
Cells(i, 7) = 5
Cells(i, 8) = 2
szLastdatatype = "other"
End If
End If
Next i
End Sub