public static tcFunctionResult tcReadAll() { if (!tcClient.IsConnected) { return(tcFunctionResult.TC_NOT_CONNECTED); } for (int i = 0; i < tcVarList.Count; i++) { AdsStream tcDataStream = new AdsStream(tcVarList[i].DataSize); AdsBinaryReader tcStreamReader = new AdsBinaryReader(tcDataStream); try { tcClient.Read(tcVarList[i].Handle, tcDataStream); } catch (Exception ex) { LogMessage(string.Format("{0}\t: {1}", "Failed to read from device", ex.Message)); return(tcFunctionResult.TC_FAIL_TO_READ_DATA); } tcPlcVar buffer = tcVarList[i]; switch (tcVarList[i].VariableType.ToLower()) { case "bool": buffer.Data = tcStreamReader.ReadBoolean(); break; case "arbool": bool[] boolBuffer = new bool[buffer.Count]; for (int j = 0; j < boolBuffer.Length; j++) { boolBuffer[j] = tcStreamReader.ReadBoolean(); } buffer.Data = (object)boolBuffer; break; case "int": buffer.Data = tcStreamReader.ReadInt16(); break; case "arint": short[] shortBuffer = new short[buffer.Count]; for (int j = 0; j < shortBuffer.Length; j++) { shortBuffer[j] = tcStreamReader.ReadInt16(); } buffer.Data = (object)shortBuffer; break; case "string": buffer.Data = tcStreamReader.ReadPlcString(255); break; case "real": buffer.Data = tcStreamReader.ReadSingle(); break; case "lreal": buffer.Data = tcStreamReader.ReadDouble(); break; } tcVarList[i] = buffer; tcStreamReader.Close(); } return(tcFunctionResult.TC_SUCCESS); }
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); } }