private void button2_Click(object sender, EventArgs e) { try { PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString()); myConn.Connect(); int anz = readBytes.Length / Convert.ToInt32(txtSize.Text); int olen = Convert.ToInt32(txtSize.Text); int len = Convert.ToInt32(txtNewSize.Text); for (int n = 0; n < anz; n++) { PLCTag plcTag = new PLCTag() { TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text) + n * Convert.ToInt32(txtNewSize.Text), ArraySize = Convert.ToInt32(txtSize.Text) }; byte[] ctrlV = new byte[len]; Array.Copy(readBytes, olen * n, ctrlV, 0, olen); plcTag.Controlvalue = ctrlV; myConn.WriteValue(plcTag); } lblState.Text = anz.ToString() + " Strukturen a " + len.ToString() + " Bytes geschrieben!"; } catch (Exception ex) { lblState.Text = ex.Message; } }
private void button2_Click(object sender, EventArgs e) { try { PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString()); myConn.Connect(); int anz = readBytes.Length/Convert.ToInt32(txtSize.Text); int olen = Convert.ToInt32(txtSize.Text); int len = Convert.ToInt32(txtNewSize.Text); for (int n = 0; n < anz; n++) { PLCTag plcTag = new PLCTag() {TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text) + n*Convert.ToInt32(txtNewSize.Text), ArraySize = Convert.ToInt32(txtSize.Text)}; byte[] ctrlV = new byte[len]; Array.Copy(readBytes, olen*n, ctrlV, 0, olen); plcTag.Controlvalue = ctrlV; myConn.WriteValue(plcTag); } lblState.Text = anz.ToString() + " Strukturen a " + len.ToString() + " Bytes geschrieben!"; } catch (Exception ex) { lblState.Text = ex.Message; } }
public bool SetStart() { if (!IsPlcConnected()) { return(false); } var tag = new PLCTag("DB10.DBX0.0", TagDataType.Bool) { Value = 1 }; lock (_lockObject) { _plcConnection.WriteValue(tag); } return(true); }
private static void WriteNck(string ipAddress = "192.168.214.1") { using (var con = new PLCConnection("WriteNck")) { con.Configuration.CpuIP = ipAddress; con.Configuration.CpuSlot = 4; con.Connect(); try { if (!con.Connected) { con.Connect(); } #region Channel 1 R[0] var R0 = new NC_Var(0x82, 0x41, 0x1, 0x1, 0x15, 0x1, 0xF, 0x8).GetNckTag(); R0.Controlvalue = 5; con.WriteValue(R0); #endregion #region List of R-Parameter var rpa = new NC_Var(0x82, 0x40, 0x1, 0x0, 0x15, 0x1, 0xF, 0x8); var tags = new List <PLCNckTag>(); int channel = 1; for (int i = 1; i < 10; i++) { tags.Add(rpa.GetNckTag(channel, i)); tags.Last().Controlvalue = i; } con.WriteValues(tags); #endregion } catch (Exception ex) { System.Diagnostics.Debugger.Break(); } } }
private void cmdReadStruct_Click(object sender, EventArgs e) { myConn = new PLCConnection("SimpleCSharpDemonstrationConnection"); myConn.Connect(); //PLCTagGeneric PLCTag <TestStruct> tst = new PLCTag <TestStruct>() { DatablockNumber = 97, ByteAddress = 0 }; myConn.ReadValue(tst); TestStruct read = tst.GenericValue; TestStruct wrt = new TestStruct(); wrt.aa = 11; wrt.bb = 12; wrt.cc = 13; wrt.ee = 14; wrt.ff = 15; wrt.test = "Bin da!"; tst.Controlvalue = wrt; myConn.WriteValue(tst); }
private void button3_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "*.data|*.data"; dlg.Multiselect = true; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foreach (var fileName in dlg.FileNames) { var rd = new BinaryReader(File.Open(fileName, FileMode.Open)); var bytes = rd.ReadBytes(Convert.ToInt32(rd.BaseStream.Length)); var fn = Path.GetFileName(fileName); fn = fn.Substring(0, fn.Length - 5); var Tag = new PLCTag(fn + ".DBX0.0"); Tag.TagDataType = TagDataType.ByteArray; Tag.ArraySize = bytes.Length; Tag.Controlvalue = bytes; myConn.WriteValue(Tag); } } }
private void WaitForTrigger() { try { bool alreadyWritten = false; while (true) { if (triggerConn.Connected) { try { //Read the Trigger Bit triggerConn.ReadValue(readBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText("Error: Exception during ReadData, maybe Connection interupted?", ex, Logging.LogLevel.Error); try { triggerConn.Disconnect(); triggerConn.Connect(); } catch (Exception exex) { Logging.LogText("Error: Exception during Connect...", exex, Logging.LogLevel.Error); } } else { throw; } } //If the cycle counter is 0, switch to the slower interval (it means that no new data was there for a long time! ;-) if (cycle_counter > 0) { //Logging.LogTextToLog4Net("WaitForTrigger() => \"" + datasetConfig.TriggerConnection.Name + "\" - NoDataInterval active"); cycle_counter--; if (cycle_counter == 0) { ak_interval = NoDataInterval; } if ((!((bool)readBit.Value) && !alreadyWritten) && datasetConfig.TriggerConnection is LibNoDaveConfig && !((LibNoDaveConfig)datasetConfig.TriggerConnection).StayConnected) { ak_interval = NoDataInterval; Logging.LogTextToLog4Net("WaitForTrigger() => \"" + datasetConfig.TriggerConnection.Name + "\" Discconnect because !StayConnected"); triggerConn.Disconnect(); } } if (((bool)readBit.Value & !alreadyWritten) || ((bool)readBit.Value) && onlyUseOneTag) { //Logging.LogTextToLog4Net("WaitForTrigger() => \"" + datasetConfig.TriggerConnection.Name + "\" - NewDataInterval active"); alreadyWritten = true; cycle_counter = NoDataCycles; ak_interval = NewDataInterval; IEnumerable <object> values = ReadData.ReadDataFromDataSources(datasetConfig, datasetConfig.DatasetConfigRows, activConnections, StartedAsService); if (values != null) { dbInterface.Write(values); if (!onlyUseOneTag) { quittBit.Value = true; try { triggerConn.WriteValue(quittBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText( "Error: Exception during WriteValue, maybe Connection interupted?", ex, Logging.LogLevel.Error); try { triggerConn.Disconnect(); triggerConn.Connect(); } catch (Exception exex) { Logging.LogText("Error: Exception during Connect...", exex, Logging.LogLevel.Error); } } else { throw; } } } else { readBit.Value = false; triggerConn.WriteValue(readBit); } } } else if (!(bool)readBit.Value && !onlyUseOneTag) { if (alreadyWritten) { alreadyWritten = false; quittBit.Value = false; try { triggerConn.WriteValue(quittBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText("Error: Exception during WriteValue, maybe Connection interupted?", ex, Logging.LogLevel.Error); } else { throw; } } } } //Logging.LogTextToLog4Net("Sleep() => \"" + datasetConfig.TriggerConnection.Name + "\" Interval:" + ak_interval); Thread.Sleep(ak_interval); } else { Logging.LogTextToLog4Net("WaitForTrigger() => \"" + datasetConfig.TriggerConnection.Name + "\" => Connect..."); cycle_counter = NoDataCycles; triggerConn.Connect(); } } } catch (ThreadAbortException ex) { //ThreadExceptionOccured.Invoke(this, new ThreadExceptionEventArgs(ex)); } catch (Exception ex) { ThreadExceptionOccured.Invoke(this, new ThreadExceptionEventArgs(ex)); } }
private void cmdReadStruct_Click(object sender, EventArgs e) { myConn = new PLCConnection("SimpleCSharpDemonstrationConnection"); myConn.Connect(); //PLCTagGeneric PLCTag<TestStruct> tst = new PLCTag<TestStruct>() {DataBlockNumber = 97, ByteAddress = 0}; myConn.ReadValue(tst); TestStruct read = tst.GenericValue; TestStruct wrt = new TestStruct(); wrt.aa = 11; wrt.bb = 12; wrt.cc = 13; wrt.ee = 14; wrt.ff = 15; wrt.test = "Bin da!"; tst.Controlvalue = wrt; myConn.WriteValue(tst); }
private void WaitForTrigger() { try { bool alreadyWritten = false; while (true) { if (triggerConn.Connected) { try { //Read the Trigger Bit triggerConn.ReadValue(readBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText("Error: Exception during ReadData, maybe Connection interupted?", ex, Logging.LogLevel.Error); } else { throw; } } //If the cycle counter is 0, switch to the slower interval (it means that no new data was there for a long time! ;-) if (cycle_counter > 0) { cycle_counter--; ak_interval = NoDataInterval; } if ((bool)readBit.Value & !alreadyWritten) { alreadyWritten = true; cycle_counter = NoDataCycles; ak_interval = NewDataInterval; IEnumerable <object> values = ReadData.ReadDataFromDataSources(datasetConfig, datasetConfig.DatasetConfigRows, activConnections, StartedAsService); if (values != null) { dbInterface.Write(values); quittBit.Value = true; try { triggerConn.WriteValue(quittBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText("Error: Exception during WriteValue, maybe Connection interupted?", ex, Logging.LogLevel.Error); } else { throw; } } } } else if (!(bool)readBit.Value) { if (alreadyWritten) { alreadyWritten = false; quittBit.Value = false; try { triggerConn.WriteValue(quittBit); } catch (Exception ex) { if (StartedAsService) { Logging.LogText("Error: Exception during WriteValue, maybe Connection interupted?", ex, Logging.LogLevel.Error); } else { throw; } } } } } Thread.Sleep(ak_interval); } } catch (ThreadAbortException ex) { //ThreadExceptionOccured.Invoke(this, new ThreadExceptionEventArgs(ex)); } catch (Exception ex) { ThreadExceptionOccured.Invoke(this, new ThreadExceptionEventArgs(ex)); } }