im trying to read value from OPC over DA protocol. I have found that titanium-as is free, thats my condition. i'm using this manual, where is writen how to read values from OPC server, but only from groups which you made on OPC server with your own created variables. I wanna read values which are already on OPC server and create group from them so i can read their values.
SOLVED
Here is solution
static void Main(string[] args)
{
TitaniumAS.Opc.Client.Bootstrap.Initialize();
Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6");
using (var server = new OpcDaServer(url))
{
server.Connect();
OpcDaGroup MyGroup = server.AddGroup("MyGroup");
MyGroup.IsActive = true;
var browser = new OpcDaBrowserAuto(server);
OpcDaBrowseElement[] MySimulation = browser.GetElements("Simulation.Functions");
OpcDaItemDefinition[] definitions = new OpcDaItemDefinition[MySimulation.Count()];
for (int i = 0; i < MySimulation.Count(); i++)
{
definitions[i] = new OpcDaItemDefinition { ItemId = MySimulation[i].ItemId, IsActive = true };
}
MyGroup.AddItems(definitions);
MyGroup.ValuesChanged += OnGroupValuesChanged;
MyGroup.UpdateRate = TimeSpan.FromMilliseconds(100); // ValuesChanged won't be triggered if zero
}
}
static void OnGroupValuesChanged(object sender, OpcDaItemValuesChangedEventArgs args)
{
// Output values.
foreach (OpcDaItemValue value in args.Values)
{
Console.WriteLine("ItemId: {0}; Value: {1}; Quality: {2}; Timestamp: {3}",
value.Item.ItemId, value.Value, value.Quality, value.Timestamp);
}
}
For forms you need to edit Program.cs like that
static class Program
{
/// <summary>
/// The main entry point for the application.
///
/// </summary>
//[STAThread]
static void Main()
{
TitaniumAS.Opc.Client.Bootstrap.Initialize();
var thread = new Thread(RunApplication);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
static void RunApplication()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Many thanks for any advise.