public static tcFunctionResult tcConnect() { try { tcConnectorConfig = XDocument.Load(tcConfigPath); } catch (Exception ex) { LogMessage(string.Format("{0}\t: {1}", "Unable to load PlcConfig.xml", ex.Message)); return(tcFunctionResult.TC_FAIL_TO_LOAD_PLC_CONFIG); } XElement tcPLC = tcConnectorConfig.Root;//("Plc"); tcClient = new TcAdsClient(); try { tcClient.Connect(tcPLC.Attribute("AmsNetId").Value, int.Parse(tcPLC.Attribute("AmsPort").Value)); } catch (Exception ex) { LogMessage(string.Format("{0}\t: {1}", "Unable to connect to device", ex.Message)); return(tcFunctionResult.TC_FAIL_TO_CONNECT_DEVICE); } LogMessage(string.Format("{0}\t: {1}", "Report", "Device connected.")); return(tcFunctionResult.TC_SUCCESS); }
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); } } }
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); } } }
public MainWindow() { InitializeComponent(); /// casting the content into panel Panel mainContainer = (Panel)this.Content; /// GetAll UIElement UIElementCollection element = mainContainer.Children; /// casting the UIElementCollection into List List <FrameworkElement> lstElement = element.Cast <FrameworkElement>().ToList(); /// Getting all Control from list var lstControl = lstElement.OfType <Control>(); foreach (var control in lstControl) { this.communicationManager .Register(control as Control); } this.components = new System.ComponentModel.Container(); this.loopTime = new System.Windows.Forms.Timer(this.components); this.loopTimer = new System.Windows.Forms.Timer(this.components); // loopTime // loopTime.Tick += new System.EventHandler(this.loopTime_Tick); this.loopTimer.Tick += (s, e) => { this.communicationManager.Poll(); }; this.clientBool = new TwinCAT.Ads.TcAdsClient(); this.clientBool.Connect(851); this.loopTimer.Enabled = true; this.loopTime.Enabled = true; this.loopTimer.Start(); }
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(); } }