public OpcConnector(string progId, string plcName, string opcAddressFmt) { m_The_srv = new OpcServer(); m_The_srv.Connect(progId); Thread.Sleep(500); // we are faster then some servers! // add our only working group m_The_grp = m_The_srv.AddGroup(plcName + "-strings", false, 900); // add two items and save server handles for (int i = 0; i < StrCount; i++) m_Item_defs[i] = new OPCItemDef(string.Format(opcAddressFmt, plcName, 272 + i*6), true, i + 1, VarEnum.VT_EMPTY); OPCItemResult[] rItm; m_The_grp.AddItems(m_Item_defs, out rItm); if (rItm == null) return; if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error)) { InstantLogger.msg("OPCDirectWriter: {0} -- AddItems - some failed", plcName); m_The_grp.Remove(true); m_The_srv.Disconnect(); return; } for (int i = 0; i < StrCount; i++) m_Handles_srv[i] = rItm[i].HandleServer; m_The_grp.WriteCompleted += TheGrpWriteComplete; }
public override void ReadValue(Command command, Action <int, PointValueType, object> onValueReceive) { int index = command.DeviceAddress.IndexOf("|"); string ip = command.DeviceAddress.Substring(0, index); string progid = command.DeviceAddress.Substring(index + 1); OPC.Data.OpcServer opcServer = new OPC.Data.OpcServer(); opcServer.Connect(progid, ip); OPC.Data.OpcGroup group1 = new OPC.Data.OpcGroup("g2", false, 1000, 1000, 0); opcServer.OpcGroups.Add(group1); int tranid = 0; foreach (var point in command.Points) { OPCItem item = new OPCItem(point, tranid++); group1.Items.Add(item); } var states = group1.Items.GetItemValues(); foreach (OPCItemState itemState in states) { pushValue(itemState, onValueReceive); } opcServer.Disconnect(); }
public override bool[] WriteValue(Command command) { int index = command.DeviceAddress.IndexOf("|"); string ip = command.DeviceAddress.Substring(0, index); string progid = command.DeviceAddress.Substring(index + 1); OPC.Data.OpcServer opcServer = new OPC.Data.OpcServer(); opcServer.Connect(progid, ip); OPC.Data.OpcGroup group1 = new OPC.Data.OpcGroup("g3", false, 1000, 1000, 0); opcServer.OpcGroups.Add(group1); int tranid = 0; foreach (var point in command.Points) { OPCItem item = new OPCItem(point, tranid++); group1.Items.Add(item); } bool[] result = new bool[command.Values.Length]; for (int i = 0; i < command.Values.Length; i++) { if (command.Values[i] is Newtonsoft.Json.Linq.JArray) { var jarray = (Newtonsoft.Json.Linq.JArray)command.Values[i]; if (jarray.Count > 0) { try { var newValue = Array.CreateInstance(((Newtonsoft.Json.Linq.JValue)jarray[0]).Value.GetType(), jarray.Count); for (int j = 0; j < jarray.Count; j++) { newValue.SetValue(((Newtonsoft.Json.Linq.JValue)jarray[j]).Value, j); } command.Values[i] = newValue; result[i] = true; } catch { } } else { result[i] = true; } } else { result[i] = true; } } group1.Items.WriteItemValues(command.Values); opcServer.Disconnect(); return(result); }
public override bool CheckDeviceExist(Command command) { int index = command.DeviceAddress.IndexOf("|"); string ip = command.DeviceAddress.Substring(0, index); string progid = command.DeviceAddress.Substring(index + 1); try { OPC.Data.OpcServer opcServer = new OPC.Data.OpcServer(); opcServer.Connect(progid, ip); opcServer.Disconnect(); } catch { return(false); } return(true); }
private static void Main(string[] args) { MainConf = System.Configuration.ConfigurationManager.OpenExeConfiguration(""); Destination = MainConf.AppSettings.Settings["OPCDestination"].Value; CfgPath = MainConf.AppSettings.Settings["CfgPath"].Value; var reqUpdateRateMs = Convert.ToInt32(MainConf.AppSettings.Settings["OPCReqUpdateRate"].Value); MainGate = new ConnectionProvider.Client(new CoreListener()); MainGate.Subscribe(); var descriptionLoader = new LoaderCSV(Destination); descriptions = descriptionLoader.LoadAndGet(CfgPath); OpcServer_ = new OpcServer(); OpcServer_.Connect(MainConf.AppSettings.Settings["OPCServerProgID"].Value); OpcGroup_ = OpcServer_.AddGroup(Destination + "-flex-events", false, reqUpdateRateMs); OpcGroup_.DataChanged += OnDataChange; var hClient = 0; for (int dix = 0; dix < descriptions.Count; dix++) { var d = descriptions[dix]; foreach (var item in d.Arguments) { OpcItemDefs_.Add(new OPCItemDef(((Element) item.Value).opcItemID, true, ++hClient, VarEnum.VT_EMPTY)); ((Element) item.Value).cHandle = hClient; } } int[] aE; int addCount = 0; while (!OpcGroup_.AddItems(OpcItemDefs_.ToArray(), out OpcItemResults_)) { //if (++addCount > 1) throw new InvalidDataException("!!!AddItems failed"); for (var i = 0; i < OpcItemResults_.Count(); i++) { if (HRESULTS.Failed(OpcItemResults_[i].Error)) { OpcItemDefs_.RemoveAt(i); break; } } OpcGroup_.RemoveItems(OpcItemResults_.Select(ir => ir.HandleServer).ToArray(), out aE); } int k = 0; for (int j = 0; j < OpcItemDefs_.Count(); j++) SetServerHandle(OpcItemDefs_[j].HandleClient, OpcItemResults_[k++].HandleServer); for (int dix = 0; dix < descriptions.Count; dix++) Console.WriteLine(descriptions[dix]); OpcGroup_.Active = true; Console.WriteLine("OPCFlex is running, press enter to exit"); Console.ReadLine(); OpcGroup_.DataChanged -= OnDataChange; OpcGroup_.RemoveItems(OpcItemResults_.Select(ir => ir.HandleServer).ToArray(), out aE); OpcGroup_.Remove(false); OpcServer_.Disconnect(); Console.WriteLine("Bye!"); }