public void TestTriggers(DatasetConfig testDataset) { if (testDataset.Trigger == DatasetTriggerType.Tags_Handshake_Trigger) { EstablishConnections(); PLCConnection conn = ConnectionList[testDataset.TriggerConnection] as PLCConnection; if (conn != null) { conn.ReadValue(testDataset.TriggerReadBit); conn.ReadValue(testDataset.TriggerQuittBit); } } }
static void Main(string[] args) { if (args.Length > 0) { if (args[0] == "/config" || args[0] == "-config" || args[0] == "--config") { Configuration.ShowConfiguration("CommandWhenBitSetConn", true); } } else { try { PLCConnection myConn = new PLCConnection("CommandWhenBitSetConn"); myConn.Connect(); PLCTag tag = new PLCTag(Settings.Default.PLCVar); myConn.ReadValue(tag); if ((bool)tag.Value == true) { Process P = new Process(); P.StartInfo.FileName = Settings.Default.CommandToRun; P.StartInfo.Arguments = Settings.Default.CommandToRun; P.Start(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void button4_Click(object sender, EventArgs e) { myConn = new PLCConnection("SimpleCSharpDemonstrationConnection"); myConn.Connect(); //var _tags = new List<PLCTag>(); //var j = 0; //for (var i = 0; i < 96; i++) //{ // Console.WriteLine("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture)); // _tags.Add(new PLCTag("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture)) // { // TagDataType = TagDataType.Float // }); // j += 4; //} //myConn.ReadValues(_tags, false); var tag = new PLCTag(); tag.TagDataType = TagDataType.Word; tag.SymbolicAccessKey = "8a0e000124134d054000000a"; myConn.ReadValue(tag); /*tag.Controlvalue = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77 }; * myConn.WriteValue(tag); * var db = myConn.PLCGetBlockInMC7("DB99"); * MessageBox.Show("DB:" + Encoding.ASCII.GetString(db)); * myConn.PLCPutBlockFromMC7toPLC("DB98", db);*/ }
private void ThreadProc() { PLCTag tag = new PLCTag(triggerBit); while (threadShouldRun) { myConn.ReadValue(tag); if ((bool)tag.Value == true) { PLCTag writeData = new PLCTag(); writeData.TagDataType = TagDataType.CharArray; writeData.DataBlockNumber = writeDB; writeData.ArraySize = writeCharArraySize; string writeDataString = ""; using (StreamReader sr = new StreamReader(csvFile)) { writeDataString = sr.ReadToEnd(); } writeData.Controlvalue = writeDataString; //Reset the Bit tag.Controlvalue = false; myConn.WriteValues(new[] { writeData, tag }); } Thread.Sleep(20); } }
private void _dataReadTimer_Elapsed(object sender, ElapsedEventArgs e) { if (!_plcConnection.Connected) { RaiseIsDisconnected(); return; } // Do the read lock (_lockObject) { _currentReadValue = (short)_plcConnection.ReadValue("DB10.DBW4", TagDataType.Int); } RaiseDataReaded(); }
private void button1_Click(object sender, EventArgs e) { try { PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString()); myConn.Connect(); PLCTag plcTag=new PLCTag(){TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text), ArraySize = Convert.ToInt32(txtBytes.Text)}; myConn.ReadValue(plcTag); readBytes = (byte[]) plcTag.Value; myConn.Disconnect(); lblState.Text = readBytes.Length.ToString() + " Bytes gelesen"; MessageBox.Show("So nun den neuen DB übertragen...."); } catch (Exception ex) { lblState.Text = ex.Message; } }
private void button2_Click(object sender, EventArgs e) { FolderBrowserDialog saveDataDir = new FolderBrowserDialog(); if (saveDataDir.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foreach (string db in listBox1.SelectedItems) { var size = myConn.PLCGetDataBlockSize(db); var Tag = new PLCTag(db + ".DBX0.0"); Tag.TagDataType = TagDataType.ByteArray; Tag.ArraySize = size; myConn.ReadValue(Tag); BinaryWriter wrt = new BinaryWriter(File.Open(Path.Combine(saveDataDir.SelectedPath, db + ".data"), FileMode.Create)); wrt.Write((byte[])Tag.Value); wrt.Close(); } } }
private void button1_Click(object sender, EventArgs e) { try { PLCConnection myConn = new PLCConnection(lstConnections.SelectedItem.ToString()); myConn.Connect(); PLCTag plcTag = new PLCTag() { TagDataType = TagDataType.ByteArray, TagDataSource = MemoryArea.Datablock, DataBlockNumber = Convert.ToInt32(txtDB.Text), ByteAddress = Convert.ToInt32(txtStartByte.Text), ArraySize = Convert.ToInt32(txtBytes.Text) }; myConn.ReadValue(plcTag); readBytes = (byte[])plcTag.Value; myConn.Disconnect(); lblState.Text = readBytes.Length.ToString() + " Bytes gelesen"; MessageBox.Show("So nun den neuen DB übertragen...."); } catch (Exception ex) { lblState.Text = ex.Message; } }
private static void ReadFromNck(string ipAddress = "192.168.214.1") { using (var con = new PLCConnection("ReadFromNck")) { con.Configuration.CpuIP = ipAddress; con.Configuration.CpuSlot = 4; con.Connect(); try { if (!con.Connected) { con.Connect(); } #region Channel 1 R[0] var R0 = con.ReadValue(new NC_Var(0x82, 0x41, 0x1, 0x1, 0x15, 0x1, 0xF, 0x8)); Console.WriteLine("R0: {0}", 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().Tag = string.Format("R[{0}]", i - 1); } con.ReadValues(tags); tags.ForEach(f => Console.WriteLine("{0}: {1}", f.Tag, f.Value)); #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 timer_Tick(object sender, EventArgs e) { myConn.ReadValue(myValue); lblString.Text = myValue.GetValueAsString(); }
public string CheckConfiguration(bool TestConnections) { string error = ""; var ConnectionList = new Dictionary <ConnectionConfig, PLCConnection>(); //Try to Connect to the PLCs if (TestConnections) { foreach (ConnectionConfig connectionConfig in Connections) { LibNoDaveConfig lConn = connectionConfig as LibNoDaveConfig; if (lConn != null) { using (PLCConnection myConn = new PLCConnection(lConn.Configuration)) { try { myConn.Connect(); ConnectionList.Add(connectionConfig, myConn); } catch (Exception ex) { error += "Error: Error connecting \"" + lConn.Name + "\" : " + ex.Message + Environment.NewLine; } } } } } var TCPIPKeys = new Dictionary <string, int>(); foreach (DatasetConfig datasetConfig in Datasets) { var DatasetConnectionKeys = new Dictionary <string, object>(); //Look if TCPIP Connection is only used in one Dataset, because we need the length for each Connection! try { if (datasetConfig.DatasetConfigRows.Count > 0) { if (datasetConfig.DatasetConfigRows[0].Connection != null) { var tcp = datasetConfig.DatasetConfigRows[0].Connection as TCPIPConfig; var byteCount = ReadData.GetCountOfBytesToRead(datasetConfig.DatasetConfigRows); if (tcp != null && !tcp.DontUseFixedTCPLength) { if (!TCPIPKeys.ContainsKey(datasetConfig.DatasetConfigRows[0].Connection.Name)) { TCPIPKeys.Add(datasetConfig.DatasetConfigRows[0].Connection.Name, byteCount); } if (TCPIPKeys[datasetConfig.DatasetConfigRows[0].Connection.Name] != byteCount) { error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset with differnet bytes sizes, but fixed Length should be used!" + Environment.NewLine; } } } } else { error += "Error: Dataset \"" + datasetConfig.Name + "\" - No DatasetConfigRow is set!" + Environment.NewLine; } } catch { error += "Error: Dataset \"" + datasetConfig.Name + "\" - The same TCP/IP Connection is used in more than one Dataset!" + Environment.NewLine; } //Look if Trigger on a Dataset with TCP/IP Connection is Incoming Data, and that this trigger is not used on a Connection without TCP/IP try { if (!(datasetConfig.TriggerConnection is TCPIPConfig && (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection))) { error += "Error: Dataset \"" + datasetConfig.Name + "\" - The selected Connection for incoming Trigger is no TCP/IP Connection !" + Environment.NewLine; } } catch { } //Look if Trigger Connection was selected (Handshake Trigger) if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection == null) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine; } //Look if Trigger Connection was selected (TCPIP Trigger) if (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection && datasetConfig.TriggerConnection == null) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine; } if (datasetConfig.Storage == null) { error += "Error: Dataset \"" + datasetConfig.Name + " - Storage is not set!" + Environment.NewLine; } foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows) { if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine; } } if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger && datasetConfig.TriggerConnection == null) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Trigger Connection not set!" + Environment.NewLine; } foreach (DatasetConfigRow datasetConfigRow in datasetConfig.DatasetConfigRows) { //Look if PLC-Connection was selected if (datasetConfigRow.Connection == null && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Connection not Set!" + Environment.NewLine; } //Look if DatabaseFieldType was selected if (datasetConfigRow.DatabaseFieldType == "") { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - DatabaseFieldType not Set!" + Environment.NewLine; } ////Look if PLC-ValueType was selected if (datasetConfigRow.PLCTag.LibNoDaveDataType == null) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - PLC-ValueType not Set!" + Environment.NewLine; } if (TestConnections && datasetConfig.Trigger != DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection) { PLCConnection conn = null as PLCConnection; if (datasetConfigRow.Connection != null) { try { conn = ConnectionList[datasetConfigRow.Connection] as PLCConnection; } catch { conn = null; } } else { conn = null; } if (conn != null) { try { conn.ReadValue(datasetConfigRow.PLCTag); if (datasetConfigRow.PLCTag.ItemDoesNotExist) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine; } } catch (Exception ex) { error += "Error: Dataset \"" + datasetConfig.Name + "\" Row \"" + datasetConfigRow.DatabaseField + "\" - Error Reading Value on Address " + datasetConfigRow.PLCTag.S7FormatAddress + " !" + Environment.NewLine; } } } } } //Look if Connection Name exists only once var ConnectionNames = new List <string>(); var ConnectionKeys = new Dictionary <string, object>(); foreach (ConnectionConfig item in Connections) { if (ConnectionKeys.ContainsKey(item.Name)) { error += "Error: Connection name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine; } else { ConnectionKeys.Add(item.Name, null); } } //Look if Storrage Name exists only once var StorageNames = new List <string>(); var StoragesKeys = new Dictionary <string, object>(); foreach (StorageConfig item in Storages) { if (StoragesKeys.ContainsKey(item.Name)) { error += "Error: Storage name \"" + item.Name + "\" - exist more than once!" + Environment.NewLine; } else { StoragesKeys.Add(item.Name, null); } } //Look if the Database Field Type is in the Field Types List //Look if Field Name Exists only Once -> This is possible in excel, but not in databases if (error == "") { return(null); } return(error); }
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)); } }
private void button4_Click(object sender, EventArgs e) { myConn = new PLCConnection("SimpleCSharpDemonstrationConnection"); myConn.Connect(); //var _tags = new List<PLCTag>(); //var j = 0; //for (var i = 0; i < 96; i++) //{ // Console.WriteLine("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture)); // _tags.Add(new PLCTag("DB1.DBD" + j.ToString(CultureInfo.InvariantCulture)) // { // TagDataType = TagDataType.Float // }); // j += 4; //} //myConn.ReadValues(_tags, false); var tag = new PLCTag(); tag.TagDataType = TagDataType.Word; tag.SymbolicAccessKey = "8a0e000124134d054000000a"; myConn.ReadValue(tag); /*tag.Controlvalue = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77 }; myConn.WriteValue(tag); var db = myConn.PLCGetBlockInMC7("DB99"); MessageBox.Show("DB:" + Encoding.ASCII.GetString(db)); myConn.PLCPutBlockFromMC7toPLC("DB98", db);*/ }