//------------------------------------------------------ //wird aufgerufen, wenn das Feld 'schnell' markiert wird //is activated when the 'fast' field is marked //------------------------------------------------------ private void optSpeedFast_Click(object sender, EventArgs e) { try { tcClient.WriteAny(hSwitchWrite, true); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void WriteWithHandle <T>(int variableHandle, T value) { if (typeof(T) == typeof(string)) { // ReSharper disable once PossibleNullReferenceException _client.WriteAny(variableHandle, value, new[] { (value as string).Length }); } else { _client.WriteAny(variableHandle, value); } }
public void StopProcess() { try { adsClient.WriteAny(hIsRunning, false); } catch (Exception err) { Console.WriteLine(err.Message); Console.ReadLine(); } }
private void buttonSetSR1_Click(object sender, EventArgs e) { // Laitetaan Start päälle adsClient.WriteAny(startHandle, true); // Odotetaan 0.5 s System.Threading.Thread.Sleep(500); // laitetaan Stop päälle adsClient.WriteAny(startHandle, false); buttonConnect.Enabled = false; buttonSetSR1.Enabled = false; buttonResetSR1.Enabled = true; }
private void button2_Click(object sender, EventArgs e) { try { adsClient.WriteAny(hbool1, Boolean.Parse(textBox1.Text)); adsClient.WriteAny(hint1, byte.Parse(textBox2.Text)); adsClient.WriteAny(hstr1, textBox3.Text, new int[] { 5 }); adsClient.WriteAny(hcomplexStruct, GetStructFromControls()); adsClient.WriteAny(hlreal1, Double.Parse(textBox4.Text)); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public bool WriteBool(string pou, string variableName, bool value) { try { var hVar = _tcClient.CreateVariableHandle(pou + "." + variableName); _tcClient.WriteAny(hVar, value); _tcClient.DeleteVariableHandle(hVar); return(true); } catch (AdsErrorException exp) { Debug.LogError("TC Write Error - Bool" + exp.Message); } return(false); }
//TcAdsClient tcClient = new TcAdsClient(); //AdsStream dataStream = new AdsStream(4); //AdsBinaryReader binReader = new AdsBinaryReader(dataStream); public bool writeToPLCInt(string sName, int iValue) { TcAdsClient tcClient = new TcAdsClient(); AdsStream dataStream = new AdsStream(4); AdsBinaryReader binReader = new AdsBinaryReader(dataStream); int iHandle = 0; tcClient.Connect(851); iHandle = tcClient.CreateVariableHandle(sName); try { tcClient.WriteAny(iHandle, iValue); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { tcClient.DeleteVariableHandle(iHandle); tcClient.Dispose(); } return(true); }
/// <summary> /// Sets the state of the device in the PLC /// </summary> /// <param name="vDeviceName">string, Name of device that matches variable name in PLC</param> /// <param name="vValue">object, value to send to the PLC</param> public void SetState(string vDeviceName, object vValue) { // Find Index of the Device int vIndex = vDevices.FindIndex(ni => ni.Name == vDeviceName); // Write the value using the ADS write handler vAdsClient.WriteAny(vDevices[vIndex].WriteHndl, vValue); }
internal void WriteVariable <TValue>(int handle, TValue value) { try { _client.WriteAny(handle, value); } catch (AdsException ex) { throw new ControllerCommunicationException("Unable to write to variable due to issues with controller connection.", ex); } }
private void btnWrite_Click(object sender, EventArgs e) { try { client.WriteAny(hValue, Convert.ToUInt16(tbWrite.Text)); } catch (Exception err) { MessageBox.Show(err.Message); } }
public bool WriteValue(int paramAddress, Object newValue, string DebugTag = "") { try { if (IsBeckhoff) { TcAdsClient Kid = (TcAdsClient)this.Client; if (bool.Parse(newValue.ToString()) == true) { newValue = 1; } if (bool.TryParse(newValue.ToString(), out bool b) && b == false) { newValue = 0; } if (int.Parse(newValue.ToString()) == 1 || int.Parse(newValue.ToString()) == 0) { Kid.WriteAny(paramAddress, byte.Parse(newValue.ToString())); } else { if (paramAddress == Gantry1BeaconNumber || paramAddress == Gantry2BeaconNumber || paramAddress == Joystick1SelectedScreenber || paramAddress == Joystick2SelectedScreenber) { Kid.WriteAny(paramAddress, short.Parse(newValue.ToString())); } else { Kid.WriteAny(paramAddress, int.Parse(newValue.ToString())); } } } return(true); } catch { return(false); } }
/// <summary> /// 写字符串-(PLC定义的STRING类型) /// </summary> /// <param name="socket"></param> /// <param name="address">地址格式(MAIN.dint1 (MAIN目录标签 ,dint1在MAIN这个目录定义的dint1这个名)) </param> /// <param name="values">值</param> /// <returns></returns> public bool WriteString(TcAdsClient tac, string address, string values) { try { tac.WriteAny(tac.CreateVariableHandle(address), values, new int[] { values.Length }); return(true); } catch { return(false); } }
/// <summary> /// 写单个D-双精浮点数(PLC定义的LREAL类型) /// </summary> /// <param name="socket"></param> /// <param name="address">地址格式(MAIN.dint1 (MAIN目录标签 ,dint1在MAIN这个目录定义的dint1这个名)) </param> /// <param name="values">值</param> /// <returns></returns> public bool WriteSingleDoubleD(TcAdsClient tac, string address, double values) { try { tac.WriteAny(tac.CreateVariableHandle(address), values); return(true); } catch { return(false); } }
/// <summary> /// 写单个M-布尔(PLC定义的Bool类型) /// </summary> /// <param name="socket"></param> /// <param name="address"></param> /// <param name="values">值</param> /// <returns></returns> public bool WriteSingleBoolM(TcAdsClient tac, string address, int values) { try { tac.WriteAny(tac.CreateVariableHandle(address), values == 1 ? true : false); return(true); } catch { return(false); } }
/// <summary> /// 写单个D-32位无符号整数(PLC定义的USINT类型) /// </summary> /// <param name="socket"></param> /// <param name="address">地址格式(MAIN.dint1 (MAIN目录标签 ,dint1在MAIN这个目录定义的dint1这个名)) </param> /// <param name="values">数值</param> /// <returns></returns> public bool WriteSingleUsintD(TcAdsClient tac, string address, int values) { try { tac.WriteAny(tac.CreateVariableHandle(address), Convert.ToByte(values)); return(true); } catch { return(false); } }
public static void Writestring(int variableHandle, string content) { try { Sendtemp = content; adsClient.WriteAny(variableHandle, content, new int[] { content.Length }); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 写单个D-INT(PLC定义的INT和DINT类型) /// </summary> /// <param name="socket"></param> /// <param name="address">地址格式(MAIN.dint1 (MAIN目录标签 ,dint1在MAIN这个目录定义的dint1这个名)) </param> /// <param name="values">数值</param> /// <returns></returns> public bool WriteSingleIntD(TcAdsClient tac, string address, int values) { try { int NUMBER = tac.CreateVariableHandle(address); tac.WriteAny(tac.CreateVariableHandle(address), values); return(true); } catch { return(false); } }
public void writeAny(String plcVarName, Object value) { if (!User.GetInstance().GetCurrentUserInfo().IsLogin) { MessageBox.Show("您需要登录才能修改!"); Dictionary <String, Object> bundle = new Dictionary <string, object>(); bundle.Add(PageUserRegister.USER_PAGE, PageUserRegister.ID_LOGIN); PageDataExchange.getInstance().putExtra(PageUserRegister.TAG, bundle); Utils.NavigateToPage(MainWindow.sFrameReportName, PageUserRegister.TAG); return; } else if (".test_run_unprotected".Equals(plcVarName) && !(User.GetInstance().GetCurrentUserInfo().UserLevel >= User.USER_PREVILIDGE_DEBUGGER)) { MessageBox.Show("您需要更高权限才能修改!"); return; } try { Boolean flag = true; adsClient.WriteAny(mWriteFlagHandle, flag); Object originalValue; plcVarUserdataMap.TryGetValue(plcVarName, out originalValue); int handle; plcVarHAccessMap.TryGetValue(plcVarName, out handle); adsClient.WriteAny(handle, value); String description; plcVarDescriptionMap.TryGetValue(plcVarName, out description); String content = String.Format("参数 {0} 被用户 {1} 从 {2} 修改为 {3}", description, User.GetInstance().GetCurrentUserInfo().Account, originalValue, value); Log.write(Log.CATEGOTY_PARAMETER, content); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void btnWrite_Click(object sender, System.EventArgs e) { try { //write by handle //the second parameter is the object to be written to the PLC variable adsClient.WriteAny(htest1, int.Parse(tbDint1.Text)); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 写入PLC /// </summary> /// <param name="hand"></param> /// <param name="data"></param> public void WriterData(string hand, object data) { try { int _hand = 0; _hand = _client.CreateVariableHandle(hand); _client.WriteAny(_hand, data); } catch (Exception ex) { msg = "写入数据失败!原因:" + ex.Message; return; } }
static void Main(string[] args) { ads = new TcAdsClient(); ads.Connect(851); CreateHandles(); ReadVariables(); ExcelInit(); ExcelWriteArray(ReadTimeArray(), column_to_write, columnname); ExcelClose(); Console.WriteLine("array successfully written to excel"); Thread.Sleep(2000); ads.WriteAny(hReady, true); }
static void Main(string[] args) { // Create the ADS Client using (client = new TcAdsClient()) { // Establish Connection client.Connect(new AmsAddress("10.1.2.95.1.1", 851)); int handle = client.CreateVariableHandle("PRG_AIS.stAds"); AdsClass ads = (AdsClass)client.ReadAny(handle, typeof(AdsClass)); ads.boolArr[0] = 1; client.WriteAny(handle, ads); Console.ReadLine(); } }
private void lblStart_Click(object sender, EventArgs e) { // AdsStream btnStream = new AdsStream(1); AdsBinaryWriter btnWriter = new AdsBinaryWriter(btnStream); try { btnWriter.Write(true); client.Write(hStartUI, btnStream); //------adsClient的WriteAny方法 client.WriteAny(hPauseUI, false); client.WriteAny(hStopUI, false); //------adsClient的WriteAny方法 } catch (Exception err) { MessageBox.Show("write start value to PLC: " + err.Message); } lblPause.BackColor = Color.Empty; lblStart.BackColor = Color.Green; lblStop.BackColor = Color.Empty; }
private void cbDebugging_CheckedChanged(object sender, EventArgs e) { try { if (client.IsConnected) { if (cbDebugging.Checked) { client.WriteAny(debugVarHandle, true); debugState = true; } else { client.WriteAny(debugVarHandle, false); debugState = false; } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show("Error accessing variable.\nPlease make sure the variable name in XML file is correct."); } }
//往TwinCAT写入数值 private void mTwinCATCoil_CoilUpdate(object sender, EventArgs e) { try { TwinCATCoil temp = sender as TwinCATCoil; if (temp.CoilHandle == 0) { temp.CoilHandle = adsClient.CreateVariableHandle(temp.CoilName); } if (temp.CoilGroup != 0)//如果是数组~ { //adsClient.WriteAny(temp.CoilHandle, a, new int[] { temp.CoilGroup });//这样不行~ dynamic dynamicObj = Convert.ChangeType(temp.CoilValue, temp.CoilType); int size = 0; if (dynamicObj is bool[])//否则得出结果是4字节 { size = 1; } else { size = Marshal.SizeOf(dynamicObj[0]); } AdsStream datastream = new AdsStream(size * temp.CoilGroup); //每个元素占用位 System.IO.BinaryWriter binwrite = new System.IO.BinaryWriter(datastream); datastream.Position = 0; for (int i = 0; i < temp.CoilGroup; i++) { binwrite.Write(dynamicObj[i]); } adsClient.Write(temp.CoilHandle, datastream); } else { adsClient.WriteAny(temp.CoilHandle, Convert.ChangeType(temp.CoilValue, temp.CoilType)); } } catch (Exception ex) { if (sender is TwinCATCoil) { Trace.Write(ex, "Update:" + (sender as TwinCATCoil).CoilName); } else { Trace.Write(ex, "Update"); } } }
public void beat() { try { while (true) { beatVal = !beatVal; tcClient.WriteAny(iHandle, beatVal); Thread.Sleep(1000); } } catch (ThreadAbortException ex) { tcClient.DeleteVariableHandle(iHandle); } }
private void button1_Click(object sender, EventArgs e) //Turnon { try { Form1_Load(sender, e); AdsStream adsStream = new AdsStream(30); // AdsBinaryReader reader = new AdsBinaryReader(adsStream); // adsClient.Read(intStart, adsStream); AdsStream adsStream2 = new AdsStream(30); // AdsBinaryReader reader2 = new AdsBinaryReader(adsStream2); // adsClient.Read(intStatus, adsStream2); AdsStream adsStream3 = new AdsStream(30); // blStart = reader.ReadBoolean(); // blStatus = reader2.ReadBoolean(); // if (blStart == false) // { label2.Text = adsClient.ReadAny(intStart, typeof(Boolean)).ToString(); label3.Text = adsClient.ReadAny(intStop, typeof(Boolean)).ToString(); label4.Text = adsClient.ReadAny(intStatus, typeof(Boolean)).ToString(); blStart = true; blStatus = true; blStop = false; AdsBinaryWriter write = new AdsBinaryWriter(adsStream); adsClient.WriteAny(intStart, blStart); AdsBinaryWriter writer2 = new AdsBinaryWriter(adsStream2); adsClient.WriteAny(intStatus, blStatus); AdsBinaryWriter writer3 = new AdsBinaryWriter(adsStream3); adsClient.WriteAny(intStop, blStop); label1.Text = "Đã bật đèn"; label5.Text = adsClient.ReadAny(intStart, typeof(Boolean)).ToString(); label6.Text = adsClient.ReadAny(intStop, typeof(Boolean)).ToString(); label7.Text = adsClient.ReadAny(intStatus, typeof(Boolean)).ToString(); // } // else //blStatus = true; // label1.Text = "TRUE nè"; adsClient.Dispose(); } catch (Exception err) { MessageBox.Show(err.Message); } }
private void BtnWrite_Click(object sender, EventArgs e) { try { //----write primitive data------- client.WriteAny(hint, short.Parse(txtINT.Text)); client.WriteAny(hBool, Boolean.Parse(txtBool.Text)); client.WriteAny(hDint1, int.Parse(txtDint.Text)); client.WriteAny(hByte1, byte.Parse(txtByte.Text)); client.WriteAny(hReal1, Single.Parse(txtReal.Text)); client.WriteAny(hLreal1, double.Parse(txtLreal.Text)); client.WriteAny(hstr1, txtStr1.Text, new int[] { 50 }); client.WriteAny(hstr2, txtStr2.Text, new int[] { 50 }); //----write primitive data------- //-----write the struct dataType--------------- client.WriteAny(hComplexStruct, GetStruct()); //-----write the struct dataType--------------- } catch (Exception err) { MessageBox.Show("Write all " + err.Message); } }
public static bool WriteAny(string VarAdress, object Message) { bool result = false; try { hVar = TwinCat3Client.CreateVariableHandle(VarAdress); TwinCat3Client.WriteAny(hVar, Message); result = true; TwinCat3Client.DeleteVariableHandle(hVar); hVar = 0; } catch (Exception err) { MessageBox.Show(err.Message); } return(result); }
static void Main(string[] args) { TcAdsClient adsClient = new TcAdsClient(); byte[] boolArray = new byte[100]; // Fill array with 010101010... for (int i = 0; i < 100; i++) { boolArray[i] = (i % 2 != 0) ? (byte)1 : (byte)0; } // Connect to PLC try { if (adsClient != null) { Console.WriteLine("Connecting to PC"); adsClient.Connect(851); } } catch (Exception err) { Console.WriteLine(err.Message); adsClient = null; } if (adsClient != null) { try { // Get the handle for the array int handle_array = adsClient.CreateVariableHandle("MAIN.boolArray"); // Write the array to PLC Console.WriteLine("Writing the array at handle: " + handle_array.ToString()); adsClient.WriteAny(handle_array, boolArray); } catch (Exception err) { Console.WriteLine(err.Message); } // The end Console.WriteLine("Done"); Thread.Sleep(3000); } }
public override void init() { adsClient = new TcAdsClient(); nameDict["MAIN.MDF_WhichStack"] = typeof(int); nameDict["MAIN.MDF_WhichDish"] = typeof(int); nameDict["MAIN.MDF_RunningError"] = typeof(int); nameDict["MAIN.MDF_online_state"] = typeof(int); nameDict["MAIN.MDF_Command_response"] = typeof(String); nameDict["MAIN.MDF_bar_code"] = typeof(string); nameDict["MAIN.MDF_Motor_1_cur"] = typeof(float); nameDict["MAIN.MDF_Motor_2_cur"] = typeof(float); nameDict["MAIN.MDF_Motor_3_cur"] = typeof(float); nameDict["MAIN.MDF_Motor_4_cur"] = typeof(float); nameDict["MAIN.CCS_to_MDF_command_listen"] = typeof(String); nameDict["MAIN.CCS_to_MDF_NumsperStack_listen"] = typeof(int); nameDict["MAIN.CCS_to_MDF_VolsperDish_listen"] = typeof(float); cmdString = "MAIN.MDF_Command_response"; try { adsClient.Connect(801); adsClient.AdsNotificationEx += new AdsNotificationExEventHandler(handleNotification); foreach (String s in nameDict.Keys) { handleMap[s] = adsClient.CreateVariableHandle(s); if (nameDict[s] == typeof(string)) { adsClient.AddDeviceNotificationEx(s, AdsTransMode.OnChange, 100, 0, s, nameDict[s],new int[]{ConstSettings.StringLength}); } else { adsClient.AddDeviceNotificationEx(s, AdsTransMode.OnChange, 100, 0, s, nameDict[s]); } } adsClient.WriteAny(handleMap["MAIN.MDF_online_state"], 1); } catch (Exception ex) { } }