private void readInt16Val(Tag tag, Libplctag client) { var sb = new StringBuilder(); for (int i = 0; i < tag.ElementCount; i++) { sb.Append($"{tag.Name} = {client.GetInt16Value(tag, (i * tag.ElementSize))}\n"); } MessageBox.Show(sb.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public void readdata() { //var watch = new System.Diagnostics.Stopwatch(); //watch.Start(); try { // creates a tag to read B3:0, 1 item, from LGX ip address 192.168.0.100 // The last entry in this new tag is the element count. It is currently // set to 1 //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) //string name is the textual name of the tag in plc //elementSize is the size of the element in bytes //elementCount elements count: 1- single, n-array //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) var tag = new Tag("10.14.6.100", "1, 0", CpuType.LGX, "REPORT_INT[0]", 2, 15, 0); var tag1 = new Tag("10.14.6.100", "1, 0", CpuType.LGX, "REPORT_FLOAT[0]", 4, 14, 0); using (var client = new Libplctag()) { // add the tag client.AddTag(tag); client.AddTag(tag1); //client.AddTag(tag2); // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tag) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tag1) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // if the status is not ok, we have to handle the error if (client.GetStatus(tag) != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString((client.GetStatus(tag))) + ("\n" + $"Five Minute Data tag 5 Read Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag))}\n")); } return; } // if the status is not ok, we have to handle the error if (client.GetStatus(tag1) != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString((client.GetStatus(tag1))) + ("\n" + $"Five Minute Data tag 5 Read Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag1))}\n")); } return; } // Execute the read var result = client.ReadTag(tag, DataTimeout); // Execute the read var result1 = client.ReadTag(tag1, DataTimeout); // Execute the read //var result2 = client.ReadTag(tag2, DataTimeout); // Check the read operation result if (result != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString(($"Five Minute Data Read ERROR: Unable to read the data! Got error code {result}: {client.DecodeError(result)}\n"))); } return; } if (result1 != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString(($"Five Minute Data Read ERROR: Unable to read the data! Got error code {result1}: {client.DecodeError(result1)}\n"))); } return; } dh = new SQLDataHandler(); //dr = dh.getReader("Select schrunmins, shiftstarttime FROM ConfigurationSettings Where plantname = 'Kanawha Eagle';"); //dr.Read(); //int schrunmins = (int)dr["schrunmins"]; // Convert the data var plantairpressure = client.GetInt16Value(tag, 0 * tag.ElementSize); var thickenerunderflowpsi = client.GetInt16Value(tag, 1 * tag.ElementSize); var thickenerrotationtorque = client.GetInt16Value(tag, 2 * tag.ElementSize); var thickenerunderflowgpm = client.GetInt16Value(tag, 3 * tag.ElementSize); var screenbowl1amps = client.GetInt16Value(tag, 4 * tag.ElementSize); var screenbowl1torque = client.GetInt16Value(tag, 5 * tag.ElementSize); var screenbowl2amps = client.GetInt16Value(tag, 6 * tag.ElementSize); var screenbowl2torque = client.GetInt16Value(tag, 7 * tag.ElementSize); var plantfeedtph = client.GetInt16Value(tag, 8 * tag.ElementSize); var cleancoaltph = client.GetInt16Value(tag, 9 * tag.ElementSize); var stokertph = client.GetInt16Value(tag, 10 * tag.ElementSize); var bypasstph = client.GetInt16Value(tag, 11 * tag.ElementSize); var refusetph = client.GetInt16Value(tag, 12 * tag.ElementSize); var scalpedtph = client.GetInt16Value(tag, 13 * tag.ElementSize); var slurrytph = client.GetInt16Value(tag, 14 * tag.ElementSize); var thickenerunderflowsg = client.GetFloat32Value(tag1, 0 * tag1.ElementSize); var thickenerrotationamps = client.GetFloat32Value(tag1, 1 * tag1.ElementSize); //var hmvsg = client.GetFloat32Value(tag1, 2 * tag1.ElementSize); //var hmvsgsetpoint = client.GetFloat32Value(tag1, 3 * tag1.ElementSize); var hmcyclonesg = client.GetFloat32Value(tag1, 4 * tag1.ElementSize); var hmcyclone1sgsetpoint = client.GetFloat32Value(tag1, 5 * tag1.ElementSize); var yield = client.GetFloat32Value(tag1, 6 * tag1.ElementSize); var hmcyclone1psi = client.GetFloat32Value(tag1, 7 * tag1.ElementSize); var hmcyclone2psi = client.GetFloat32Value(tag1, 8 * tag1.ElementSize); var deslimecyclonepsi = client.GetFloat32Value(tag1, 9 * tag1.ElementSize); var ccclasscyclonepsi = client.GetFloat32Value(tag1, 10 * tag1.ElementSize); var rawcoalcyclonepsi = client.GetFloat32Value(tag1, 11 * tag1.ElementSize); var magscrewtotalrunmins = client.GetFloat32Value(tag1, 12 * tag1.ElementSize); var plantfeedtotalrunmins = client.GetFloat32Value(tag1, 13 * tag1.ElementSize); try { dh.executeSql("Insert INTO TrendingData(created_at, plantair, thickenerunderpsi, thickenertorque, thickenerunderflow, thickenergravity, thickeneramps, screenbowl1amps, " + "screenbowl1torque, screenbowl2amps, screenbowl2torque, hmcyclonesg1, hmcyclone1sgsetpoint, hmcyclone1psi, " + "hmcyclone2psi, deslimecyclonepsi, ccclasscyclonepsi, rawcoalcyclonepsi, yield, plantfeedtph, cleantph, stokertph, bypasstph, reftph, scalpedtph, " + "slurrytph) VALUES (GETDATE(), '" + plantairpressure + "', '" + thickenerunderflowpsi + "', '" + thickenerrotationtorque + "', '" + thickenerunderflowgpm + "', '" + thickenerunderflowsg + "', '" + thickenerrotationamps + "', '" + screenbowl1amps + "', '" + screenbowl1torque + "', '" + screenbowl2amps + "', '" + screenbowl2torque + "', '" + hmcyclonesg + "', '" + hmcyclone1sgsetpoint + "', '" + hmcyclone1psi + "', '" + hmcyclone2psi + "', '" + deslimecyclonepsi + "', '" + ccclasscyclonepsi + "', '" + rawcoalcyclonepsi + "', '" + yield + "', '" + plantfeedtph + "', '" + cleancoaltph + "', '" + stokertph + "', '" + bypasstph + "', '" + refusetph + "', '" + scalpedtph + "', '" + slurrytph + "')"); } catch (SqlException se) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occurred at one minute data insert to database on: " + DateTime.Now + " \r\n" + se + "\r\n"); } return; } client.RemoveTag(tag); client.RemoveTag(tag1); } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occurred at one minute data plc connection on: " + DateTime.Now + " \r\n" + ex + "\r\n"); } return; } }
public void createConnection(string tagname, int bytesize, int numretrieved) { //var watch = new System.Diagnostics.Stopwatch(); //watch.Start(); try { dm = new DownTimeMonitoring(); // creates a tag to read B3:0, 1 item, from LGX ip address 192.168.0.100 // The last entry in this new tag is the element count. It is currently // set to 1 //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) //string name is the textual name of the tag in plc //elementSize is the size of the element in bytes //elementCount elements count: 1- single, n-array //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) var tagstatus = new Tag("10.14.6.100", "1, 0", CpuType.LGX, tagname, bytesize, numretrieved); using (var client = new Libplctag()) { // add the tag client.AddTag(tagstatus); // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tagstatus) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // if the status is not ok, we have to handle the error if (client.GetStatus(tagstatus) != Libplctag.PLCTAG_STATUS_OK) { setDownbit(-5); return; } // Execute the read var result = client.ReadTag(tagstatus, DataTimeout); Int16 checkError; // Check the read operation result if (result != Libplctag.PLCTAG_STATUS_OK) { setDownbit(-5); return; } else { checkError = client.GetInt16Value(tagstatus, 0 * tagstatus.ElementSize); setDownbit(checkError); } client.RemoveTag(tagstatus); } } catch (Exception ex) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\Users\KE_PLCData\Report Error Logging\ErrorLog.txt", true)) { file.WriteLine("Error occurred with plc connection: " + DateTime.Now + " \r\n" + ex + "\r\n"); } return; } }
public void readData() { detail = new DetailedReport(); con = new PLCConnection(); setTimeRanges(); try { // creates a tag to read B3:0, 1 item, from LGX ip address 192.168.0.100 // The last entry in this new tag is the element count. It is currently // set to 1 //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) //string name is the textual name of the tag in plc //elementSize is the size of the element in bytes //elementCount elements count: 1- single, n-array //public Tag(string ipAddress, string path, CpuType cpuType, string name, int elementSize, int elementCount, int debugLevel = 0) var tag5 = new Tag("10.14.6.100", "1, 0", CpuType.LGX, "REPORT_DINT[0]", 4, 6, 0); var tag6 = new Tag("10.14.6.100", "1, 0", CpuType.LGX, "REPORT_FLOAT[12]", 4, 3, 0); var tag12 = new Tag("10.14.6.100", "1, 0", CpuType.LGX, "REPORT_INT[8]", 2, 2, 0); using (var client = new Libplctag()) { // add the tag client.AddTag(tag5); client.AddTag(tag6); client.AddTag(tag12); //client.AddTag(tag2); // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tag5) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tag6) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // check that the tag has been added, if it returns pending we have to retry while (client.GetStatus(tag12) == Libplctag.PLCTAG_STATUS_PENDING) { Thread.Sleep(100); } // if the status is not ok, we have to handle the error if (client.GetStatus(tag5) != Libplctag.PLCTAG_STATUS_OK) { con.setDownbit(-5); using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString((client.GetStatus(tag5))) + ("\n" + $"Five Minute Data tag 5 Read Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag5))}\n")); } return; } // if the status is not ok, we have to handle the error if (client.GetStatus(tag6) != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString((client.GetStatus(tag6))) + ("\n" + $"Five Minute Data tag 6 Read Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag6))}\n")); } return; } // if the status is not ok, we have to handle the error if (client.GetStatus(tag12) != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString((client.GetStatus(tag12))) + ("\n" + $"Five Minute Data tag 6 Read Error setting up tag internal state. Error{ client.DecodeError(client.GetStatus(tag12))}\n")); } return; } // Execute the read var result = client.ReadTag(tag5, DataTimeout); // Execute the read var result1 = client.ReadTag(tag6, DataTimeout); // Execute the read var result2 = client.ReadTag(tag12, DataTimeout); // Check the read operation result if (result != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString(($"Five Minute Data Read ERROR: Unable to read the data! Got error code {result}: {client.DecodeError(result)}\n"))); } return; } if (result1 != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString(($"Five Minute Data Read ERROR: Unable to read the data! Got error code {result1}: {client.DecodeError(result1)}\n"))); } return; } if (result2 != Libplctag.PLCTAG_STATUS_OK) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occured at: " + DateTime.Now + Convert.ToString(($"Five Minute Data Read ERROR: Unable to read the data! Got error code {result2}: {client.DecodeError(result2)}\n"))); } return; } // read the data from the array that was retrieved from the PLC //double integer values var plantfeedtotaltons = client.GetInt32Value(tag5, 0 * tag5.ElementSize); var cleancoaltotaltons = client.GetInt32Value(tag5, 1 * tag5.ElementSize); var stokercoaltotaltons = client.GetInt32Value(tag5, 2 * tag5.ElementSize); var bypasstotaltons = client.GetInt32Value(tag5, 3 * tag5.ElementSize); var refusetotaltons = client.GetInt32Value(tag5, 4 * tag5.ElementSize); var scalpedtotaltons = client.GetInt32Value(tag5, 5 * tag5.ElementSize); //float values //var yield = client.GetFloat32Value(tag6, 0 * tag6.ElementSize); //mag minutes float 12 needs added to the desktop reporting //grand total number like the coal values var magmins = client.GetFloat32Value(tag6, 0 * tag6.ElementSize); var plantfeedrunmins = client.GetFloat32Value(tag6, 1 * tag6.ElementSize); var downminstotal = client.GetFloat32Value(tag6, 2 * tag6.ElementSize); //int 16 values var plantfeedtph = client.GetInt16Value(tag12, 0 * tag12.ElementSize); var cleancoaltph = client.GetInt16Value(tag12, 1 * tag12.ElementSize); //code about this needs to be here and this data from the PLC needs used for updating DetailedReport table //it is at the top for hard code testing detail = new DetailedReport(); detail.createDetailedReport(plantfeedtotaltons, cleancoaltotaltons, plantfeedtph, cleancoaltph, downminstotal, plantfeedrunmins, magmins); try { //Insert data from PLC into FiveMinuteData table every five minute read dh.executeSql("Insert INTO FiveMinuteData(created_at, plantname, plantfeedtons, cleantons, stokertons, bypasstons, refusetons, scalpedtons) VALUES " + "(GETDATE(), 'Kanawha Eagle', '" + plantfeedtotaltons + "', '" + cleancoaltotaltons + "', '" + stokercoaltotaltons + "', '" + bypasstotaltons + "', '" + refusetotaltons + "', '" + scalpedtotaltons + "')"); }catch (SqlException se) { //writing data to a text file to observe errors for troubleshooting using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occurred at Five Minute Data insert to database: " + DateTime.Now + " \r\n" + se + "\r\n"); } return; } client.RemoveTag(tag5); client.RemoveTag(tag6); } } catch (Exception ex) { //writing errors to text file for troubleshooting using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\KE_PLCData\Documents\ReportErrorLogging\ErrorLog.txt", true)) { file.WriteLine("Error occurred at Five Minute Data insert to database: " + DateTime.Now + " \r\n" + ex + "\r\n"); } return; } }
private static bool RefreshTags(List <Tag> tags, Libplctag client) { foreach (var tag in tags) { if (tag.Name.StartsWith("F")) { /* get the data */ var rc = client.ReadTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to read the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* print out the data */ for (int i = 0; i < tag.ElementCount; i++) { Console.WriteLine($"{tag.Name} data[{i}]={client.GetFloat32Value(tag, (i*tag.ElementSize))}"); } /* now test a write */ for (int i = 0; i < tag.ElementCount; i++) { var val = client.GetFloat32Value(tag, (i * tag.ElementSize)); val++; if (val > 1000) { val = 0; } Console.WriteLine($"{tag.Name} Setting element {i} to {val}"); client.SetFloat32Value(tag, (i * tag.ElementSize), val); } rc = client.WriteTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to read the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* get the data again*/ rc = client.ReadTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to read the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* print out the data */ for (int i = 0; i < tag.ElementCount; i++) { Console.WriteLine($"{tag.Name} data[{i}]={client.GetFloat32Value(tag, (i*tag.ElementSize))}"); } } else { /* get the data */ var rc = client.ReadTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to read the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* print out the data */ for (int i = 0; i < tag.ElementCount; i++) { Console.WriteLine($"{tag.Name} data[{i}]={client.GetInt16Value(tag, (i*tag.ElementSize))}"); } if (!tag.Name.Contains("O") && !tag.Name.Contains("I")) // we can't write on I/O { /* now test a write */ for (int i = 0; i < tag.ElementCount; i++) { var val = client.GetInt16Value(tag, (i * tag.ElementSize)); val++; if (val > 1000) { val = 0; } Console.WriteLine($"{tag.Name} Setting element {i} to {val}"); client.SetInt16Value(tag, (i * tag.ElementSize), val); } rc = client.WriteTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to write the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* get the data again*/ rc = client.ReadTag(tag, DataTimeout); if (rc != Libplctag.PLCTAG_STATUS_OK) { LogError( $"{tag.Name} ERROR: Unable to read the data! Got error code {rc}: {client.DecodeError(rc)}"); return(false); } /* print out the data */ for (int i = 0; i < tag.ElementCount; i++) { Console.WriteLine($"{tag.Name} data[{i}]={client.GetInt16Value(tag, (i*tag.ElementSize))}"); } } } } return(true); }