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;
        }
        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!");
        }
        public OpcConnector(string progId, string opcDestination, string opcAddressFmt, int opcConvSchema = 0,
            int reqUpdateRate_ms = 500)
        {
            using (Logger l = new Logger("OpcConnector")) {
                string plcName = "NO-PLC";
                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(opcDestination + "-items", false, reqUpdateRate_ms);

                // add all the items and save server handles
                int itemCounter = 0;

                EventsList = BaseEvent.GetEvents();
                for (int eventCounter = 0; eventCounter < EventsList.Length; eventCounter++) {
                    var heatEvent = EventsList[eventCounter];
                    l.msg(heatEvent.Name); // название события
                    EventStore.Add((CommonTypes.BaseEvent) Activator.CreateInstance(heatEvent));
                    // создаем экземпляр события
                    int plcpIndex = -1;
                    bool opcRelated = false;
                    for (int i = 0; i < heatEvent.GetCustomAttributes(false).Length; i++) {
                        object x = heatEvent.GetCustomAttributes(false)[i];
                        if (x.GetType().Name == "PLCGroup") {
                            PLCGroup p = (PLCGroup) x;
                            if (p.Destination == opcDestination) {
                                plcName = p.Location;
                                l.msg("     {0} ==>> {1}", p.Location, p.Destination);
                                opcRelated = true;
                                break;
                            }
                        }
                    }
                    if (!opcRelated) continue;
                    for (int propertyCounter = 0; propertyCounter < heatEvent.GetProperties().Length; propertyCounter++) {
                        var prop = heatEvent.GetProperties()[propertyCounter];
                        object first = null;
                        for (int index = 0; index < prop.GetCustomAttributes(false).Length; index++) {
                            object x = prop.GetCustomAttributes(false)[index];
                            if (x.GetType().Name == "PLCPoint") {
                                plcpIndex = index;
                                first = x;
                                break;
                            }
                        }
                        var plcp = (PLCPoint) first;
                        if (plcp == null) continue;
                        string s = string.Format(opcAddressFmt, plcName, cnv(plcp.Location, opcConvSchema));
                        // add new OPC Item using itemCounter as client handle
                        m_Item_defs.Add(new OPCItemDef(s, true, itemCounter, VarEnum.VT_EMPTY));
                        m_Item_props.Add(new FledgedItemDef(eventCounter, propertyCounter, plcpIndex));
                        l.msg("{0}:  {1}", itemCounter, s);
                        itemCounter++;
                    }
                    if (itemCounter >= maxItemCount) break;
                }
                l.msg("Counter is {0}", itemCounter);
                if (itemCounter == 0) throw new Exception("No items found");
                // Validate Items (ignoring BLOBs
                OPCItemResult[] rItm;
                m_The_grp.ValidateItems(m_Item_defs.ToArray(), false, out rItm);
                if (rItm == null) throw new Exception("OPC ValidateItems: -- system error: arrRes is null");
                List<int> itemExclude = new List<int>();
                for (int i = 0; i < itemCounter; i++) {
                    if (HRESULTS.Failed(rItm[i].Error)) {
                        l.err(
                            "Error 0x{1:x} while adding item {0} -- item EXCLUDED from monitoring",
                            i, rItm[i].Error);
                        itemExclude.Add(i);
                    }
                }
                if (itemCounter == itemExclude.Count) throw new Exception("No items passed validation");
                // Exclude invalid items
                // Add Items
                m_The_grp.AddItems(m_Item_defs.ToArray(), out rItm);
                if (rItm == null) return;
                for (int i = 0; i < itemCounter; i++) {
                    if (HRESULTS.Failed(rItm[i].Error))
                        rItm[i].HandleServer = -1;
                }

                m_Handles_srv = new int[itemCounter];
                for (int i = 0; i < itemCounter; i++)
                    m_Handles_srv[i] = rItm[i].HandleServer;
                //m_The_grp.WriteCompleted += TheGrpWriteComplete;

                int cancelId;
                int[] aE;
                //  l.msg("start read");
                m_The_grp.SetEnable(true);
                m_The_grp.Active = true;
                m_The_grp.DataChanged += new DataChangeEventHandler(this.TheGrpDataChange);
                m_The_grp.ReadCompleted += new ReadCompleteEventHandler(this.TheGrpReadComplete);
                //m_The_grp.Read(m_Handles_srv, 55667788, out cancelId, out aE);

                //Thread.Sleep(500);
                // l.msg("end read");
            }
        }