OPC DA Communication

nandgate

Member
Join Date
Nov 2012
Location
Hyderabad
Posts
14
Dear Friends,

I am using MS Visual Studio 2010 for creating a OPC DA Client (VB).
i have written the following code for connection/ disconnection of OPC Server

'Connect

Try
ConnectedOPCServer = New OPCAutomation.OPCServer
ConnectedOPCServer.Connect("OPC.SimaticNet.1")
Connect.Enabled = False
Disconnect.Enabled = True
MessageBox.Show("OPC Server is Connected")
Catch ex As Exception
ConnectedOPCServer = Nothing
MessageBox.Show("OPC server connect failed: " + ex.Message, "Exception", MessageBoxButtons.OK)
End Try
Try
ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True
ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0
ConnectedGroup = ConnectedOPCServer.OPCGroups.Add("A")
ConnectedGroup.UpdateRate = 10
ConnectedGroup.IsSubscribed = True
Catch ex As Exception
MessageBox.Show("OPC server add group failed: " + ex.Message, "Exception", MessageBoxButtons.OK)
End Try


'Disconnect

If Not ConnectedGroup Is Nothing Then
Try
ConnectedOPCServer.OPCGroups.Remove("A")
Catch ex As Exception
MessageBox.Show("OPC server remove group failed: " + ex.Message, "Exception", MessageBoxButtons.OK)
Finally
ConnectedGroup = Nothing
End Try
End If

If Not ConnectedOPCServer Is Nothing Then
Try
ConnectedOPCServer.Disconnect()
Connect.Enabled = True
Disconnect.Enabled = False
MessageBox.Show("OPC Server is disconnected")
Catch ex As Exception
MessageBox.Show("OPC server disconnect failed: " + ex.Message, "Exception", MessageBoxButtons.OK)
Finally
ConnectedOPCServer = Nothing
End Try
End If


So far there are no problems with this code. Can anyone guide me with Data Read/Write Code ??

Sorry for my bad English

With Regards,
NandGate.
 
Hello,

Here is a sample that uses the automation interface. I have never written one in VB that uses the custom interfaces.

Imports OPCAutomation

'you must have the OPC DA Automation wrapper has a reference.

Public Class Form1
Const itemCount = 6
Dim WithEvents opc_Server As OPCServer
Dim opc_Groups As OPCGroups
Dim WithEvents opc_Group As OPCGroup
Dim clientHandles(itemCount) As Integer
Dim opc_items(itemCount) As OPCItem

Private Sub ConnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectBtn.Click
opc_Server = New OPCServer()
If opc_Server Is Nothing Then
MsgBox("Server not created", vbCritical, "")
Exit Sub
End If

opc_Server.Connect("MODOPC.DA2")
If opc_Server Is Nothing Then
MsgBox("OPC server connection failed", vbCritical, "")
Exit Sub
End If

ConnectedLbl.Visible = True
ConnectedLbl.Update()

'add the group
opc_Groups = opc_Server.OPCGroups
opc_Group = opc_Groups.Add("Group_1")
opc_Group.IsActive = True


'create the items
opc_items(0) = opc_Group.OPCItems.AddItem("Port_1.400001", clientHandles(0))
opc_items(1) = opc_Group.OPCItems.AddItem("Port_1.400002", clientHandles(1))
opc_items(2) = opc_Group.OPCItems.AddItem("Port_1.400003", clientHandles(2))
opc_items(3) = opc_Group.OPCItems.AddItem("Port_1.400004", clientHandles(3))
opc_items(4) = opc_Group.OPCItems.AddItem("Port_1.400005", clientHandles(4))
opc_items(5) = opc_Group.OPCItems.AddItem("Port_1.100001", clientHandles(5))

ReadTimer_Tick(sender, e) 'we can start reading


End Sub

Private Sub DisconnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectBtn.Click

ConnectedLbl.Visible = False
ConnectedLbl.Update()

If opc_Server Is Nothing Then 'safety
Exit Sub
End If

opc_Server.OPCGroups.RemoveAll() 'free all the items and groups
opc_Server.Disconnect() 'disconnect from the OPC Server
opc_Server = Nothing
End Sub

Private Sub ReadTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadTimer.Tick
Dim i As Integer

Dim ServerHandles As Array = Array.CreateInstance(GetType(Int32), itemCount) 'item ID (server side)
Dim Values As Array = Array.CreateInstance(GetType(Object), itemCount) 'return values
Dim Errors As Array = Array.CreateInstance(GetType(Int32), itemCount)


Dim Qual As Array = Array.CreateInstance(GetType(Object), itemCount)
Dim TimeValue As Array = Array.CreateInstance(GetType(Object), itemCount)

Try
ReadTimer.Enabled() = False
If opc_Server Is Nothing Then 'safety
Exit Sub
End If

If Not (opc_Server.ServerState = OPCServerState.OPCRunning) Then 'safety
Exit Sub
End If

'set up which items to be read
For i = 0 To itemCount - 1
ServerHandles(i) = opc_items(i).ServerHandle
Next i

'Try
opc_Group.SyncRead(OPCDataSource.OPCCache, 5, ServerHandles, Values, Errors, Qual, TimeValue)

'Catch ex As Exception

'End Try
Label1.Text = Values(1)
Label1.Update()
Label2.Text = Values(2)
Label2.Update()
Label3.Text = Values(3)
Label3.Update()
Label4.Text = Values(4)
Label4.Update()
Label5.Text = Values(5)
Label5.Update()
' Label6.Text = Values(5)
' Label6.Update()

'There seems to be some issue with zero based arrays and one based arrays.
'I added 6 items with success but I cannot read the six items.
'As you see label1 is using values(1), it should be values(0). I am not a VB
'programmer so I do not know what is going on.

Finally
ReadTimer.Enabled() = True

End Try



End Sub
End Class
 
Dude,
Thank you very much for the code.
I am able to run the program without any run time errors.
Tomorrow i am gonna check the functioning of the code when it is physically connected to a PLC.🤞🏻


Thanks and regards,
Nandgate
 
'There seems to be some issue with zero based arrays and one based arrays.
'I added 6 items with success but I cannot read the six items.
'As you see label1 is using values(1), it should be values(0). I am not a VB
'programmer so I do not know what is going on.

If I remember correctly, all the arrays returned by OPCAutomation are 1-based. That was OK for VB6, where one could choose between 0 and 1-based arrays, but causes additional complications in VB.NET where all arrays are 0-based.
 
Dear Guys,
whenever i try increasing the itemcount value i am getting errors in runtime. Why is it happening?? and i am not able to read item '0' value. Any Specific reasons for this??
Also Please share the code for writing data to PLC.
 
Hello,

Telling us you are getting an error without telling us the error type/kind/code/message does not allow much help. Plus on what line does the error occur?

>...read item '0' value.

Read the comment at the end.
 
I have solved all the other problems, Only "write data to PLC (Sync Write)" code is still troubling me. Please help me out with that.
 
The final code looks something like this


Public Class Ayyagari
Const itemCount = 16
Dim WithEvents opc_Server As OPCServer
Dim opc_Groups As OPCGroups
Dim WithEvents opc_Group As OPCGroup
Dim clientHandles(itemCount) As Integer
Dim opc_items(itemCount) As OPCItem
Dim Errorintimer


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
Try
opc_Server = New OPCServer()
If opc_Server Is Nothing Then
MsgBox("Server not created", vbCritical, "")
Exit Sub
End If

opc_Server.Connect("OPC.SimaticNet.1")
If opc_Server Is Nothing Then
MsgBox("OPC server connection failed", vbCritical, "")
Exit Sub
End If


'add the group
opc_Groups = opc_Server.OPCGroups
opc_Group = opc_Groups.Add("A")
opc_Group.IsActive = True


'create the items
opc_items(0) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB0", clientHandles(0))
opc_items(1) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB0", clientHandles(1))
opc_items(2) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB1", clientHandles(2))
opc_items(3) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB2", clientHandles(3))
opc_items(4) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB3", clientHandles(4))
opc_items(5) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]C0", clientHandles(5))
opc_items(6) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]C1", clientHandles(6))
opc_items(7) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB4", clientHandles(7))
opc_items(8) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB5", clientHandles(8))
opc_items(9) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB6", clientHandles(9))
opc_items(10) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB7", clientHandles(10))
opc_items(11) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB8", clientHandles(11))
opc_items(12) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB9", clientHandles(12))
opc_items(13) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB10", clientHandles(13))
opc_items(14) = opc_Group.OPCItems.AddItem("S7:[S7 connection_1]MB11", clientHandles(14))

MessageBox.Show("OPC Server is Connected")
If Errorintimer = False Then
Timer1_Tick(sender, e) 'we can start reading
Else
MessageBox.Show("Something is Wrong with the Code")
End If

Catch ex As Exception
MessageBox.Show("OPC Connection failed")
End Try

End Sub

Private Sub Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Disconnect.Click
Try
If opc_Server Is Nothing Then 'safety
Exit Sub
End If

opc_Server.OPCGroups.RemoveAll() 'free all the items and groups
opc_Server.Disconnect() 'disconnect from the OPC Server
opc_Server = Nothing
MessageBox.Show("OPC server has been Disconnected")
Catch ex As Exception
MessageBox.Show("OPC Disconnection failed")
End Try

End Sub



Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
Dim ServerHandles As Array = Array.CreateInstance(GetType(Int32), itemCount) 'item ID (server side)
Dim Values As Array = Array.CreateInstance(GetType(Object), itemCount) 'return values
Dim Errors As Array = Array.CreateInstance(GetType(Int32), itemCount)
Dim a(5) As Array

Dim Qual As Array = Array.CreateInstance(GetType(Object), itemCount)
Dim TimeValue As Array = Array.CreateInstance(GetType(Object), itemCount)

Try
Timer1.Enabled() = False
If opc_Server Is Nothing Then 'safety
Exit Sub
End If

If Not (opc_Server.ServerState = OPCServerState.OPCRunning) Then 'safety
Exit Sub
End If

'set up which items to be read
Try
For i = 0 To 14
ServerHandles(i) = opc_items(i).ServerHandle
Next i
Catch ex As Exception
MessageBox.Show("server handle code has some problem")
Errorintimer = 1
Timer1.Enabled = False
End Try


Try
opc_Group.SyncRead(OPCDataSource.OPCCache, 14, ServerHandles, Values, Errors, Qual, TimeValue)
For i = 1 To 14
Me.Controls("TextBox" & i).Text = Values(i)
Next
For i = 15 To 28
Me.Controls("TextBox" & i).Text = Qual(i - 14)
Next

Catch ex As Exception
MessageBox.Show("Sync read failed")
Errorintimer = 1
Timer1.Enabled = False
End Try

' Label6.Text = Values(5)
' Label6.Update()

'There seems to be some issue with zero based arrays and one based arrays.
'I added 6 items with success but I cannot read the six items.
'As you see label1 is using values(1), it should be values(0). I am not a VB
'programmer so I do not know what is going on.
Finally
If Errorintimer = False Then
Timer1.Enabled() = True
Else
Timer1.Enabled() = False
End If
End Try
End Sub
End Class
 
Last edited:
Hello,

I have never created a write example in VB but, I did for Excel. It should be similar.

Private Sub WriteBtn_Click()
Dim ServerHandles(36) As Long 'item ID (server side)
Dim Values(36) As Variant 'values
Dim Errors() As Long
Dim i As Integer

If OPCServer1 Is Nothing Then 'safety
Exit Sub
End If

If Not (OPCServer1.ServerState = OPCServerState.OPCRunning) Then 'safety
Exit Sub
End If

'set up which items to be write
For i = 1 To 36
ServerHandles(i) = MyOPCItems(i).ServerHandle
Next i

'fetch the values from the cells
For i = 1 To 36
Values(i) = Cells(8 + i, 3)
If Values(i) = "" Then
Values(i) = 0
End If
Next i

Call OPCGroup1.SyncWrite(36, ServerHandles, Values, Errors)
End Sub
 
This is the code i have used for write operation

Dim ServerHandles As Array = Array.CreateInstance(GetType(Int32), itemCount)
Dim Values As Array = Array.CreateInstance(GetType(Object), itemCount)
Dim Errors As Array = Array.CreateInstance(GetType(Int32), itemCount)
If opc_Server Is Nothing Then 'safety
Exit Sub
End If

If Not (opc_Server.ServerState = OPCServerState.OPCRunning) Then 'safety
Exit Sub
End If

'set up which items to be write
Try
ServerHandles(7) = opc_items(7).ServerHandle
Values(7) = 23
opc_Group.SyncWrite(14, ServerHandles, Values, Errors)
Catch ex As Exception
MessageBox.Show("Write Failed")
End Try
 
Hello,

For me, the image is useless. It is too small and scaling just produces a blob.

For the code: You need to read the spec for SyncWrite and what the parameters are used to communicate. You are telling it to write 14 items and you are passing in arrays that are 36 long.

Might work. NOT TESTED. JUST GUESSING


Dim ServerHandles As Array = Array.CreateInstance(GetType(Int32), 1)
Dim Values As Array = Array.CreateInstance(GetType(Object), 1)
Dim Errors As Array = Array.CreateInstance(GetType(Int32), 1)
If opc_Server Is Nothing Then 'safety
Exit Sub
End If

If Not (opc_Server.ServerState = OPCServerState.OPCRunning) Then 'safety
Exit Sub
End If

'set up which items to be written
Try
ServerHandles(1) = opc_items(7).ServerHandle
Values(1) = 23
opc_Group.SyncWrite(1, ServerHandles, Values, Errors)
Catch ex As Exception
MessageBox.Show("Write Failed")
End Try
 
Now the code is working Fine.
Special Thanks to Mark for the Support.

If Any body wants the final code of OPC Client, just send me a message. I will fwd it
 
No value return when read from OPC Cache using opcautomation wrapper

Hi guys,

I'm a dead man now,I'm doing cache read using Interop.opcautomation.dll in c# but never get the values,all have returned to my program is null. OPC Server used in my project is Kepware server.Please I really need help and quick response.My code looks like below:

OPCServer opcServer = new OPCServer();
Array OPCItemIDs = Array.CreateInstance(typeof(string),
Array ItemServerHandles = Array.CreateInstance(typeof(Int32),
Array ItemServerErrors = Array.CreateInstance(typeof(Int32),
Array ClientHandles = Array.CreateInstance(typeof(Int32),
Array RequestedDataTypes = Array.CreateInstance(typeof(Int16),
Array AccessPaths = Array.CreateInstance(typeof(string),

OPCGroup OpcGroupNames;

try
{
opcServer.Connect("Kepware.KEPServerEX.V5", "");
}
catch (Exception ex)
{
throw ex;
}

//Add tags and OPC group.
//set up the tags
OPCItemIDs.SetValue(pointName, 1);
OpcGroupNames = opcServer.OPCGroups.Add(pointGroup);
OpcGroupNames.DeadBand = 0;
OpcGroupNames.UpdateRate = 1000;
OpcGroupNames.IsSubscribed = true;
OpcGroupNames.IsActive = true;
OpcGroupNames.OPCItems.AddItems(1, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);

//Read the values from the server for those tags.
Array ItemServerReadValues = Array.CreateInstance(typeof(string), 2);
object a;
object b;

try
{
System.Threading.Thread.Sleep(1000);
OpcGroupNames.SyncRead((short)OPCDataSource.OPCCache, 1, ref ItemServerHandles, out ItemServerReadValues, out ItemServerErrors, out a, out b);
}

catch (Exception ex)
{
throw ex.InnerException;
}

var value = ItemServerReadValues.GetValue(1);
opcServer.OPCGroups.RemoveAll();
opcServer.Disconnect();
 

Similar Topics

Hi good day Everyone, I have a cimplicity v10 project with 7 to 8k tags communicating with AB PLC through OPC and Rslinx classic. I have this...
Replies
1
Views
82
Trying to use the APPICOM OPC server that comes with Bradcommunications APPLICOM Card for wornderware. When I "browse" the OPC tags I get Group...
Replies
14
Views
4,243
Dear experts, I have the following setup: CPU-313C (6ES7313-5BF03-0AB0 V2.6) + CP343-1 (6GK7343-1EX30-0XE0 V2.0) communicating with Siemens PC...
Replies
7
Views
2,633
Hey! In my previous queries on how to handle an OPC server like RSLinx in the ArchestrA IDE galaxy, I have succeeded and the answer was to just...
Replies
0
Views
1,786
Hi, Has anyone communicated Rexroth IndtaControl PLC CML40.2 with over OPC or MODBUS TCP/IP? I am working on a Project for MES where i need to...
Replies
0
Views
1,567
Back
Top Bottom