| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1330 人关注过本帖
标题:VB 与OPC SERVER通讯
只看楼主 加入收藏
lihaisen
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-1-24
收藏
 问题点数:0 回复次数:0 
VB 与OPC SERVER通讯

请问这局是什么意思:OPCMyitems.AddItems 1, OPCItemIDs, ClientHandles, ItemServerHandles, Errors ''',

以下是原代码

Option Explicit


Public sql, signal1, signal2, signal3, signal4 As String
Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset

Option Base 1

Private Const ItemMax = 35 'Maximum of registered Items
Private Const HEAD_TKBSVR As String = "Takebishi" 'Header of TAKEBISHI OPC Server
Dim WithEvents OPCMyserver As OPCServer 'Server object
Dim WithEvents OPCMygroups As OPCGroups 'Group collection
Dim WithEvents OPCMygroup As OPCGroup 'Group object
Dim OPCMyitems As OPCItems 'Item collection
Dim OPCMyitem As OPCItem 'Item object


Dim bConnect As Boolean

Private Sub CONNECT_Click()

Dim ItemServerHandles() As Long
Dim ClientHandles(1) As Long
Dim OPCItemIDs(1) As String
Dim Errors() As Long
Dim i As Integer

If bConnect = False Then
On Error GoTo ConnectError

Set OPCMyserver = New OPCServer
OPCMyserver.CONNECT Form1.ServerName.List(ServerName.ListIndex), ""

Set OPCMygroups = OPCMyserver.OPCGroups
Set OPCMygroup = OPCMygroups.Add("Group1")
OPCMygroup.UpdateRate = Val(UpdateRateSet.Text)
Set OPCMyitems = OPCMygroup.OPCItems

For i = 1 To ItemMax
ClientHandles(1) = i
OPCItemIDs(1) = Form1.ItemName(i - 1).Text
OPCMyitems.AddItems 1, OPCItemIDs, ClientHandles, ItemServerHandles, Errors ''', RequestedDataTypes, AccessPaths
If Errors(1) <> 0 Then
Form1.Value(i - 1) = "Error"
End If
Next i

bConnect = True
CONNECT.Caption = "DisConnect"
READ_Button.Enabled = True
WRITE_Button.Enabled = True
ADVISE_Button.Enabled = True
ADVISE_Button.Caption = "Auto Read On"
OPCMygroup.IsActive = False

For i = ItemName.LBound To ItemName.UBound
ItemName(i).Enabled = False
Next i
Else
On Error Resume Next

OPCMygroup.IsActive = False
OPCMygroups.Remove OPCMygroup.ServerHandle

Set OPCMyitems = Nothing 'Delete Item collection
Set OPCMyitem = Nothing 'Delete Item object
Set OPCMygroups = Nothing 'Delete Group collection
Set OPCMygroup = Nothing 'Delete Group object

OPCMyserver.Disconnect 'Disconnect with OPC Server
Set OPCMyserver = Nothing 'Delete Server object

bConnect = False
READ_Button.Enabled = False
WRITE_Button.Enabled = False
ADVISE_Button.Enabled = False
CONNECT.Caption = "Connect"
For i = ItemName.LBound To ItemName.UBound
ItemName(i).Enabled = True
Next i

Exit Sub
End If
Exit Sub

ConnectError:
MsgBox "Error Connecting"
For i = 0 To ItemMax - 1
Form1.Value(i) = "Error"
Next i

End Sub

Private Sub Form_Load()

Call link

Dim Getserver As OPCServer
Dim Servers As Variant
Dim i As Integer
Dim ItemName As String
Dim Fno As Integer
On Error GoTo LoadEnd
ServerName.Clear
Set Getserver = New OPCServer
Servers = Getserver.GetOPCServers
For i = LBound(Servers) To UBound(Servers)
If InStr(1, Servers(i), HEAD_TKBSVR, vbTextCompare) > 0 Then
ServerName.AddItem Servers(i)
End If
Next i
Set Getserver = Nothing
ServerName.ListIndex = 0

Fno = FreeFile(1)
Open "OPCSample2.INI" For Input As #Fno
i = 1
Do While Not EOF(1) 'Repeat to the terminal of the file.
Input #Fno, ItemName
Form1.ItemName(i - 1).Text = ItemName
If i > ItemMax Then
Exit Do
End If
i = i + 1
Loop

Close #Fno
Exit Sub
LoadEnd:
If Fno > 0 Then
Close #Fno
End If
For i = 0 To ItemMax - 1
Form1.ItemName(i).Text = "Device1.y" + Format$(i)
Next i

End Sub


Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim Fno As Integer
If bConnect = True Then
CONNECT_Click
End If
Fno = FreeFile(1)
Open "OPCSample2.INI" For Output As #Fno
For i = 1 To ItemMax
Print #Fno, Form1.ItemName(i - 1).Text
Next i
Close #Fno

End Sub

Private Sub READ_Button_Click()
On Error Resume Next
Dim anItem As OPCItem
For Each anItem In OPCMygroup.OPCItems
anItem.Read OPCDevice ', value, qual, time ' If subscribed, don't do this!
Form1.Value(anItem.ClientHandle - 1) = anItem.Value
Form1.Time(anItem.ClientHandle - 1) = anItem.TimeStamp
Form1.Quality(anItem.ClientHandle - 1) = anItem.Quality
Next anItem
Set anItem = Nothing

End Sub


Private Sub Timer1_Timer()

Call send
If Label1.ForeColor = &HFF00& Then
Label1.ForeColor = &HFF&
ElseIf Label1.ForeColor = &HFF& Then
Label1.ForeColor = &HFF00&
End If
End Sub

Private Sub WRITE_Button_Click()

On Error Resume Next
Dim anItem As OPCItem
For Each anItem In OPCMygroup.OPCItems
anItem.Write Form1.Value(anItem.ClientHandle - 1)
Next anItem
Set anItem = Nothing

End Sub
Private Sub ADVISE_button_Click()

OPCMygroup.IsActive = Not OPCMygroup.IsActive
OPCMygroup.IsSubscribed = OPCMygroup.IsActive
If OPCMygroup.IsActive = False Then
ADVISE_Button.Caption = "Auto Read On"
Timer1.Enabled = False
Else
ADVISE_Button.Caption = "Auto Read Off"
Timer1.Enabled = True
READ_Button_Click
End If
End Sub

Private Sub OPCMygroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)

Dim id As Integer
Dim i As Integer
For i = 1 To NumItems
id = ClientHandles(i) - 1
Form1.Value(id) = ItemValues(i)
Form1.Time(id) = TimeStamps(i)
Form1.Quality(id) = Qualities(i)
Next i

End Sub
Private Sub OPCMyserver_ServerShutDown(ByVal Reason As String)
MsgBox "Server Shutdown"

End Sub
Private Sub send()

Dim t(36)
Dim j
Dim i
Dim d

For i = 1 To 35
t(i) = Value(i - 1).Text
Next i

If Value(34).Text <> "" Then
rs.AddNew

For j = 1 To 13
rs.Fields(j).Value = Val(t(j))
Next j

For d = 17 To 30
rs.Fields(d + 2).Value = Val(t(d))
Next d

rs.Fields(0).Value = Now

rs.Fields(15).Value = Val(t(14))
rs.Fields(16).Value = Val(t(15))
rs.Fields(18).Value = Val(t(16))


rs.Fields(33).Value = Val(t(33))
rs.Fields(34).Value = Val(t(34))
rs.Fields(35).Value = Val(t(35))
rs.Update
' rs.Close
End If

End Sub
Private Sub link() '''连接

Set cn = New Connection
With cn
.ConnectionTimeout = 3
.CursorLocation = adUseClient
.Provider = "msdaora.1"
End With
cn.Open "password=1;user ID=lgyb;data source=lgyb;Persist Security Info=True"

Set rs = New ADODB.Recordset
sql = "select * from lsb "
rs.Open sql, cn, adOpenStatic, adLockOptimistic

End Sub

搜索更多相关主题的帖子: 通讯 registered 
2006-01-24 16:57
快速回复:VB 与OPC SERVER通讯
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.108718 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved