Exemplo n.º 1
0
 public Tag(PlcVar itemProp, string itemName, object itemValue)
 {
     this.ItemProp  = itemProp;
     this.ItemName  = itemName;
     this.ItemValue = itemValue;
 }
        public static tcFunctionResult tcCreateHandle()
        {
            if (!tcClient.IsConnected)
            {
                return(tcFunctionResult.TC_NOT_CONNECTED);
            }

            bool _io_ok = false, _axis_ok = false;

            #region Creating PLC IO handles
            IEnumerable <XElement> PlcVars = tcConnectorConfig.Root.Elements("PlcVar");
            foreach (XElement PlcVar in PlcVars)
            {
                tcPlcVar PlcVarBuffer = new tcPlcVar();
                try
                {
                    PlcVarBuffer.Handle = tcClient.CreateVariableHandle(PlcVar.Attribute("Name").Value);
                    if (PlcVarBuffer.Handle == 0)
                    {
                        return(tcFunctionResult.TC_FAIL_TO_CREATE_HANDLE);
                    }
                    PlcVarBuffer.VariableName = PlcVar.Attribute("Name").Value;
                    PlcVarBuffer.VariableType = PlcVar.Attribute("Type").Value;
                    PlcVarBuffer.DataSize     = int.Parse(PlcVar.Attribute("Size").Value);
                    PlcVarBuffer.Count        = int.Parse(PlcVar.Attribute("Count").Value);
                    PlcVarBuffer.Data         = new object();
                    PlcVarBuffer.Tag          = PlcVar.Attribute("Tag").Value;
                    PlcVarBuffer.Index        = int.Parse(PlcVar.Attribute("Index").Value);
                    tcVarList.Add(PlcVarBuffer);
                    _io_ok = true;
                }
                catch (Exception ex)
                {
                    LogMessage(string.Format("{0}\t: {1}", "Error handling PlcConfig.xml for IO " + PlcVar.Attribute("Name").Value, ex.Message));
                }
            }
            #endregion

            #region Creating PLC Axis handles
            XElement AxsVar = tcConnectorConfig.Root.Element("AxsVar");
            if (AxsVar == null)
            {
                Axis_MaxAxes = 0;
                LogMessage(string.Format("{0}\t: {1}", "Report", "No axis specified in config file"));
            }
            else
            {
                try
                {
                    int AxCount = tcClient.CreateVariableHandle(AxsVar.Attribute("Count").Value);
                    Axis_MaxAxes = (short)tcClient.ReadAny(AxCount, typeof(short));
                    tcClient.DeleteVariableHandle(AxCount);
                    tcAxFeedback = new _Axis_PlcToHmi[Axis_MaxAxes];
                    tcAxCommand  = new _Axis_HmiToPlc[Axis_MaxAxes];
                    for (int k = 0; k < Axis_MaxAxes; k++)
                    {
                        tcAxFeedback[k]        = new _Axis_PlcToHmi();
                        tcAxFeedback[k].handle = tcClient.CreateVariableHandle(AxsVar.Attribute("PlcToHmi").Value + "[" + (k + 1).ToString() + "]");
                        tcAxFeedback[k].size   = short.Parse(AxsVar.Attribute("P2HSize").Value);
                        tcAxCommand[k]         = new _Axis_HmiToPlc();
                        tcAxCommand[k].handle  = tcClient.CreateVariableHandle(AxsVar.Attribute("HmiToPlc").Value + "[" + (k + 1).ToString() + "]");
                        tcAxCommand[k].size    = short.Parse(AxsVar.Attribute("H2PSize").Value);
                    }
                    _axis_ok = true;
                }
                catch (Exception ex)
                {
                    LogMessage(string.Format("{0}\t: {1}", "Error", "Cannot create handle for axis " + ex.Message));
                }
            }
            #endregion

            if (_io_ok && _axis_ok)
            {
                LogMessage(string.Format("{0}\t: {1}", "Report", "Handles created."));
                return(tcFunctionResult.TC_SUCCESS);
            }
            else
            {
                LogMessage(string.Format("{0}\t: {1}", "Report", "Not all handles created."));
                return(tcFunctionResult.TC_SUCCESS);
            }
        }