Chương trỡnh Visual Basic

Một phần của tài liệu luận văn công nghệ thực phẩm Dây chuyền sản xuất cồn tinh bột theo công nghệ Pháp (Trang 80)

‘ Chương trỡnh chớnh Dim i310 As Long Dim i307 As Long Dim i315 As Long Dim i314 As Long Dim i304 As Long Dim i303 As Long

End Sub

Private Sub b304B_Click() PopupMenu Menub304B End Sub

Private Sub b307A_Click() PopupMenu Menub307A End Sub

Private Sub b307B_Click() PopupMenu Menub307B End Sub

Private Sub b310A_Click() PopupMenu Menub310A End Sub

Private Sub b310B_Click() PopupMenu Menub310B End Sub

Private Sub cmdAbout_Click() frmAbout.Show

End Sub

Private Sub Cmdstart_Click()

Timer1.Enabled = True Timer1.Interval = 100 Timer2.Enabled = True

frmMain. CommPortPLC.Output = "@" + "S" + "?" End Sub

Private Sub cmdMainMenu_Click()

PopupMenu mnuMain, vbPopupMenuLeftAlign, 155, 8020 End Sub

Private Sub Command5_Click() Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False Timer5.Enabled = False Timer6.Enabled = False Timer7.Enabled = False

b304A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") b304B.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") b310A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") b310B.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") b307A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") b307B.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") k303.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp") k304.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp")

mt304.Picture = LoadPicture(App.Path & "\bimap\LedOff.bmp") mt305.Picture = LoadPicture(App.Path & "\bimap\LedOff.bmp") mt307.Picture = LoadPicture(App.Path & "\bimap\LedOff.bmp") mt310.Picture = LoadPicture(App.Path & "\bimap\LedOff.bmp") S7300.Picture = LoadPicture(App.Path & "\bimap\LedOff.bmp") Label1.Caption = " "

Timer1.Enabled = False

frmMain. frmMain. CommPortPLC.Output = "@" + "s" + "?" End Sub

Private Sub Form_Load() i310 = 70 i315 = 117 i307 = 93 Timer1.Enabled = False Timer2.Enabled = False i304 = 20 i314 = 75 End Sub

Private Sub k303_Click() PopupMenu Menuk303 End Sub

Private Sub k304_Click() PopupMenu Menuk304

End Sub

Private Sub k310_Click() PopupMenu Menuk310 End Sub

Private Sub SS_Click()

If Right(SS.Caption, 4) = "Auto" Then

msg = " Run with manual control ?" Startb304A.Enabled = True Stopb304A.Enabled = True Startb304B.Enabled = True Stopb304B.Enabled = True Startb307B.Enabled = True Stopb307B.Enabled = True Startb307A.Enabled = True Stopb307A.Enabled = True Startb310B.Enabled = True Stopb310B.Enabled = True Startb310A.Enabled = True Stopb310A.Enabled = True Startk304.Enabled = True Stopk304.Enabled = True Startk303.Enabled = True Stopk303.Enabled = True

Startk310.Enabled = True Stopk310.Enabled = True Else

msg = " Run with automatic control ?" Startb304A.Enabled = False Stopb304A.Enabled = False Startb304B.Enabled = False Stopb304B.Enabled = False Startb307B.Enabled = False Stopb307B.Enabled = False Startb307A.Enabled = False Stopb307A.Enabled = False Startb310B.Enabled = False Stopb310B.Enabled = False Startb310A.Enabled = False Stopb310A.Enabled = False Startk304.Enabled = False Stopk304.Enabled = False Startk303.Enabled = False Stopk303.Enabled = False Startk305.Enabled = False Stopk305.Enabled = False Startk310.Enabled = False Stopk310.Enabled = False End If

SS.Caption = "Auto" SS.BackColor = vbBlue Auto = True Else SS.Caption = "Manual" SS.BackColor = vbTooltip Auto = False End If End If ' focus.SetFocus End Sub

Private Sub Startb304A_Click()

b304A.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb304A.Enabled = False

Stopb304A.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start"

txtcomment = "Bạn chọn bơm P304A" With comment

!comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "C" + "?" End Sub

Private Sub Startb304B_Click()

b304B.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb304B.Enabled = False

Stopb304B.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn bơm P304B " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With

Private Sub Startb307A_Click()

b307A.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb307A.Enabled = False

Stopb307A.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start"

txtcomment = "Bạn chọn bơm P307A " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "E" + "?" End Sub

Private Sub Startb307B_Click()

b307B.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb307B.Enabled = False

Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn bơm P307B " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "F" + "?" End Sub

Private Sub Startb310A_Click()

b310A.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb310A.Enabled = False

Stopb310A.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start"

!Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "G" + "?" End Sub

Private Sub Startb310B_Click()

b310B.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Startb310B.Enabled = False

Stopb310B.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn bơm P310B " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart

CommPortPLC.Output = "@" + "H" + "?" End Sub

Private Sub Startk303_Click()

k303.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") Startk303.Enabled = False

Stopk303.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn cỏnh khuấy k303 " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "I" + "?" End Sub

Private Sub Startk304_Click()

Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn cỏnh khuấy k304 " With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "J" + "?" End Sub

Private Sub Startk305_Click()

k305.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") Startk305.Enabled = False

Stopk305.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "K" + "?" End Sub

Private Sub Startk310_Click()

k310.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") Startk310.Enabled = False

Stopk310.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Start" txtcomment = "Bạn chọn cỏnh khuấy k310 " With comment .AddNew !Date = Format(Date) !Time = Format(Time)

End With

CommPortPLC.Output = "@" + "L" + "?" End Sub

Private Sub Stopb304A_Click()

b304A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") Startb304A.Enabled = True

Stopb304A.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop"

txtcomment = "Bạn chọn bơm P304A" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "c" + "?" End Sub

Startb304B.Enabled = True Stopb304B.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop" txtcomment = "Bạn chọn bơm P304B" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "d" + "?" End Sub

Private Sub Stopb307A_Click()

b307A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") Startb307A.Enabled = True

Stopb307A.Enabled = True Dim txtstart As String Dim txtcomment As String

txtcomment = "Bạn chọn bơm P307A" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "e" + "?" End Sub

Private Sub Stopb307B_Click()

b307B.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") Startb307B.Enabled = True

Stopb307B.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop"

txtcomment = "Bạn chọn bơm P307B" With comment

!comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "f" + "?" End Sub

Private Sub Stopb310A_Click()

b310A.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") Startb310A.Enabled = True

Stopb310A.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop"

txtcomment = "Bạn chọn bơm P310A" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With

Private Sub Stopb310B_Click()

b310B.Picture = LoadPicture(App.Path & "\bimap\MotoOff.bmp") Startb310B.Enabled = True

Stopb310B.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop" txtcomment = "Bạn chọn bơm P310B" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "h" + "?" End Sub

Private Sub Stopk303_Click()

k303.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp") Startk303.Enabled = True

Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop" txtcomment = "Bạn chọn Cỏnh khuấy k303" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "i" + "?" End Sub

Private Sub Stopk304_Click()

k304.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp") Startk304.Enabled = True

Stopk304.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop"

!Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "j" + "?" End Sub

Private Sub Stopk305_Click()

k305.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp") Startk305.Enabled = True

Stopk305.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop" txtcomment = "Bạn chọn Cỏnh khuấy k305" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart

CommPortPLC.Output = "@" + "k" + "?" End Sub

Private Sub Stopk310_Click()

k310.Picture = LoadPicture(App.Path & "\bimap\Motorxuongoff.bmp") Startk310.Enabled = True

Stopk310.Enabled = True Dim txtstart As String Dim txtcomment As String Dim comment As Recordset

Set comment = OpenTable(dbMain, "Activity", "true") txtstart = "Stop" txtcomment = "Bạn chọn Cỏnh khuấy k310" With comment .AddNew !Date = Format(Date) !Time = Format(Time) !comment = txtcomment !Status = txtstart .Update End With CommPortPLC.Output = "@" + "l" + "?" End Sub

Private Sub Timer1_Timer() Label1.Left = Label1.Left + 60

End Sub

Private Sub Timer2_Timer() i303 = i303 + 2

If i303 <= 30 Then r303.Caption = i303

Else: i303 = i303 - (Rnd * 10) Timer3.Enabled = True

k304.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") mt304.Picture = LoadPicture(App.Path & "\bimap\LedOn.bmp")

End If End Sub

Private Sub Timer3_Timer() i304 = i304 + 2

If i304 <= 85 Then r304.Caption = i304 Else:

i304 = i304 - (Rnd * 10)

b304B.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") b304A.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") Timer4.Enabled = True

End If End Sub

If i314 <= 120 Then J314.Caption = i314 Else: Timer5.Enabled = True i314 = i314 - (Rnd * 10) End If End Sub

Private Sub Timer5_Timer() i315 = i315 - 2

If i315 >= 95 Then r315.Caption = i315 Else:

k305.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") mt305.Picture = LoadPicture(App.Path & "\bimap\LedOn.bmp")

i315 = i315 + (Rnd * 10) Timer6.Enabled = True

mt307.Picture = LoadPicture(App.Path & "\bimap\LedOn.bmp") End If

End Sub

Private Sub Timer6_Timer() i307 = i307 - 2

If i307 >= 73 Then r307.Caption = i307 Else:

k310.Picture = LoadPicture(App.Path & "\bimap\MotorxuongOn.bmp") mt310.Picture = LoadPicture(App.Path & "\bimap\LedOn.bmp")

Timer7.Enabled = True End If

End Sub

Private Sub Timer7_Timer() i310 = i310 - 2

If i310 >= 30 Then r310.Caption = i310 Else:

i310 = i310 + (Rnd * 10)

b310A.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") b310B.Picture = LoadPicture(App.Path & "\bimap\MotoOn.bmp") End If

End Sub

Private Sub TimerDateTime_Timer()

lblDateTime.Caption = Format(Date, "Long Date") + " " + Format(Time, "Long time")

End Sub

Cỏc Module AppVarible Module AppVarible

Long, ByVal uReturnCode As Long) As Long

Public fStart As frmMain Public temp As Date

' Parameter of Application Public dbMain As String Public nSHD As Long

' Parameter of Communication Global nCom As Byte

Global nPLC As Byte Global portOK As Boolean Global ScanPort As Boolean Global NoStatusUnit As Boolean Public Auto As Boolean

Public Run As Boolean Public FreeTime As Integer Public TimeScanPort

Public TimeScanPLC

Global nowStatus As String Global nowStatus2 As String Global nowStatus3 As String Global OutputPLC As String ' Wait Response

Public Const WhileTime = 0.25 Public Const CheckTime = 0.5

Public Const PLC_DataLen = 64 ' Shut down windows

Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long)

Public Const EWX_FORCE = 4 Public Const EWX_LOGOFF = 0 Public Const EWX_REBOOT = 2 Public Const EWX_SHUTDOWN = 1

Public Function strByte(S As String) As String Dim ASCI As String

Dim n As Byte Dim num As Byte Dim i As Byte ASCI = "" num = 0 i = Len(S) n = 1 Do While i > 0

num = num + n * Val(Mid(S, i, 1)) If n < 128 Then n = n * 2

i = i - 1

If i Mod 8 = 0 Then

ASCI = Chr(num) + ASCI n = 1

Loop

strByte = ASCI End Function

Public Function strBinary(strByte As String, n As Byte) As String Dim i As Byte

Dim ac As Byte

Dim valueBIN As String Dim oneByte As String valueBIN = ""

For i = 1 To Len(strByte) ac = Asc(Mid(strByte, i, 1)) oneByte = ""

Do While ac > 0

oneByte = oneByte + Trim(Str(ac Mod 2)) ac = Int(ac / 2)

Loop

Do While Len(oneByte) < 8 oneByte = oneByte + "0" Loop

valueBIN = valueBIN + oneByte Next i

strBinary = valueBIN End Function

Dim strSQL As String

strSQL = "Select * From " & NameTable & " Where " & Logic Set OpenTable = OpenDb.OpenRecordset(strSQL, dbOpenDynaset) End Function

Public Sub CheckStatusUnit() Dim i, K, l

Dim StartTime

Dim strSend As String Dim strRes As String Dim Unit As String Dim iFcs As Integer Dim clearPort As String NoStatusUnit = True

frmMain.TimerPLC.Enabled = False If portOK = True Then

For i = 1 To maxClock If frmMain.comPortRUN.InBufferCount >= 0 Then clearPort = frmMain.comPortRUN.Input NoStatusUnit = False End If Next i Else

End If

If NoStatusUnit Then

MsgBox "Not response device !", vbOKOnly, "Message" Exit Sub

End If End Sub

Public Sub UseCom(Com As Byte, ComPort As MSComm, setCom As String) On Error GoTo ErrorLine

If ComPort.PortOpen = True Then ComPort.PortOpen = False With ComPort .CommPort = Com .Settings = setCom .InputLen = 0 .InputMode = comInputModeText .DTREnable = True .RTSEnable = True .PortOpen = True End With portOK = True ScanPort = True Exit Sub ErrorLine: Dim msg

End Sub

‘ Chương trỡnh kết nối PLC với mỏy tớnh

Private useTime As Recordset Private useComNo As Recordset

Private Sub cmdCancel_Click() useTime.Close

Unload Me End Sub

Private Sub cmdok_Click() Dim oldCom

oldCom = nCom

If OptCom(0).Value = True Then nowCom = 1

End If

If OptCom(1).Value = True Then nowCom = 2

Set useComNo = OpenTable(dbMain, "ComPort", "True") With useTime .MoveFirst .Edit !timeScan = Text7.Text TimeScanPort = !timeScan * 100 .Update .Close End With

If nCom <> oldCom Then With useComNo .MoveFirst .Edit !UsePort = nCom .Update .Close End With

UseCom nCom, frmMain.comPortRUN, "9600,e,7,2" nPLC = nCom Mod 2 + 1

UseCom nPLC, frmMain.CommPortPLC, "9600,n,8,1" End If

Unload Me End Sub

If nowCom = 1 Then OptCom(0).Value = True If nowCom = 2 Then OptCom(1).Value = True

Set useTime = OpenTable(dbMain, "TimeComm", "True") With useTime

.MoveFirst

Text7 = !timeScan End With

Một phần của tài liệu luận văn công nghệ thực phẩm Dây chuyền sản xuất cồn tinh bột theo công nghệ Pháp (Trang 80)