public static void tcDispose() { int totalCount = tcVarList.Count; for (int i = 0; i < totalCount; i++) { int j = totalCount - (i + 1); if (tcVarList[j].Handle != 0) { tcClient.DeleteVariableHandle(tcVarList[j].Handle); } tcVarList.RemoveAt(j); } for (int i = 0; i < Axis_MaxAxes; i++) { if (tcAxFeedback[i].handle != 0) { tcClient.DeleteVariableHandle(tcAxFeedback[i].handle); } tcAxFeedback[i].handle = 0; if (tcAxCommand[i].handle != 0) { tcClient.DeleteVariableHandle(tcAxCommand[i].handle); } tcAxCommand[i].handle = 0; } tcClient.Dispose(); LogMessage(string.Format("{0}\t: {1}", "Report", "Device connection terminated.")); }
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); } } }
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(); } }
private void Window_Closing(object sender, CancelEventArgs e) { clientBool.Dispose(); }