Mình sẽ nói sơ qua về cách lấy alarm history sử dụng connectivity pack
Mình sẽ lấy ví dụ sư sau. Ví dụ bạn muốn truy suất alarm từ ngày nào tới ngày nào đó và vị trí xảy ra lỗi như hình sau
'2. Connecting WinCC-Database
'////////////////////////////////////////////////////
Dim Pro 'Provider
Dim DSN 'Data Source Name
Dim DS 'Data Source
Dim ConnString 'Connection String
Dim MachineNameRT 'Name of the PC from WinCC-RT
Dim DSNRT 'Data Source Nane from WinCC-RT
Dim Conn 'Connection to ADODB
Dim RecSet 'RecordSet
Dim Command 'Query
Dim CommandText 'Command-Text
Dim sqlSec 'Seconds for SQL
Dim sqlMin 'Minutes for SQL
Dim sqlHour 'Hours for SQL
Dim sqlDay 'Day for SQL
Dim sqlMonth 'Month for SQL
Dim sqlYear 'Year for SQL
Dim CurrLanguage'Current Language
'Attention: Tag-Archiving is based on UTC, that means the timestamp of a Tag is in UTC !
'Read the name of the PC-Station and the DSN-Name from WinCC-RT
Set MachineNameRT = HMIRuntime.Tags("@LocalMachineName")
Set DSNRT = HMIRuntime.Tags("@DatasourceNameRT")
'Preparing the Connection-String
Pro="Provider=WinCCOLEDBProvider.1;" 'First instance of WinCCOLEDB
DSN="Catalog=" & DSNRT.Read & ";" 'Name of Runtime-Database
DS= "Data Source=" & MachineNameRT.Read & "\WinCC" 'Data Source
'Build the complete String:
ConnString = Pro + DSN + DS
'Make Connection
Set Conn = CreateObject("ADODB.Connection")
Conn.ConnectionString = ConnString
Conn.CursorLocation = 3
Conn.open
'Preparing query
'Format needed for StartTime: '2009-01-20 13:26:45'"
'Date and time 24 hours before
StartTime=DateAdd("h",-24,StartTime)
'Split in Years, months, days, hours, min.,seconds
sqlSec=second (StartTime)
sqlMin=Minute (StartTime)
sqlHour=Hour (StartTime)
sqlDay=Day (StartTime)
sqlMonth=Month (StartTime)
sqlYear=Year (StartTime)
'Creating leading zeros
sqlSec=Right("00" & sqlSec,2)
sqlMin=Right("00" & sqlMin,2)
sqlHour=Right("00" & sqlHour,2)
sqlDay=Right("00" & sqlDay,2)
sqlMonth=Right("00" & sqlMonth,2)
'Formating Starttime fpr SQL-Statement:
StartTime="'" & sqlYear & "-" & sqlMonth & "-" & sqlDay & " " _
& sqlHour & ":" & sqlMin & ":" & sqlSec & "'"
'MsgBox "Jahr: " & sqlYear & vbcrlf & "Monat: " & sqlMonth & vbcrlf & "Tage: " & sqlDay & vbcrlf & _
'"Stunden: " & sqlHour & vbcrlf & "Minuten: " & sqlMin & vbcrlf & _
'"Sekunden: " & sqlSec 'MBox for Diagnosis only
'Building the complete String:
Set CurrLanguage = HMIRuntime.Tags("@CurrentLanguage")
Select Case CurrLanguage.Read
Case 1031 'German
CommandText= "ALARMVIEW:Select * FROM AlgViewDeu WHERE DateTime>" & ngaybatdau & "AND DateTime<" & ngaybatdau " AND text2="&khuxayraloi&""
Case 1033 'English
CommandText= "ALARMVIEW:Select * FROM AlgViewEnu WHERE DateTime>" & ngaybatdau & "AND DateTime<" & ngaybatdau " AND text2="&khuxayraloi&""
End Select
'MsgBox "Open Connection with: " & CommandText 'MBox for Diagnosis only
'Create the recordset, read the records and set to first recordset:
Set RecSet = CreateObject("ADODB.Recordset")
Set Command = CreateObject("ADODB.Command")
Command.CommandType = 1
Set Command.ActiveConnection = Conn
Command.CommandText=CommandText
Set RecSet = Command.Execute
RecSet.MoveFirst
'write recordsets to CSV-File
Select Case CurrLanguage.Read
Case 1031 'German
ts.WriteLine ("Datum/Zeit;Meldenr.;Ereignis;Klasse")
Case 1033 'Englisch
ts.WriteLine ("Date/Time;Message;point of error")
End Select
Do while Not RecSet.EOF
ts.WriteLine (RecSet.Fields(2).Value & ";" & RecSet.Fields(37).Value & ";" & _
RecSet.Fields(38).Value &)
RecSet.MoveNext
Loop
' Please don't forget this............. !!!
ts.close
RecSet.Close
Set Recset=Nothing
Set Command = Nothing
conn.close 'Close connection
Set Conn = Nothing
Set fso = Nothing
Set f = Nothing
Set ts = Nothing
End Sub
trong đó giá trị ngày bắt đầu, ngày kết thúc, vị trí xảy ra lỗi lấy từ màn hinh xuống. Còn Field datetime là 2, field message la 37, field point of error là 38. còn một field nữa là Duration mình tìm không thấy nếu ai đã làm rồi chỉ mình lấy Duration này với.
các bạn chú ý nhé có thể code mình có chỗ nào bị sai các bạn chỉnh lại Do cái mình làm cũng chưa hoàn chỉnh.
Hieplangmoi@gmail.com