Esempio n. 1
0
        private void buttonTrue_Click(object sender, RoutedEventArgs e)
        {
            using (var client = new TwinCAT.Ads.TcAdsClient())
            {
                client.Connect(851);


                // creates a stream with a length of 4 byte
                AdsStream    ds = new AdsStream(4);
                BinaryReader br = new BinaryReader(ds);

                // reads a DINT from PLC
                client.Read(0x4020, 0x0001, ds);

                ds.Position    = 0;
                label1.Content = br.ReadInt32().ToString();

                //client.WriteSymbol("MyGVL.MyBoolVar", true, reloadSymbolInfo: true);
                client.WriteSymbol("P_Motion2.ati_xStart", true, reloadSymbolInfo: true);

                client.Dispose();
            }
        }
        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);
        }