示例#1
0
        private void buttonFalse_Click(object sender, RoutedEventArgs e)
        {
            using (var client = new TwinCAT.Ads.TcAdsClient())
            {
                try
                {
                    client.Connect(851);

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

                    ds.Position = 0;

                    bw.Write(Convert.ToInt32(textBox.Text));

                    // writes a DINT to PLC
                    client.Write(0x4020, 0x0001, ds);

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

                    client.Dispose();
                }
                catch (TwinCAT.Ads.AdsErrorException ex)
                {
                    MessageBox.Show("PLC Control Error: \r\n\r\n" + ex.Message);
                }
                catch (System.FormatException ex)
                {
                    textBoxPLCSetPosition.Text = "0";
                    MessageBox.Show("Invalid float value for Position. ErrMsg: \r\n\r\n" + ex.Message);
                }
            }
        }
示例#2
0
        private void textBoxPLCSetPosition_TextChanged(object sender, TextChangedEventArgs e)
        {
            using (var client = new TwinCAT.Ads.TcAdsClient())
            {
                try
                {
                    client.Connect(851);
                    if (textBoxPLCSetPosition.Text == "")
                    {
                        return;
                    }
                    double dPos = Convert.ToDouble(textBoxPLCSetPosition.Text);


                    //client.WriteSymbol("MyGVL.MyBoolVar", false, reloadSymbolInfo: true);
                    client.WriteSymbol("P_Motion2.stAxis.PlcToNc.ExtSetPos", dPos, reloadSymbolInfo: true);

                    client.Dispose();
                }
                catch (TwinCAT.Ads.AdsErrorException ex)
                {
                    MessageBox.Show("PLC Control Error: \r\n\r\n" + ex.Message);
                    //Application.Current.Shutdown();
                }
                catch (System.FormatException ex)
                {
                    textBoxPLCSetPosition.Text = "0";
                    MessageBox.Show("Invalid float value for Position. ErrMsg: \r\n\r\n" + ex.Message);
                }
            }
        }
示例#3
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();
            }
        }