Você está na página 1de 4

6407 - VBA Sample - DLG ROI Example to capture PLC timestamped data.

VBA Sample - DLG ROI Example to capture PLC timestamped data


Background:
RSView's datalog sub-system will time stamp all data logged. When the PLC needs to time stamp the data because
the process is time critical, this application example can be consulted as a starting point. In this example 2 datalog
models are created. The first model is to capture the PLC timestamped data via a DataLogSnapShot
command. Some trigger mechanism between the HMI and PLC needs to be developed to generate the
snapshot. When that snapshot is complete, the PLC ladder logic should reinitialize the data table. The second
model used in this example is hard coded to reduce the number of parameters passed into the VBA sub-routine. The
second model is called TrendData; it is of type wide, and contains 1 tag called TrendData\Word1. Because the PLC
is timestamping the data, the second model should NEVER be started. Starting the datalog model will create a B
record (for begin) with the current time stamp, and prevent any earlier PLC timestamped data from being saved.

PLC Data description:
The PLC5 ladder logic is structured so that 10 tag values comprise one PLC timestamped data record. The data is
layed out in 10 word groupings in an integer file as follows. Because the PLC5 is limited to 1000 element tables,
this example stops at that point. The CL5550 could be used to go beyond that limit. When traversing the datalog
collection, the tag's units are checked to determine how to process the record. The tag units are used to determine
what the record type is because the tagname must be unique for each record in the narrow model.

PLC Data table offset Description PLC status register RSView Tag units value
N9:0 Month S:19 Month
N9:1 Day S:20 Day
N9:2 Year S:18 Year
N9:3 Hour S:21 Hour
N9:4 Minute S:22 Minute
N9:5 Second S:23 Second
N9:6 Millisecond T4:0.ACC Millisecond
N9:7 Value N9:7 Value
N9:8 OPEN N9:8 (value not used) ""
N9:9 END N9:9 (value not used) End

Data repeats every 10 words.

VBA Code:

Sub CreatePLCTimeStampedData(Sstart As String, sEnd As String, sModel As
String)

'this example shows how to read plc5 time stamped data from a narrow model,
'writing that data back to a wide model.
'this example hard codes the wide model name to TrendData, and
'hard codes the tag name to TrendData\Word1

'TrendX is used to trend the data to the millisecond granularity
'---------------------------------------------------------------

Dim oDLModel As DataLogModel 'used for narrow model read
Dim oDLModel2 As DataLogModel 'used for wide model write
Dim oTagList As StringList
Dim oDLNarrowRec As DataLogNarrowRecord
Dim oDLNarrowRecs As DataLogNarrowRecords
Dim dtStart As Date
Dim dtEnd As Date

Dim oMyTag As Tag
Dim X As Integer 'used to walk dlg collection
'working storage for PLC logged data
Dim iMonth As Integer
Dim iDay As Integer
Dim iYear As Integer
Dim iHour As Integer
Dim iMinute As Integer
Dim iSecond As Integer
Dim iMillisecond As Integer
Dim iValue As Integer
Dim myDate As Date
Dim iPos As Integer
Dim iNumRecs As Integer

'resultant data storage
Dim oDLWideRec As DataLogWideRecord
Dim oDLTagValue As DataLogTagValue

On Error GoTo err_Handler

'the PC time is captured in a string tag initially
'this tag is then passed to VBA as a string.
'Since there is NO way to extract the contents of the string
'tag on the command line, a tag object must be used to refernce
'the time date string.
'RSView date/time string contains the day of the week
'the following code serves to remove this string from the
'time string so that VBA can deal with it

Set oMyTag = gTagDb.GetTag(Sstart)
Sstart = oMyTag.Value
iPos = InStr(1, Sstart, ",") + 1
Sstart = Right(Sstart, Len(Sstart) - iPos)
dtStart = CDate(Sstart)

Set oMyTag = gTagDb.GetTag(sEnd)
sEnd = oMyTag.Value
iPos = InStr(1, sEnd, ",") + 1
sEnd = Right(sEnd, Len(sEnd) - iPos)
dtEnd = CDate(sEnd)

'initialize the datalog object to be the model name specified
Set oDLModel = gDataLog.DataLogModels(sModel)

' Set the list of tags to be all the tags.
Set oTagList = oDLModel.DataLogModelCfg.TagsInModel


' Retrieve all the tags for the model from dtStart to dtEnd.
' The Milliseconds are included from dtStart.000 to dtEnd.999
Set oDLNarrowRecs = oDLModel.ReadTagDataNarrow(oTagList, _
dtStart, dtEnd, , roMaxReadRecordsAll)
If oDLNarrowRecs.Count > 0 Then
'create the widemodel record now...
Set oDLModel2 = gDataLog.DataLogModels("TrendData")
iNumRecs = 0
'---------override the default collection traverse order now!
'For Each oDLNarrowRec In oDLNarrowRecs traverses the collection
'in reverse order
For X = oDLNarrowRecs.Count To 1 Step -1
Set oDLNarrowRec = oDLNarrowRecs(X)
'ignore the begin /end markers of data as this is not relevant to
the process
'we are only interested the data collected via the datalogsnapshot
command
With oDLNarrowRec
If .Marker = roMarkerSnapshot Then
'the tag's units field determines how the record is parsed
'because the tag name is unique for each plc record, the
units
'is used to build the wide record from multiple narrow
records
Set oMyTag = gTagDb.GetTag(.DataLogTagValue.TagName)
Select Case oMyTag.Units
Case "Month"
iMonth = .DataLogTagValue.Value

Case "Day"
iDay = .DataLogTagValue.Value

Case "Year"
iYear = .DataLogTagValue.Value

Case "Hour"
iHour = .DataLogTagValue.Value

Case "Minute"
iMinute = .DataLogTagValue.Value

Case "Second"
iSecond = .DataLogTagValue.Value

Case "Millisecond"
iMillisecond = .DataLogTagValue.Value

Case "Value"
iValue = .DataLogTagValue.Value

Case "End"
'Now here is where we do all the work,
'should have all the temporary values initialized
'and ready for our custom record.
'hard wire the tagname for now....
'create the new record now!
Set oDLWideRec = oDLModel2.CreateWideRecord
'need to retrieve value from the collection,
'tagname property is read only in Wide models
With oDLWideRec
.DataLogTagValues("TRENDDATA\Word1").Value =
iValue
.Marker = roMarkerSnapshot
'build the datestamp value from the integer
data
myDate = DateSerial(iYear, iMonth, iDay)
'now add the times to the date.
myDate = DateAdd("h", iHour, myDate)
myDate = DateAdd("n", iMinute, myDate)
myDate = DateAdd("s", iSecond, myDate)
.Timestamp = myDate
'store away the millisecond data from the PLC
.TimestampMilliseconds = iMillisecond
.WriteRecord
End With
iNumRecs = iNumRecs + 1
Case Else
'just ignore any blank units records
'records were padded to even increments of 10 records
End Select
End If
End With
Next
gActivity.Log "PLC timestamped data conversion complete, number of
records written are: " & iNumRecs, _
ActivityTypeConstants.roActivityWarning, _
ActivityCategoryConstants.roActivityApplication, , "VBA
Code"

Else
MsgBox "No Records found between " & dtStart & " and " & dtEnd & ",
from " & oDLModel.Name
End If

err_Handler:
'release all objects back
Set oDLNarrowRecs = Nothing
Set oDLNarrowRec = Nothing
Set oTagList = Nothing
Set oDLModel = Nothing
Set oDLModel2 = Nothing
Set oDLWideRec = Nothing
Set oDLTagValue = Nothing
'error processing begins
If Err.Number Then
gActivity.Log "Error converting PLC timestamped data: " & Err.Number &
" " & Err.Description, _
ActivityTypeConstants.roActivityError, _
ActivityCategoryConstants.roActivityApplication, , "VBA
Code"
Err.Clear

End If

End Sub

Você também pode gostar