Upload
khien-do
View
446
Download
7
Embed Size (px)
Citation preview
KẾT NỐI BỘ ĐIỀU KHIỂN VÀ MÁY TÍNH
THỰC HIỆN CHƯƠNG TRÌNH VISUAL BASIC ĐIỀU KHIỂN HỆ THỐNG ĐÈN GIAO THÔNG
Với bộ điều khiển là PLC S7-200 siemens, phần mềm lập trình Microwin 4.0, bộ OPC Access sẽ truyền dữ liệu giữa PLC và máy tính . Thông qua OPC, chúng ta có thể kết nối các phần mềm ứng dụng khác như Matlab, Visual Basic ….. Với ngôn ngữ lập trình đơn giản và tốc độ xử lý nhanh . OPC VB và Visual Basic sẽ được chọn thiết kế bộ điều khiển và máy tính trong đề tài này . Để thực hiện được đề tài bằng phương pháp này ta cần sử dụng thêm hai phần mềm là OPC VB, PC Access không kể Visual Basic , Microwin 4.0 Cách kết nối OPC VB và PC Access Bước 1 : Cài đặt OPC VB
Nhấn vào file Setup OPC VB . Ta cài đặt bộ OPC của Vb vào máy tính điều khiển .
Bước 2 : Khởi tạo biến PC Access
1. Mở giao diện V1.0 PC Access Sp1 2. Kích chuột phải chọn New PLC đặt tên là PLC1
Đặt tên các biến theo địa chỉ ô nhớ trong chương trình PLC
Chọn Save để lưu các biến PC Access vào máy tính
Bước 3 : Liên kết PC Access và Visual Basic 6.0
Mở giao diện OPC for Visual Basic
Chọn Edit > Add Item Chọn liên kết S7200.OPCSever-Microwin-PLC Chọn Add tất cả các biến có trong OPC PC Access vào list Opc Item .
Chọn OK
Bước 4 : Viết chương trình và tạo giao diện trên Visual Basic
Mở giao diện Visual Basic Chọn Project – Components Chọn click vào OPC Control
Lúc này trên VB sẽ xuất hiện biểu tượng Opc Control trên thanh công cụ .
Kích chọn vẽ OPC vào vùng giao diện Ta đặt tên Name : FastOPCControl Vào phần Properties – ConfigPath . Ta ghi đường dẫn flie.csv đã lưu ở
trên .
Bước 5 : Làm việc với FastOPCControll
Lấy dữ liệu từ một Item của OPC
LabelNORMAL.Caption= FastOPCControll.GetValue(‘S7200.OPCSever\
MicroWin.PLC.START)
Viết dữ liệu vào một Item của OPC
FastOPCControll.WriteValue 0,” S7200.OPCSever\MicroWin.PLC…..”
Vậy là ta đã hoàn thành bước đầu kết nối PLC và máy tính phần còn lại là thao tác trên VB
Sau đây là Code chương trình VB và giao diện đạt được
Giao diện VB
Code chương trình VB
Option Explicit
Option Base 1 ' All OPC Automation Arrays start with 1
Private MyOPCServer As OPCServer ' OPCServer Object
Private MyGroups As OPCGroups ' OPCGroups Collection Object
Private WithEvents MyGroup As OPCGroup ' OPCGroup Object
Private MyItems As OPCItems ' OPCItems Collection Object
Private MyItemServerHandles() As Long ' Server Handles for Items
Private CHON As Long
Private muclogic As String
Dim MyTID As Long ' Transaction ID for asynchronous calls
''''''''
Dim Group_name As String
'<><><><><><><><><><><><><><><><><>
Private db As ADODB.Connection
Private Sub Cmd_Exit_Click()
Unload Me
End Sub
Private Sub Cmd_RemoveItem_()
Dim sql As String '
On Error Resume Next
sql = "DELETE *" & vbCrLf & "FROM ITEMS" & vbCrLf & "WHERE(((ITEMS.PLC_add) = " &
Me.lst_item.SelectedItem.SubItems(1) & ") and ((ITEMS.Item) = " & Chr(34) &
Me.lst_item.SelectedItem.SubItems(2) & Chr(34) & "));"
db.Execute sql
Call dpl_Item
End Sub
Private Sub Cmd_Write_Click()
End Sub
Private Sub Form_Load()
Dim sql As String
On Error GoTo w_err
''' mo CSDL
Set db = New ADODB.Connection
db.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path
& "\ITEM.MDB" & ";Persist Security Info=False;Jet OLEDB:Database
Password=manhchau" ' chon bo may' DL
db.Open ' mo DL
''
w_err:
If Err.Number = -2147467259 Then MsgBox "khong thay FILE ITEM.mdb trong " &
App.Path: Exit Sub
Call Cmd_RemoveItem_
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & "," &
Chr(34) & "M0.0" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & "," &
Chr(34) & "M0.1" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & "," &
Chr(34) & "M0.2" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & "," &
Chr(34) & "Q0.0" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & "," &
Chr(34) & "Q0.1" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & ","
& Chr(34) & "Q0.2" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & ","
& Chr(34) & "Q0.3" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & ","
& Chr(34) & "Q0.4" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
sql = "INSERT INTO ITEMS(PLC_add,Item,Type)" & vbCrLf & "VALUES (" & 2 & ","
& Chr(34) & "Q0.5" & Chr(34) & "," & Chr(34) & "BOOL" & Chr(34) & ");"
db.Execute (sql)
Call dpl_Item
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Call Cmd_RemoveItem_
Set db = Nothing
End Sub
Private Sub dpl_Item()
Me.lst_item.ListItems.Clear
Dim rs As ADODB.Recordset
Dim sql As String
Dim i As Integer
i = 0
On Error GoTo w_err
Set rs = New ADODB.Recordset
sql = "SELECT * FROM ITEMS;"
'' nay du lieu
rs.Open sql, db
'' hien thi
rs.MoveFirst ' dua ve vi tri dau dong
While Not rs.EOF ' vong lap cho het dulieu
i = i + 1
Me.lst_item.ListItems.Add = Str(i) ' so ' thu tu.
''' load vao bang lvwTCT
Me.lst_item.ListItems(i).SubItems(1) = rs!PLC_add
Me.lst_item.ListItems(i).SubItems(2) = rs!Item
Me.lst_item.ListItems(i).SubItems(3) = rs!Type
rs.MoveNext
Wend
Set rs = Nothing
Exit Sub
w_err:
Exit Sub
End Sub
Private Sub MIDNIGHT_Click()
muclogic = 1
CHON = 2
Call cmd_Write_CCC
muclogic = 0
CHON = 2
Call cmd_Write_CCC
End Sub
Private Sub NOMAL_Click()
muclogic = 1
CHON = 1
Call cmd_Write_CCC
muclogic = 0
CHON = 1
Call cmd_Write_CCC
End Sub
Private Sub STOPS_Click()
muclogic = 1
CHON = 3
Call cmd_Write_CCC
muclogic = 0
CHON = 3
Call cmd_Write_CCC
End Sub
'''''''''''''''''''''''''''**************************''''''''''''''''''''''''''''''
Private Sub connect_OPC()
On Error GoTo ErrorHandler
Set MyOPCServer = New OPCServer ' Create OPCServer Object
Call MyOPCServer.Connect("S7200.OPCServer") ' connect from OPC Server
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Connecting to OPC Server", vbCritical, "ERROR"
End Sub
Private Sub Disconnect_OPC()
On Error GoTo ErrorHandler
MyOPCServer.Disconnect ' Disconnect from OPC Server
Set MyOPCServer = Nothing ' Delete OPCServer Object
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Disconnecting from OPC Server", vbCritical,
"ERROR"
End Sub
Public Sub add_Group()
On Error GoTo ErrorHandler
Set MyGroups = MyOPCServer.OPCGroups ' Get OPCGroups Collection Object
from MyOPCServer
' Set Default Properties for Group Collection
' These Properties are used to set the Properies for new Groups
MyGroups.DefaultGroupIsActive = 500 ' Set Default Group Update Rate to 500 ms
MyGroups.DefaultGroupIsActive = False ' Set Default Group Active State to Inactive
Set MyGroup = MyGroups.Add(Group_name) ' Add a new Group to the Group
Collection
' Set Group Properties
MyGroup.IsSubscribed = True ' Enable Callbacks
MyGroup.IsActive = True
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Adding a Group to OPC Server", vbCritical,
"ERROR"
End Sub
Private Sub remove_Group()
On Error GoTo ErrorHandler
MyGroups.RemoveAll ' Removes all Groups
Set MyGroup = Nothing ' Delete OPCGroup Object
Set MyGroups = Nothing ' Delete OPCGroups Collection Object
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Removing Group from OPC Server", vbCritical,
"ERROR"
End Sub
Private Sub add_Items()
'On Error GoTo ErrorHandler
Dim i As Long
Dim ErrorFlag As Boolean
Dim ItemObj As OPCItem
Dim ItemIDs(65535) As String
Dim ItemClientHandles(65535) As Long
Dim Errors() As Long ' Array for returned Item related errors
ErrorFlag = False
Set MyItems = MyGroup.OPCItems ' Get OPCItems Collection Object from
MyOPCServer
' Initialize the [IN] parameters for the Add Items call
' ItemIDs -> ItemIDs of the Items to add
' ItemClientHandles -> Client defined handles for the Items. The Server sends these
handles in the Callbacks
'''ADD ITEMS
'Read itemId from lst_item
For i = 1 To Me.lst_item.ListItems.Count Step 1
ItemIDs(i) = Me.lst_item.ListItems(i).SubItems(1) & "," &
Me.lst_item.ListItems(i).SubItems(2) & "," & Me.lst_item.ListItems(i).SubItems(3)
Next
''
For i = 1 To Me.lst_item.ListItems.Count Step 1
ItemClientHandles(i) = i
Next
' [OUT] parameters are
' ItemServerHandles -> Server defined handles for the Items. The client must use
these handles for all Read/Write calls
' Errors -> Item related errors
' Add Items to the Group
Call MyItems.AddItems(Me.lst_item.ListItems.Count, ItemIDs, ItemClientHandles,
MyItemServerHandles, Errors)
' Check Item Errors
For i = 1 To Me.lst_item.ListItems.Count
If Not Errors(i) = 0 Then
MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
ErrorFlag = True
End If
Next
' Continue only if all Items SUCCEEDED
If ErrorFlag Then
Dim RemoveErrors() As Long
Dim RemoveHandles(1) As Long
' Remove Succeede Items
For i = 1 To Me.lst_item.ListItems.Count
If Errors(i) = 0 Then
RemoveHandles(1) = MyItemServerHandles(i)
Call MyItems.Remove(1, RemoveHandles, RemoveErrors)
End If
Next
End If
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Adding Items to the Group", vbCritical, "ERROR"
End Sub
Private Sub remove_Items()
On Error GoTo ErrorHandler
Dim i As Long
Dim Errors() As Long ' Array for returned Item related errors
' Remove Items from the Group
Call MyItems.Remove(Me.lst_item.ListItems.Count, MyItemServerHandles, Errors)
' Check Item Errors
For i = 1 To Me.lst_item.ListItems.Count
If Not Errors(i) = 0 Then MsgBox "Item " + Str$(i) + " FAILED. Error Code = " +
Str$(Errors(i)), vbCritical
Next
Erase MyItemServerHandles ' Erase Item Server Handle Array
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Removing Items from the Group", vbCritical,
"ERROR"
End Sub
Private Sub Cmd_connect_Click()
Call connect_OPC
Call add_Group
Call add_Items
NOMAL.Enabled = True
MIDNIGHT.Enabled = True
STOPS.Enabled = True
Cmd_Exit.Enabled = False
Cmd_Connect.Enabled = False
Cmd_Disconnect.Enabled = True
End Sub
Private Sub Cmd_Disconnect_Click()
Call remove_Items
Call remove_Group
Call Disconnect_OPC
NOMAL.Enabled = False
MIDNIGHT.Enabled = False
STOPS.Enabled = False
Cmd_Exit.Enabled = True
Cmd_Connect.Enabled = True
Cmd_Disconnect.Enabled = False
End Sub
Private Sub cmd_Write_CCC()
On Error GoTo ErrorHandler
Dim i As Long
Dim Values(65535) As Variant '
Dim Errors() As Long ' Array for returned Item related errors
' Initialize the [IN] parameters for the SyncWrite call
' Values -> Values to write
Values(CHON) = muclogic ' Read Value from Text Box
' ItemServerHandles -> Server defined handles from the AddItems call
' Write Values Syncronous
Call MyGroup.SyncWrite(CHON, MyItemServerHandles, Values, Errors)
' Check Item Errors
If Not Errors(CHON) = 0 Then MsgBox "Item " +
Str$(Me.lst_item.SelectedItem.Index) + " FAILED. Error Code = " +
Str$(Errors(Me.lst_item.SelectedItem.Index)), vbCritical
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Writing Items Syncronous", vbCritical, "ERROR"
End Sub
' Callback from OnDataChange
Private Sub MyGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As
Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long,
TimeStamps() As Date)
On Error GoTo ErrorHandler
Dim i As Long
' Check Parameters
For i = 1 To NumItems
If ClientHandles(i) > 0 And ClientHandles(i) <= Me.lst_item.ListItems.Count Then
' Values -> Values from read complete
' Qualities -> Qualities of the values
If ItemValues(i) = True Then
Me.lst_item.ListItems(ClientHandles(i)).SubItems(4) = 1
ElseIf ItemValues(i) = False Then
Me.lst_item.ListItems(ClientHandles(i)).SubItems(4) = 0
Else
Me.lst_item.ListItems(ClientHandles(i)).SubItems(4) = ItemValues(i)
End If
Me.lst_item.ListItems(ClientHandles(i)).SubItems(5) =
GetQualityText(Qualities(i))
Else
MsgBox "DataChange Item " + Str$(i) + " has invalid Client Handle ", vbCritical
End If
Next
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "OnDataChange", vbCritical, "ERROR"
End Sub
Private Function GetQualityText(Quality) As String
Select Case Quality
Case 0: GetQualityText = "BAD"
Case 64: GetQualityText = "UNCERTAIN"
Case 192: GetQualityText = "GOOD"
Case 8: GetQualityText = "NOT_CONNECTED"
Case 13: GetQualityText = "DEVICE_FAILURE"
Case 16: GetQualityText = "SENSOR_FAILURE"
Case 20: GetQualityText = "LAST_KNOWN"
Case 24: GetQualityText = "COMM_FAILURE"
Case 28: GetQualityText = "OUT_OF_SERVICE"
Case 132: GetQualityText = "LAST_USABLE"
Case 144: GetQualityText = "SENSOR_CAL"
Case 148: GetQualityText = "EGU_EXCEEDED"
Case 152: GetQualityText = "SUB_NORMAL"
Case 216: GetQualityText = "LOCAL_OVERRIDE"
Case Else: GetQualityText = "UNKNOWN QUALITY"
End Select
End Function
Private Sub Timer1_Timer()
If Val(Me.lst_item.ListItems(4).SubItems(4)) = 1 And
Val(Me.lst_item.ListItems(9).SubItems(4)) = 1 Then
do1.Visible = False
vang1.Visible = False
xanh1.Visible = True
do2.Visible = True
xanh2.Visible = False
vang2.Visible = False
ElseIf Val(Me.lst_item.ListItems(7).SubItems(4)) = 1 And
Val(Me.lst_item.ListItems(6).SubItems(4)) = 1 Then
do2.Visible = False
vang2.Visible = False
xanh2.Visible = True
do1.Visible = True
xanh1.Visible = False
vang1.Visible = False
ElseIf Val(Me.lst_item.ListItems(5).SubItems(4)) = 1 And
Val(Me.lst_item.ListItems(9).SubItems(4)) = 1 Then
do1.Visible = False
vang1.Visible = True
xanh1.Visible = False
do2.Visible = True
xanh2.Visible = False
vang2.Visible = False
ElseIf Val(Me.lst_item.ListItems(8).SubItems(4))=1And
Val(Me.lst_item.ListItems(6).SubItems(4)) = 1 Then
do2.Visible = False
vang2.Visible = True
xanh2.Visible = False
do1.Visible = True
xanh1.Visible = False
vang1.Visible = False
ElseIf Val(Me.lst_item.ListItems(5).SubItems(4)) = 1 And
Val(Me.lst_item.ListItems(8).SubItems(4)) = 1 Then
do2.Visible = False
vang2.Visible = True
xanh2.Visible = False
do1.Visible = False
xanh1.Visible = False
vang1.Visible = True
Else
do2.Visible = False
vang2.Visible = False
xanh2.Visible = False
do1.Visible = False
xanh1.Visible = False
vang1.Visible = False
End If
End Sub