public bool DoInit() { string[] itemstrings = new string[5]; string sPLC_Adr = "*UnDef*",sAlias = "*UnDef*",sGroup = "",sDesc = ""; int iPLCDataLength; sAliasToControl = "ACT_C1_STAHLMARKE0"; itemstrings[2] = itemstrings[3] = itemstrings[4] = "-"; try { selectedOpcSrv = "OPC.SimaticNET.1"; // ����� ���� �������� AddLogg("������� �� " + sCompName); AddLogg("LoadDefinitions()-Start"); LoadDefinitions(); AddLogg("LoadDefinitions()-End"); theSrv = new OpcServer(); if (!DoConnect(selectedOpcSrv)) { AddLogg("DoConnect(" + selectedOpcSrv + ")=false"); //////////return false; } // add event handler for server shutdown theSrv.ShutdownRequested += new ShutdownRequestEventHandler(this.theSrv_ServerShutDown); // precreate the only OPC group in this example if (!CreateGroup()) { AddLogg("CreateGroup()=false"); //////////return false; } AddLogg("DoInit: Start"); RemoveItem(); // first remove previous item if any cbCtrlAliases.Items.Clear(); groupLi = new List<clsGroupInfo>(); pointLi = new List<clsPointInfo>(); j = 0; for (i = 0; i < strItems_Alias.Count; i++) { try { itemclsPointInfo = new clsPointInfo(); itmHandleClient = j; sPLC_Adr = strItems_adrPLC[i]; // "LOCATION0 PLC13:DB2,STRING66,8" // S7:[PLC13]DB2,STRING66,8 sAlias = strItems_Alias[i]; sGroup = strItems_Groups[i]; sDesc = strItems_Desc[i]; //k = sPLC_Adr.IndexOf("STRING"); k = sPLC_Adr.IndexOf("CHAR"); if (k < 0) { itemclsPointInfo.iDataLength = 0; } else { string[] sSplitted = sPLC_Adr.Trim().Split(','); itemclsPointInfo.iDataLength = Convert.ToInt32(sSplitted[sSplitted.GetLength(0) - 1].Trim()); } OPCItemDef[] aD = new OPCItemDef[1]; aD[0] = new OPCItemDef(sPLC_Adr,true,itmHandleClient,VarEnum.VT_EMPTY); OPCItemResult[] arrRes; theGrp.AddItems(aD,out arrRes); if (arrRes == null) { AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " arrRes == null"); //continue; } else { if (arrRes[0].Error != HRESULTS.S_OK) { AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " arrRes[0].Error != HRESULTS.S_OK"); //continue; } else { itmHandleServer = arrRes[0].HandleServer; itmAccessRights = arrRes[0].AccessRights; itmTypeCode = VT2TypeCode(arrRes[0].CanonicalDataType); txbActPoint.Text = sPLC_Adr; txtItemDataType.Text = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType); //if ((itmAccessRights & OPCACCESSRIGHTS.OPC_READABLE) != 0) { // int cancelID; // theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE,7788,out cancelID); // } //else { // txtItemValue.Text = "no read access"; // AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " no read access"); // } if (itmTypeCode != TypeCode.Object) { // Object=failed! ////AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " Object=failed"); // check if write is permitted if ((itmAccessRights & OPCACCESSRIGHTS.OPC_WRITEABLE) != 0) { //btnItemWrite.Enabled = true; ////AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " write is permitted"); } } itemstrings[0] = sAlias; itemstrings[1] = sPLC_Adr; //listOpcView.Items.Add(new ListViewItem(itemstrings,0)); itemclsPointInfo.sAlias = sAlias; string[] sEvtNameSplitted = sAlias.Split('_'); //for (k = 0; k < sEvtNameSplitted.GetLength(0); k++){ // AddLogg("DEBUG sEvtNameSplitted[" + k + "] = '" + sEvtNameSplitted[k]+"'"); //} if (sEvtNameSplitted[0] == "ACT" || sEvtNameSplitted[0] == "SP") { // Fool-Proof if (sEvtNameSplitted[1] == "C1" || sEvtNameSplitted[1] == "C2" || sEvtNameSplitted[1] == "C3") { itemclsPointInfo.iCnvNr = Convert.ToInt32(sEvtNameSplitted[1].Substring(1,1)); if (sEvtNameSplitted[0] == "SP"){ itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(6,itemclsPointInfo.sAlias.Length -6); } else{ itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(7,itemclsPointInfo.sAlias.Length - 7); } } else { itemclsPointInfo.iCnvNr = 0; if (sEvtNameSplitted[0] == "SP"){ itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(3,itemclsPointInfo.sAlias.Length -3); } else{ itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(4,itemclsPointInfo.sAlias.Length - 4); } } if (sEvtNameSplitted[0] == "SP"){ itemclsPointInfo.bIsSetPoint = true; } } else { AddLogg("DoInit:AddItem" + i + " " + sAlias + " �� ���������� � 'ACT_'/'SP_'"); } itemclsPointInfo.sPLC_Adr = sPLC_Adr; itemclsPointInfo.sDesc = sDesc; itemclsPointInfo.CanonicalDataType = arrRes[0].CanonicalDataType; itemclsPointInfo.sCanonicalDataType = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType); itemclsPointInfo.AccessRights = arrRes[0].AccessRights; itemclsPointInfo.itmTypeCode = itmTypeCode; pointLi.Add(itemclsPointInfo); cbCtrlAliases.Items.Add(sAlias + " " + sPLC_Adr); // ���� � ������ ���� //k = groupLi.FindIndex(delegate(clsGroupInfo lfdGroupInfo) { // ** ��� ��������� // return lfdGroupInfo.sName == sGroup; // } // ); string @group = sGroup; // ** ����� !!! k = groupLi.FindIndex(lfdGroupInfo => lfdGroupInfo.sName == group); if (k < 0) { // ��� ������ ����� ������, ��������������, ��� TAKE_OVER !! itemclsGroupInfo = new clsGroupInfo(); itemclsGroupInfo.sName = sGroup; groupLi.Add(itemclsGroupInfo); pointLi[pointLi.Count - 1].bIsTakeOver = true; // rueckwerts in PointLi schreiben } k = pointLi.Count - 1; // wegen DEBUG pointLi[k].iIdxInGroupLi = groupLi.Count - 1; // gehoert zu dieser Gruppe;rueckwerts in PointLi schreiben k = groupLi.Count - 1; // wegen DEBUG groupLi[k].iItemsCnt++; // so viele Punkte in der Gruppe groupLi[k].iIdxInPointLi.Add(pointLi.Count - 1); // eigentlisch Punkte(als pointLi[Index]) DoStoreToOracle(j); // AnfangsZustand in Oracle generieren AddLogg("�������� Pkt" + j + " group '" + groupLi[k].sName + "',Alias'" + sAlias + "' " + sPLC_Adr); j++; } } } catch (/*COMException*/ Exception comExc) { //MessageBox.Show(this,"AddItem " + sPLC_Adr + " OPC error!","DoInit",MessageBoxButtons.OK,MessageBoxIcon.Warning); AddLogg("DoInit:AddItem" + i + " '" + sPLC_Adr + "' OPC exception:" + comExc.Message); //return false; continue; } } int cancelID; theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE,7788,out cancelID); } catch (Exception e) { // exceptions MUST be handled //MessageBox.Show(this,"init error! " + e.ToString(),"Exception",MessageBoxButtons.OK,MessageBoxIcon.Warning); AddLogg("DoInit:AddItem" + i + " '" + sPLC_Adr + "' init error! " + e.Message); AddLogg("DoInit: Ende - false"); return false; } AddLogg("DoInit: Ende-true,� pointLi " + pointLi.Count + " � groupLi " + groupLi.Count); mainGate = new MainGateClient(new InstanceContext(new DummyListener())); return true; }
private void buttProbeSincRead_Click(object sender,EventArgs e) { OpcGroup theSinchroGrp; // ���������� ��� ���, ���� ������ OPCItemDef[] aD; // ��(���������)�����, ���� ������ OPCItemResult[] arrRes; // ����������� ���������� OPCItemState[] sts; // �������(���������)�� ������ ��������� ����� int[] serverhandles; // ��� ������ ����� �.�.���� ���������� string sPLC_Adr = "S7:[PLC01]DB2,int102"; string[] sPLC_AdrLi = { "S7:[PLC01]DB2,int102" // ACT_WATCHDOG01_PLC ,"S7:[PLC11]DB2,int48" // ACT_C1_WATCHDOG1_PLC ,"S7:[PLC21]DB2,int48" // ACT_C2_WATCHDOG1_PLC ,"S7:[PLC31]DB2,int48" // ACT_C3_WATCHDOG1_PLC }; string sErgValue = ""; bool bErg = true; clsPointInfo[] probePointLi = new clsPointInfo[sPLC_AdrLi.GetLength(0)]; try { theSinchroGrp = theSrv.AddGroup("OPCdotNET-Sinchro",true,60000); aD = new OPCItemDef[1]; aD[0] = new OPCItemDef(sPLC_Adr,true,itmHandleClient,VarEnum.VT_EMPTY); theGrp.AddItems(aD,out arrRes); if (arrRes == null) { MessageBox.Show(this,"AddItems" + i,"arrRes == null",MessageBoxButtons.OK,MessageBoxIcon.Warning); } else { if (arrRes[0].Error != HRESULTS.S_OK) { MessageBox.Show(this,"AddItems" + i,"arrRes[0].Error != HRESULTS.S_OK",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } //// add event handler for data changes //theSinchroGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange); //theSinchroGrp.WriteCompleted += new WriteCompleteEventHandler(this.theGrp_WriteComplete); sts = new OPCItemState[1]; // �� ����, ����� �� ������������� itmHandleServer = 0; // ��� �� 0. ������� � "sPLC_AdrLi" serverhandles = new int[1] { itmHandleServer }; theSinchroGrp.Read(OPCDATASOURCE.OPC_DS_DEVICE,serverhandles,out sts); // ������ ��������� ����� ��������� ����� foreach (OPCItemState s in sts) { try { if (HRESULTS.Succeeded(s.Error)) { if (s.HandleClient < sPLC_AdrLi.GetLength(0)) { if (s.DataValue != null) { setTextInTxbViaDelegate(ref txtItemQual,OpcGroup.QualityToString(s.Quality)); setTextInTxbViaDelegate(ref txtItemTimeSt,DateTime.FromFileTime(s.TimeStamp).ToString()); probePointLi[s.HandleClient].opcItem = s; if (s.DataValue.GetType() == typeof(SByte[])) { probePointLi[s.HandleClient].sDataValue = ConvertToRus((SByte[])s.DataValue,probePointLi[s.HandleClient].iDataLength); setTextInTxbViaDelegate(ref txtItemValue,probePointLi[s.HandleClient].sDataValue); sErgValue = probePointLi[s.HandleClient].sDataValue; } else { if (s.DataValue.GetType() == typeof(DateTime)) { probePointLi[s.HandleClient].dtDataValue = Convert.ToDateTime(s.DataValue); setTextInTxbViaDelegate(ref txtItemValue,LoggDateToString(probePointLi[s.HandleClient].dtDataValue)); sErgValue = LoggDateToString(probePointLi[s.HandleClient].dtDataValue); } else { probePointLi[s.HandleClient].iDataValue = Convert.ToInt32(s.DataValue); setTextInTxbViaDelegate(ref txtItemValue,probePointLi[s.HandleClient].iDataValue.ToString()); sErgValue = probePointLi[s.HandleClient].iDataValue.ToString(); } } AddLogg("Pkt" + s.HandleClient + "," + s.DataValue.GetType() + " ��������: '" + sErgValue + "'"); } else { AddLogg("s.HandleClient{" + s.HandleClient + "}>=probePointLi.Count{" + sPLC_AdrLi.GetLength(0) + "}"); } } } else { setTextInTxbViaDelegate(ref txtItemTimeSt,DateTime.FromFileTime(s.TimeStamp).ToString()); setTextInTxbViaDelegate(ref txtItemQual,"error"); setTextInTxbViaDelegate(ref txtItemValue,"ERROR 0x" + s.Error.ToString("X")); AddLogg("s.HandleClient{" + s.HandleClient + "} ERROR 0x" + s.Error.ToString("X")); } } catch (Exception exc) { AddLogg("Pkt" + s.HandleClient + "," + ((s.DataValue == null) ? " DataValue==null" : s.DataValue.GetType().ToString()) + " " + exc.Message); } } GC.Collect(); // just for fun } catch (Exception eXc) { MessageBox.Show(this,"OPC Exception!",eXc.Message,MessageBoxButtons.OK,MessageBoxIcon.Warning); return; } }
// das Gelesene rausschmeissen private bool DoStoreToOracle(int iIdxInPointLi) { bool bErg = true; try { if (pointLi[iIdxInPointLi].bIsTakeOver) { // jetzt: Action! foreach (var iLfdIdxInPointLi in groupLi[pointLi[iIdxInPointLi].iIdxInGroupLi].iIdxInPointLi) { try { itemclsPointInfo = pointLi[iLfdIdxInPointLi]; // wegen DEBUG pointLi[iLfdIdxInPointLi].iReadCnt = 0; // merker: jetzt wurde gespeichert if (OraConn == null) { OraConn = new OracleConnection { ConnectionString = "data source=SMKBOFnew;user id=SMK;password=SMK;" }; } else { OraConn.Close(); } OraCmd = OraConn.CreateCommand(); OraCmd.CommandText = "pck_opc.StorePLC_Value"; OraCmd.CommandType = CommandType.StoredProcedure; OraCmd.Parameters.Add(new OracleParameter("sGroupInp",OracleType.VarChar)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("sAliasInp",OracleType.VarChar)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("sPLC_AdrInp",OracleType.VarChar)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("iIsTakeOverInp",OracleType.Number)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("iTypeCodeInp",OracleType.Number)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("sDataValueInp",OracleType.VarChar)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("dtDataValueInp",OracleType.DateTime)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("iDataValueInp",OracleType.Number)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("dtReadingDateInp",OracleType.DateTime)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("iReadCntInp",OracleType.Number)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("iReadCntMaxInp",OracleType.Number)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("dtMaxInp",OracleType.DateTime)).Direction = ParameterDirection.Input; OraCmd.Parameters.Add(new OracleParameter("sDescInp",OracleType.VarChar)).Direction = ParameterDirection.Input; OraCmd.Parameters["sGroupInp"].Value = groupLi[itemclsPointInfo.iIdxInGroupLi].sName; OraCmd.Parameters["sAliasInp"].Value = itemclsPointInfo.sAlias; OraCmd.Parameters["sPLC_AdrInp"].Value = itemclsPointInfo.sPLC_Adr; OraCmd.Parameters["iIsTakeOverInp"].Value = (itemclsPointInfo.bIsTakeOver) ? 1 : 0; OraCmd.Parameters["sDescInp"].Value = itemclsPointInfo.sDesc; OraCmd.Parameters["iTypeCodeInp"].Value = 2; OraCmd.Parameters["sDataValueInp"].Value = string.Empty; OraCmd.Parameters["dtDataValueInp"].Value = DateTime.MinValue; OraCmd.Parameters["iDataValueInp"].Value = int.MinValue; OraCmd.Parameters["dtReadingDateInp"].Value = DateTime.MinValue; OraCmd.Parameters["iReadCntInp"].Value = int.MinValue; OraCmd.Parameters["iReadCntMaxInp"].Value = int.MinValue; OraCmd.Parameters["dtMaxInp"].Value = DateTime.MinValue; if (itemclsPointInfo.opcItem != null) { if (itemclsPointInfo.opcItem.DataValue != null) { if (itemclsPointInfo.opcItem.DataValue.GetType() == typeof(SByte[])) { OraCmd.Parameters["iTypeCodeInp"].Value = 0; OraCmd.Parameters["sDataValueInp"].Value = itemclsPointInfo.sDataValue; } else { if (itemclsPointInfo.opcItem.DataValue.GetType() == typeof(DateTime)) { OraCmd.Parameters["iTypeCodeInp"].Value = 1; OraCmd.Parameters["dtDataValueInp"].Value = itemclsPointInfo.dtDataValue; } else { OraCmd.Parameters["iTypeCodeInp"].Value = 2; OraCmd.Parameters["iDataValueInp"].Value = itemclsPointInfo.iDataValue; } } OraCmd.Parameters["dtReadingDateInp"].Value = DateTime.FromFileTime(itemclsPointInfo.opcItem.TimeStamp); OraCmd.Parameters["iReadCntInp"].Value = itemclsPointInfo.iReadCnt; OraCmd.Parameters["iReadCntMaxInp"].Value = itemclsPointInfo.iReadCntMax; OraCmd.Parameters["dtMaxInp"].Value = itemclsPointInfo.dtMax; } } OraCmd.Connection.Open(); OraCmd.ExecuteNonQuery(); } catch (Exception ex) { bErg = false; sP = "������ �������: " + ((OraCmd.CommandText == null) ? "OraCmd.CommandText==null" : OraCmd.CommandText) + " ,Exc: " + ex.Message; AddLogg(sP); } if (OraConn != null) OraConn.Close(); } } } catch (Exception ex) { bErg = false; sP = "������ ������ pointLi[" + iIdxInPointLi + "],Exc: " + ex.Message; AddLogg(sP); } return bErg; }