I need to write a simple c# app which communicates with a Siemens S7 via OPC. It just needs to query single inputs and set single outputs.
What do I need to do this? Do I need stuff like Xi from OPCFoundation?
As a more general OPC solution, it may be worth looking at http://www.codeproject.com/KB/COM/opcdotnet.aspx - its a free OPC DLL complete with source code. While it is quite basic, the code is there, so you could easily update it if required, but from the sounds of your requirements it may be ideal.
#region Variable Declaration
public static Kepware.ClientAce.OpcDaClient.DaServerMgt ObjDaServerMgt = new Kepware.ClientAce.OpcDaClient.DaServerMgt();
public static Kepware.ClientAce.OpcDaClient.ConnectInfo ObjConnectInfo = new Kepware.ClientAce.OpcDaClient.ConnectInfo();
public static Kepware.ClientAce.OpcDaClient.ReturnCode ObjReturnCode = new Kepware.ClientAce.OpcDaClient.ReturnCode();
public static Kepware.ClientAce.OpcDaClient.ItemIdentifier[] ObjItemIdentifiers = new Kepware.ClientAce.OpcDaClient.ItemIdentifier[2];
#endregion
public void Connect()
{
string OPC_url_Simatic = ConfigurationManager.AppSettings["ServerId"].ToString();
ObjConnectInfo.LocalId = "en";
ObjConnectInfo.KeepAliveTime = 5000;
ObjConnectInfo.RetryAfterConnectionError = true;
ObjConnectInfo.RetryInitialConnection = true;
bool connectFailed = false;
///define a client handle
int clientHandle = 1;
//Try to connect with the API connect method:
try
{
ObjDaServerMgt.Connect(OPC_url_Simatic, clientHandle, ref ObjConnectInfo, out connectFailed);
}
catch (Exception ex)
{
// MessageBox.Show("Handled Connect exception. Reason: " + ex.Message);.
log.Error(ex.ToString());
// Make sure following code knows connection failed:
connectFailed = true;
}
// Handle result:
if (connectFailed)
{
// Tell user connection attempt failed:
//MessageBox.Show("Connect failed");
log.Error("Connection Failed");
}
}
Register for some event handlers and get work done.
Use any intermediate dll like opcdaauto.dll, clientAce,MXIO.NET.
Start coding using C#.C# provides flexible in communicating with OPC Server.
Example with Siemens using C# : In App.config :
Tag1 value="SERVER:\DiagnosticsSummary\CumulatedSubscriptionCount"
Tag2 value="SERVER:\Capabilities\MinSupportedUpdateRate"
ServerId value="opcda://localhost/OPC.SimaticNET.1/{B6EACB30-42D5-11D0-9517-0020AFAA4B3C}"/>
--Enjoy the Coding-- Refer : http://www.revanayya.blogspot.com
Sounds like you may already have an OPC Server for Siemens S7. Kepware, Simatic Net, and many others support OPC UA.
You can build a simple HMI app in C# using these free tools:
Microsoft Visual Studio Express 2013 for Windows Desktop available from: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-for-windows-desktop.
The latest source from this site: http://web.archive.org/web/20140219181837/http://opcuaservicesforwpf.codeplex.com/
With this kit you can: Browse OPC UA servers directly from the Visual Studio IDE. Drag and drop the variable nodes to create a data subscription. Use bindings to animate your UI control's properties from the subscription's data values.
Check out the sample application 'Workstation'.
Another possibility would be using Siemens SAPI-S7 interface (which is the native Siemens protocol and which is used by OPC also). SoftwareOption GmbH has a S7 .NET programming interface with C# sample. You can download a C# demo program inclusive source code. See http://www.softwareoption.de/siemens-produkte_e.htm.
I'm going through this process at the moment, I found his very useful: http://www.opcti.com/Download-OPC-Xi-source-code.aspx
Just to fill the list with an alternative solution: If you just want to read/write some data/inputs/outputs/markers to/from the Siemens S7 you don't have to use OPC. I have been using libnodave without any issues for many years. Works fine as long as you enable put/get access in the hardware configuration of the plc and access "not optimized" datablocks. Libnodave states that it works with S7-200,300,400. But we have used it with 1200 and 1500 as well. Several sources on the net, see e.g. http://libnodave.sourceforge.net/
You would probably check www.quickopc.com which deliver a perfectly wrapper between .NET and OPC Servers. There are also a lot of information for OPC Servers by www.opcfoundation.com, which you already find out yourself.
If you don't use libraries such as QuickOPC you have to deal with creating own classes to translate binary data to readable .NET codes (which have to be a task itself). This is fully doable, the major drawback is that every each device/manufacturer have thier own protocol, which means you can end up with a single wrapper per each unique device.