Option Explicit Dim WaktuLapHarian, T_off, AppStarTime, TimeGraph(1 To 25) As Date Dim WaktuLapHarianLalu, JamStanPerJam As Date Dim StanMeter(6), StanPLC(2, 6), StanDebit(5) As Double Dim StanHari(6), StanPerJam(6), SkalaStan(5) As Double Dim Debit(6) As Double Dim Press, Volt, Vmax, Vups As Double Dim LastSeconJob Dim VirC(10) As Double Dim DataGraph(1 To 5, 1 To 25) As Double Dim intFile Private Sub Form_Load() Dim Outport$ WaktuLapHarian = "08:00:00" T_off = 0 Press = 0 Volt = 0 AppStarTime = 0 'set skala 1 stan = xxx liter SkalaStan(0) = 200 SkalaStan(1) = 200 SkalaStan(2) = 1000 SkalaStan(3) = 1000 SkalaStan(4) = 1000 SkalaStan(5) = 1000 'tampilkan kapan program mulai aktif Label3 = "Mulai aktif : " & Format(Now, "dd mmm yyyy hh:mm:ss") 'baca setting port com terakhir On Error GoTo Errorhandler Open App.Path & "\Port.ini" For Input As #1 Input #1, Outport$ Close #1 MSComm1.CommPort = Outport$ 'baca data i/o terakhir dan update ke variabel GetLastLogIO 'start retrieving plc data ReadPLC.Enabled = True Exit Sub Errorhandler: If Err = 55 Or Err = 53 Then Open App.Path & "\Port.Ini" For Output As #1 Print #1, "1" Close #1 Outport$ = "1" MSComm1.CommPort = "1" End If MsgBox ("Form Load Error Code : " & CStr(Err)) Unload Me End Sub Private Sub Data_Click() 'FormData.Show 'Shell "C:\Program Files (x86)\Mozilla Firefox\firefox.exe file:///D:/SCADA/Visual/basic/Bak%20Kuala/index.html", vbNormalFocus Shell "C:\Program Files (x86)\Mozilla Firefox\firefox.exe file:///" & App.Path & "/index.html", vbNormalFocus End Sub Private Sub Quit_Click() Dim objForm As Form If (Second(Now) < 5) Then Exit Sub End If ReadPLC.Enabled = False If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If 'unload all forms except this one For Each objForm In Forms If objForm.hWnd <> Me.hWnd Then 'only the hWnd property is guaranteed to be unique Unload objForm Set objForm = Nothing End If Next objForm 'unload this form Unload Me End Sub Private Function Baca(ComData As String, Cari As String) As String Dim awal, panjang If InStr(1, ComData, Cari & "-") > 0 Then awal = InStr(1, ComData, Cari & "-") + Len(Cari & "-") panjang = InStr(awal, ComData, ";") - awal Baca = Mid(ComData, awal, panjang) Else: Baca = -1 End If End Function Private Sub ComSet_Click() Dim Settings$, Port$ Dim OldSettings$ OldSettings$ = MSComm1.Settings On Error GoTo Errorhandler Settings$ = InputBox("Enter New Settings", , MSComm1.Settings) MSComm1.Settings = Settings$ Port$ = InputBox("Enter Serial Port Number", , MSComm1.CommPort) MSComm1.CommPort = Port$ Open App.Path & "\Port.ini" For Output As #1 Print #1, Port$ Close #1 Exit Sub Errorhandler: If Err = 380 Then MsgBox ("Invalid Value") MSComm1.Settings = OldSettings Resume Next Else MsgBox (Error) Unload Me End If End Sub Private Function ComClear(sData As String) As String Dim scan As Integer Dim sHasil As String For scan = 1 To Len(sData) Select Case Mid(sData, scan, 1) Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _ "A", "C", "D", "E", "N", "T", ";", "-" sHasil = sHasil & Mid(sData, scan, 1) End Select Next ComClear = sHasil End Function Function VirtualCom() As String Dim ulang Dim instring For ulang = 0 To 6 Randomize instring = instring & "A" & ulang & "-" & Format((300 + (Rnd * 500)), "#") & ";" Next For ulang = 0 To 10 Randomize VirC(ulang) = VirC(ulang) + (Rnd * 2) + 1 instring = instring & "C" & ulang & "-" & Format(VirC(ulang), "#") & ";" Next VirtualCom = instring End Function Private Sub ReadPLC_Timer() Dim started, Done As Boolean Dim ionr, Zero4mA, Max20mA As Integer Dim instring$ Dim Lama As Date On Error GoTo Errorhandler started = False instring$ = "" Done = False Lama = Now() GoTo Virtual If MSComm1.PortOpen = False Then MSComm1.PortOpen = True End If Do While Not Done Do While MSComm1.InBufferCount > 0 started = True instring$ = Trim(instring$ & ComClear(MSComm1.Input)) Loop If InStr(instring$, "A0-") <> 1 Then instring$ = Right(instring$, Len(instring$) - InStr(instring$, "A0-") + 1) End If If MSComm1.InBufferCount = 0 And started = True And _ InStr(1, instring$, ";EN") > 0 And _ InStr(1, instring$, "A0-") = 1 Then Done = True End If 'DoEvents If (Now() - Lama) > (1 / 86400) Then Exit Sub End If Loop Virtual: instring$ = VirtualCom Text5 = instring$ & " - " & Len(instring$) ' Analog section Zero4mA = 195 Max20mA = 973 'Debit(0) = CDbl((Baca(InString$, "A0") - Zero4mA) / (Max20mA - Zero4mA)) * 1000 'Debit(1) = CDbl((Baca(InString$, "A1") - Zero4mA) / (Max20mA - Zero4mA - 45)) * 1000 Press = ((CDbl(Baca(instring$, "A0")) - Zero4mA) / (Max20mA - Zero4mA - 0)) * 15 Volt = (CDbl(Baca(instring$, "A1")) / 1023) * 220 * 7 Vups = (CDbl(Baca(instring$, "A2")) / 1023) * 50 'Digital Section For ionr = 0 To 5 StanPLC(0, ionr) = CDbl(Baca(instring$, "C" & CStr(ionr + 2))) Next StanPLC(0, 4) = CDbl(Baca(instring$, "C" & CStr(2 + 2))) If JobScheduler.Enabled = False Then JobScheduler.Enabled = True End If Exit Sub Errorhandler: Select Case Err Case Err = 8005 MsgBox ("Port Already Open - Change Settings and Select a Different Port Number") ComSet_Click Unload Me Case Err = 8002 MsgBox ("Invalid Port Number - Change Settings and Select a Different Port Number") ComSet_Click Unload Me Case Else MsgBox ("RPLC Error Code : " & CStr(Err)) Exit Sub 'Unload Me End Select End Sub Private Sub JobPerDetik() Dim ionr, scan, UpdaTime As Integer Debit(6) = 0 StanHari(6) = 0 AppStarTime = AppStarTime + 1 'tampilkan Jam sekarang LabelJam = Format(Now(), "dddd, dd mmm yyyy hh:mm:ss") 'tampilkan lama program telah berjalan Label4 = "Selama : " & Format((AppStarTime \ 86400), "0") & " hari, " & _ Format((AppStarTime \ 3600) Mod 24, "0#") & " jam, " & _ Format((AppStarTime \ 60) Mod 60, "0#") & " mnt, " & _ Format(AppStarTime Mod 60, "0#") & " detik" 'update stand meter Q1-2 = kalkulasi dari liter/detik 'For ionr = 0 To 1 ' If Debit(ionr) >= 0 Then ' StanMeter(ionr) = StanMeter(ionr) + (Debit(ionr) / SkalaStan(ionr)) ' End If 'Next 'update stand meter Q3-6 = kalkulasi dari selisih stanPLC For ionr = 0 To 5 StanMeter(ionr) = ((StanPLC(0, ionr) - StanPLC(1, ionr)) * SkalaStan(ionr) / 1000) + StanMeter(ionr) StanPLC(1, ionr) = StanPLC(0, ionr) Next 'init StanDebit saat App Baru Run If AppStarTime <= 1 Then For ionr = 0 To 5 StanDebit(ionr) = StanMeter(ionr) Next End If 'Update Stan to Debit UpdaTime = 3 'menit 'If ((second(Now) Mod UpdaTime) = 0) Then ' pembagi detik If ((Minute(Now) Mod UpdaTime) = 0) And (Second(Now) = 0) Then For ionr = 0 To 5 'Debit(ionr) = ((StanMeter(ionr) - StanDebit(ionr)) * 1000 / UpdaTime) 'pembagi = mod detik Debit(ionr) = ((StanMeter(ionr) - StanDebit(ionr)) * 1000 / (UpdaTime * 60)) 'pembagi = mod menit StanDebit(ionr) = StanMeter(ionr) Next End If For ionr = 0 To 5 'tampilkan data stand meter form Text1(ionr) = Format(StanMeter(ionr), "###.0") 'tampilkan data debit perdetik Text2(ionr) = Format(Debit(ionr), "#.0") Debit(6) = Debit(6) + Debit(ionr) 'tampilkan data debit harian Text6(ionr) = Format((StanMeter(ionr) - StanHari(ionr)), "#.0") StanHari(6) = StanHari(6) + (StanMeter(ionr) - StanHari(ionr)) Next 'tampilkan total debit dan kubikasi Text2(6) = Format(Debit(6), "#.0") Text6(6) = Format(StanHari(6), "#.0") 'tampilkan data voltase Text3(0) = Format(Volt, "#.0") If Vmax < Volt Then Vmax = Volt End If Text3(1) = Format(Vmax, "#.0") Text3(4) = Format(Vups, "#.0") Text3(5) = Format((100 * (Vups - 20) / ((13.8 * 2) - 20)), "0#.0") If Volt < 180 Then T_off = T_off + 1 Text3(2) = Format(Now, "dd/mm/yy hh:mm:ss") Text3(3) = Format((T_off \ 3600) Mod 24, "0#") & "h " & _ Format((T_off \ 60) Mod 60, "00") & "m " & Format(T_off Mod 60, "00") & "s " End If 'tampilkan data press Text4 = Format(Press, "#.00") End Sub Private Sub Text1_DblClick(Index As Integer) Dim sInStand As String If (Second(Now) < 50) And (Second(Now) > 2) Then sInStand = InputBox("Masukkan stand meter baru", "Ubah Stand Meter", StanMeter(Index)) If sInStand <> "" Then StanMeter(Index) = CDbl(sInStand) End If End If End Sub Private Sub LimitLogIO() Dim ionr Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Set conn = New ADODB.Connection conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\log\IOLog.mdb;Persist Security Info=False" Set rs = New ADODB.Recordset rs.ActiveConnection = conn rs.CursorLocation = adUseClient rs.CursorType = adOpenDynamic rs.LockType = adLockOptimistic rs.Source = "SELECT * FROM Log" rs.Open 'bersihkan data lebih lama dari 1 bulan 2 minggu rs.MoveFirst While (Not rs.EOF) 'On Error Resume Next If rs("Time") < (Now - 40) Then rs.Delete Else rs.MoveLast End If rs.MoveNext Wend 'rs.Update rs.Close conn.Close Set conn = Nothing Set rs = Nothing End Sub Private Sub SaveLogIO() Dim ionr Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Set conn = New ADODB.Connection conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\log\IOLog.mdb;Persist Security Info=False" Set rs = New ADODB.Recordset rs.ActiveConnection = conn rs.CursorLocation = adUseClient rs.CursorType = adOpenDynamic rs.LockType = adLockOptimistic rs.Source = "SELECT * FROM Log" rs.Open rs.AddNew rs("Time") = Now rs("A0") = Press rs("A1") = Volt rs("A2") = Vups rs("A4") = Debit(6) ' simpan debit total For ionr = 0 To 5 rs("C" & CStr(ionr)) = StanMeter(ionr) Next rs.Update rs.Close conn.Close Set conn = Nothing Set rs = Nothing End Sub Private Sub GetLastLogIO() Dim ionr Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim J1, J2 As Date Set conn = New ADODB.Connection conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\log\IOLog.mdb;Persist Security Info=False" Set rs = New ADODB.Recordset rs.ActiveConnection = conn rs.CursorLocation = adUseClient rs.CursorType = adOpenDynamic rs.LockType = adLockOptimistic rs.Source = "SELECT * FROM Log" rs.Open rs.MoveLast 'ambil data terakhir tersimpan tuk stan meter For ionr = 0 To 5 StanMeter(ionr) = rs("C" & CStr(ionr)) 'mana tau data belum ada sama sekali, init semua var StanHari(ionr) = rs("C" & CStr(ionr)) StanPerJam(ionr) = rs("C" & CStr(ionr)) Next 'tentukan waktu data stanharian yang lalu If TimeValue(Now) > TimeValue(WaktuLapHarian) Then 'jika hari belum berganti WaktuLapHarianLalu = DateValue(Now) + TimeValue(WaktuLapHarian) Else 'hari telah berganti WaktuLapHarianLalu = DateValue(Now) - 1 + TimeValue(WaktuLapHarian) End If 'mengambil data stand perjam JamStanPerJam = Now() - (((Minute(Now) * 60 + Second(Now))) / 86400) While (Not rs.BOF) And (rs("TIME") >= WaktuLapHarianLalu) For ionr = 0 To 5 StanHari(ionr) = rs("C" & CStr(ionr)) J2 = rs("TIME") 'data stanperjam terakhir If rs("TIME") >= JamStanPerJam Then StanPerJam(ionr) = rs("C" & CStr(ionr)) J1 = rs("TIME") End If Next rs.MovePrevious Wend JamStanPerJam = J1 WaktuLapHarianLalu = J2 'mengambil data perjam grafik J1 = Now() J1 = J1 - (((Minute(J1) * 60) + Second(J1) + 1) / 86400) ionr = 24 rs.MoveLast While (Not rs.BOF) And (ionr >= 1) If rs("TIME") >= J1 Then TimeGraph(ionr) = rs("TIME") DataGraph(2, ionr) = rs("A0") 'press DataGraph(3, ionr) = rs("A1") 'volt If Not IsNull(rs("A4")) Then DataGraph(1, ionr) = rs("A4") 'debit total Else DataGraph(1, ionr) = 0 'no valid data End If Else J1 = J1 - (3600 / 86400) 'mundur 1 jam ionr = ionr - 1 End If rs.MovePrevious Wend rs.Close conn.Close Set conn = Nothing Set rs = Nothing End Sub Private Sub JobScheduler_Timer() Dim ionr, scan As Integer 'untuk Job perdetik If LastSeconJob <> Second(Now) Then LastSeconJob = Second(Now) JobPerDetik 'Job per menit If Second(Now) = 0 Then 'buat laporan per 2 menit biar gak konflik di dropbox If Minute(Now) Mod 2 = 0 Then LapIO ("\data\IO") Laporan ("\data\new") End If 'graphical chart per 10 menit If Minute(Now) Mod 20 = 0 Then GraphHTML End If 'job per jam If Minute(Now) = 0 Then If TimeValue(Now) = TimeValue(WaktuLapHarian) Then 'Laporan Harian Laporan ("\data\" & Format(Now, "mm") & "\" & Format(Now, "yymmdd-hh") & "-H") 'update iolog.mdb FileCopy App.Path & "\log\IOLog.mdb", App.Path & "\data\IOLog.mdb" 'backup ke direktori bulan, sekali sehari FileCopy App.Path & "\log\IOLog.mdb", App.Path & "\data\" & Format(Now, "mm") & "\IOLog" & Format(Now, "yymm") & ".mdb" 'reset stan harian For ionr = 0 To 5 StanHari(ionr) = StanMeter(ionr) Next T_off = 0 'reset T_off counter WaktuLapHarianLalu = Now() Else 'Laporan per-jam Laporan ("\data\" & Format(Now, "mm") & "\" & Format(Now, "yymmdd-hh")) End If 'laporan harian 'geser log data graphis For scan = 1 To 24 For ionr = 1 To 5 DataGraph(ionr, scan) = DataGraph(ionr, scan + 1) Next TimeGraph(scan) = TimeGraph(scan + 1) Next 'reset stan per-jam For ionr = 0 To 5 StanPerJam(ionr) = StanMeter(ionr) Next JamStanPerJam = Now() 'update iolog.mdb @ data folder @ specific time If Hour(Now) = 12 Then FileCopy App.Path & "\log\IOLog.mdb", App.Path & "\data\IOLog.mdb" End If End If 'job per-jam SaveLogIO LimitLogIO End If 'job permenit End If 'job perdetik End Sub Private Sub Laporan(sFile As String) Dim ionr As Integer Dim strFile As String Dim Pembagi As Double strFile = App.Path & sFile & ".txt" intFile = FreeFile Open strFile For Output As #intFile Print #intFile, "DATA STATUS JALUR DISTRIBUSI" Print #intFile, "BAK KUALA - IPA SIBOLANGIT" Print #intFile, "" Print #intFile, "KONDISI TERKINI" Print #intFile, Format(Now, "dd mmmm yyyy - hh:mm:ss") Print #intFile, "====================================" Print #intFile, "Jalur"; Tab(8); "Debit"; Tab(18); "Stand Meter" Print #intFile, "====================================" For ionr = 0 To 5 Print #intFile, "Q" & (ionr + 1); Tab(8); _ Format(Debit(ionr), "0###.0"); Tab(18); Format(StanMeter(ionr), "0#########.0 ") Next Print #intFile, "====================================" Print #intFile, "TOTAL"; Tab(8); Format(Debit(6), "0###.0"); " liter/detik" Print #intFile, "PRESS"; Tab(8); Format(Press, "0#.00") & " kg/cm2" Print #intFile, "====================================" Print #intFile, "V pln : "; Format(Volt, "0##.0"); Tab(18); "V max : "; Format(Vmax, "0##.0") Print #intFile, "V ups : "; Format(Vups, "0##.0"); Tab(18); "PLN off : "; _ Format((T_off \ 3600) Mod 24, "0#") & "h" & _ Format((T_off \ 60) Mod 60, "00") & "m" & Format(T_off Mod 60, "00") & "s" Print #intFile, "====================================" Print #intFile, "" Print #intFile, "ANALISIS DATA PERJAM" Print #intFile, "Mulai " & Format(JamStanPerJam, "dd/mm/yyyy - hh:mm:ss") Print #intFile, "=======================================" Print #intFile, "Jalur"; Tab(8); "Debit"; Tab(18); "Kubikasi"; Tab(28); "Stand Awal" Print #intFile, "=======================================" Pembagi = (Now() - JamStanPerJam) * 86400 StanPerJam(6) = 0 ' nilai debit For ionr = 0 To 5 Print #intFile, "Q" & (ionr + 1); Tab(8); _ Format((StanMeter(ionr) - StanPerJam(ionr)) * 1000 / Pembagi, "0###.0"); _ Tab(18); Format((StanMeter(ionr) - StanPerJam(ionr)), "0#####.0 "); _ Tab(28); Format(StanPerJam(ionr), "0#########.0 ") StanPerJam(6) = StanPerJam(6) + ((StanMeter(ionr) - StanPerJam(ionr))) Next Print #intFile, "=======================================" Print #intFile, "TOTAL"; Tab(8); Format(StanPerJam(6) * 1000 / Pembagi, "0###.0"); _ Tab(18); Format(StanPerJam(6), "0#####.0 ") Print #intFile, Tab(8); "(l/d)"; Tab(18); "( m3 )" Print #intFile, "=======================================" 'isi data grafik TimeGraph(25) = Now() If Minute(Now) = 0 Then 'simpan data debit total perjam. debit perjam juga akan terecord pd iolog Debit(6) = StanPerJam(6) * 1000 / Pembagi End If DataGraph(1, 25) = Debit(6) DataGraph(2, 25) = Press DataGraph(3, 25) = Volt Print #intFile, "" Print #intFile, "ANALISIS DATA HARIAN" Print #intFile, "Mulai " & Format(WaktuLapHarianLalu, "dd mmm yyyy - hh:mm:ss") Print #intFile, "=======================================================" Print #intFile, "Jalur"; Tab(8); "Debit"; Tab(18); "Kubikasi"; _ Tab(28); "Stand Awal"; Tab(43); "Stand Akhir" Print #intFile, "=======================================================" StanHari(6) = 0 Pembagi = (Now() - WaktuLapHarianLalu) * 86400 For ionr = 0 To 5 Print #intFile, "Q" & (ionr + 1); Tab(8); _ Format((StanMeter(ionr) - StanHari(ionr)) * 1000 / Pembagi, "0###.0"); _ Tab(18); Format((StanMeter(ionr) - StanHari(ionr)), "0#####.0 "); _ Tab(28); Format(StanHari(ionr), "0##########.0 "); _ Tab(43); Format(StanMeter(ionr), "0##########.0 ") StanHari(6) = StanHari(6) + StanMeter(ionr) - StanHari(ionr) Next Print #intFile, "=======================================================" Print #intFile, "Total"; Tab(8); Format(StanHari(6) * 1000 / Pembagi, "0###.0"); _ Tab(18); Format(StanHari(6), "0#####.0 ") Print #intFile, Tab(8); "(l/d)"; Tab(18); "( m3 )" Print #intFile, "===================================================JFS=" Print #intFile, "" Print #intFile, "#### GRAFIK TOTAL DEBIT (l/s) ####" pGraph (1) 'Q-Total Print #intFile, "#### GRAFIK TEKANAN (kg/cm2) ####" pGraph (2) 'Press Print #intFile, "#### GRAFIK VOLTASE PLN (Volt)####" pGraph (3) 'PLN Close #intFile End Sub Private Function pGraph(io As Integer) Dim scan, row, col As Integer Dim min, max As Double Dim RowStr As String 'cari max dan min min = 100000 max = -10000 For scan = 1 To 25 If DataGraph(io, scan) < min Then min = DataGraph(io, scan) End If If DataGraph(io, scan) > max Then max = DataGraph(io, scan) End If Next For row = 1 To 25 RowStr = Format(TimeGraph(row), "hh:mm ") For col = 0 To 20 If DataGraph(io, row) >= (min + ((max - min) * col / 20)) Then RowStr = RowStr & "=" Else If col Mod 5 = 0 Then RowStr = RowStr & ":" Else RowStr = RowStr & " " End If End If Next col Print #intFile, RowStr; Format(DataGraph(io, row), " 0###.0") Next row Print #intFile, Tab(4); "Min|"; Format(min, "0###.0"); Tab(21); _ Format(max, "0###.0"); "|Max" Print #intFile, "" End Function Private Sub LapIO(sFile As String) Dim intFile, ionr As Integer Dim strFile, sIO As String Dim Pembagi As Double strFile = App.Path & sFile & ".txt" intFile = FreeFile Open strFile For Output As #intFile sIO = "TIME-" & Format(Now, "dd/mm/yyyy+hh:mm:ss") & ";" For ionr = 0 To 5 sIO = sIO & "Q" & (ionr + 1) & "-" & Format(Debit(ionr), "0###.0") & "+" & _ Format(StanMeter(ionr), "0#########.0") & ";" Next sIO = sIO & "QTOT-" & Format(Debit(6), "0###.0") & ";" sIO = sIO & "PRES-" & Format(Press, "0#.00") & ";" sIO = sIO & "VPLN-" & Format(Volt, "0##.0") & ";" & _ "VMAX-" & Format(Vmax, "0##.0") & ";" & _ "VUPS-" & Format(Vups, "0##.0") & ";" & _ "TLOF-" & Format((T_off \ 3600) Mod 24, "0#") & "h" & _ Format((T_off \ 60) Mod 60, "00") & "m" & _ Format(T_off Mod 60, "00") & "s;EN" Print #intFile, sIO Close #intFile End Sub Private Sub GraphHTML() Dim row As Integer Dim strFile As String strFile = App.Path & "\data\chart.html" intFile = FreeFile Open strFile For Output As #intFile Print #intFile, "" Print #intFile, "" Print #intFile, "" Print #intFile, "" Print #intFile, "" Print #intFile, "" Print #intFile, "
" Print #intFile, "" Print #intFile, "" Close #intFile End Sub