public bool InitOPC(Action <string, eOutputType> output) { OutPut = output; try { opcClient.OnDataReveiced += new OPCClient.DataReveiced(opcClient_OnDataReveiced); opcClient.OnScanError += new OPCClient.ScanErrorEventHandler(opcClient_OnScanError); //连接OPC if (opcClient.Connect(this.ServerName, this.ServerIP)) { //创建组 opcClient.CreateGroup(Tags); //开始读数 opcClient.Read(); return(true); } return(false); } catch (Exception ex) { output(string.Format("OPC服务" + ServerName + "初始化异常:{0}", ex.Message), eOutputType.Error); } return(false); }
private static void CreatGroups() { opcKepClient.ConnectServer("", sServerName); if (opcKepClient.opc_connected == true) { opcKepClient.itmHandleClient = 0; opcKepClient.lKepItem = new List <OPCItem>(); foreach (OPCItemsStruct OPCItemsStrt in lOPCItemsStrt) { opcKepClient.CreateGroup(OPCItemsStrt.sGroupName, 250, true, OPCItemsStrt.sItems.ToList()); } } }
/*private static void InsertSql(int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) * { * * for (int i = 1; i <= NumItems; i++) * { * int Index = (int)ClientHandles.GetValue(i); * string ItemID = opcKepClient.lKepItem[Index].ItemID; * var ItemValue = opcKepClient.lKepItem[Index].Value; * int? ItemQuality = opcKepClient.lKepItem[Index].Quality; * DateTime? ItemTimeStamp = opcKepClient.lKepItem[Index].TimeStamp; * string sqldelet = string.Format("delete from {0} where {1}='{2}' ", "Runtimes", "ItemID", ItemID); * //string sqlInsert = string.Format("INSERT INTO {0}({1},{2},{3},{4},{5}) VALUES ({6},{7},{8},{9},{10})", "Runtimes", "ItemID", * // "ItemValue", "ItemQualitie", "ItemTimeStamp", "UpdateTime", ItemID, ItemValue, ItemQuality, ItemTimeStamp, DateTime.Now); * string sqlInsert = string.Format("INSERT INTO {0} VALUES ('{1}','{2}','{3}','{4}','{5}') ", "Runtimes", * ItemID, ItemValue, ItemQuality, ItemTimeStamp, DateTime.Now); * SQLClient sqlclnt = new SQLClient(); * //sqlclnt.sConnectStr="Data Source=.;Initial Catalog=Runtime;Integrated Security=True"; * sqlclnt.sqlExecuteNonQuery(sqldelet); * sqlclnt.sqlExecuteNonQuery(sqlInsert); * } * * }*/ private static void CreatGroups() { opcKepClient.ConnectServer("", sServerName); //Thread.Sleep(1000); if (opcKepClient.opc_connected == true) { opcKepClient.itmHandleClient = 0; opcKepClient.lKepItem = new List <OPCItem>(); foreach (OPCItemsStruct OPCItemsStrt in lOPCItemsStrt) { opcKepClient.CreateGroup(OPCItemsStrt.sGroupName, 250, true, OPCItemsStrt.sItems.ToList()); } /*AsyncReadTimer.Enabled = true; * Console.WriteLine("Updata From OPC Server => Start"); * SqlInsertTimer.Enabled = true; * Console.WriteLine("Insert To Sql => Start"); */ } }