Ver código fonte

SerialAccess to actually use the serial port.
And some random bits.

Gregory 3 anos atrás
pai
commit
9063b73608

+ 0 - 1
crusherScanner/FileAccess.cs

@@ -9,7 +9,6 @@ namespace crusherScanner
     internal class FileAccess
     {
 
-
         public static HashSet<Samples> GetOreDefData(HashSet<Samples> samples,string barcode)
         {
             string[] DataDirs = new string[2] { Properties.Settings.Default.OreDefWorkFile, Properties.Settings.Default.OreDefInFile };

+ 21 - 40
crusherScanner/Form1.cs

@@ -46,47 +46,12 @@ namespace crusherScanner
                     backgroundWorker1.RunWorkerAsync(scanned);
                 }
                 TextBox1.Text = "";
+
+                ContaminateAlert(scanned.sampleData.HammerOil, scanned.barcode, "Hammer Oil", label5, Color.Maroon);
+                ContaminateAlert(scanned.sampleData.Manganese, scanned.barcode, "Manganese", label4, Color.Blue);
+                ContaminateAlert(scanned.sampleData.Plastic, scanned.barcode, "Plastic", label3, Color.Orange);
+                ContaminateAlert(scanned.sampleData.Pyrite, scanned.barcode, "Pyrite", label2, Color.Fuchsia);
                 
-                if (scanned.sampleData.HammerOil)
-                {
-                    label5.Text = $"Sample {scanned.barcode} contains Hammer Oil";
-                    BackColor = Color.Maroon;
-                    label5.Show();
-                }
-                else
-                {
-                    label5.Hide();
-                }
-                if (scanned.sampleData.Manganese)
-                {
-                    label4.Text = $"Sample {scanned.barcode} contains Manganese";
-                    BackColor = Color.Blue;
-                    label4.Show();
-                }
-                else
-                {
-                    label4.Hide();
-                }
-                if (scanned.sampleData.Plastic)
-                {
-                    label3.Text = $"Sample {scanned.barcode} contains Plastic";
-                    BackColor = Color.Orange;
-                    label3.Show();
-                }
-                else
-                {
-                    label3.Hide();
-                }
-                if (scanned.sampleData.Pyrite)
-                {
-                    label2.Text = $"Sample {scanned.barcode} contains Pyrite";
-                    BackColor = Color.Fuchsia;
-                    label2.Show();
-                }
-                else
-                {
-                    label2.Hide();
-                }
                 if (!scanned.Contaminated)
                 {
                     label1.Text = scanned.message;
@@ -96,6 +61,7 @@ namespace crusherScanner
                 {
                     label1.Hide();
                 }
+
                 if (scanned.sampleData.Job != null)
                 {
                     label8.Text = $"Job in progress : {scanned.sampleData.Job}";
@@ -104,6 +70,7 @@ namespace crusherScanner
                 {
                     label8.Text = "";
                 }
+
                 if (scanned.BufferCount != -1)
                 {
                     toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
@@ -111,6 +78,20 @@ namespace crusherScanner
             }
         }
 
+        private void ContaminateAlert(bool check,string bCode, string name,Label rtLabel, Color bColor)
+        {
+            if (check)
+            {
+                rtLabel.Text = $"Sample {bCode} contains {name}";
+                BackColor = bColor;
+                rtLabel.Show();   
+            }
+            else
+            {
+                rtLabel.Hide();
+            }
+        }
+
         private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
         {
             this.Close();

+ 1 - 0
crusherScanner/ProgramFunctions.cs

@@ -18,6 +18,7 @@ namespace crusherScanner
         public ProgramFunctions()
         {
             Settings tmp = new();
+            //TODO: remove debug values.
             tmp.CrusherNo = 1;
             tmp.OreDefInFile = "te";
             SettingsHandler.CheckSettings(tmp);

+ 79 - 10
crusherScanner/SerialAccess.cs

@@ -1,23 +1,92 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.IO.Ports;
 
 namespace crusherScanner
 {
+    /// <summary>
+    /// For sending data to Prepmaster via serial.
+    /// nb; The concept here is to NOT hold the serial port open.
+    /// </summary>
     internal class SerialAccess
     {
-        //Serial stuff
-        //https://www.c-sharpcorner.com/UploadFile/eclipsed4utoo/communicating-with-serial-port-in-C-Sharp/
 
+        private static bool SerialReady = false;
+        private static SerialPort? _serialPort;
+
+        /// <summary>
+        /// Send sample ID to the serial port for Prepmaster to receive.
+        /// </summary>
+        /// <param name="barcode">The sample ID to send.</param>
         public static void SendToPrepmaster(string barcode)
         {
-            if(barcode != "" || !Properties.Settings.Default.PrepmasterConnection)
+            if (barcode != "" && !Properties.Settings.Default.PrepmasterConnection)
+            {
+                MessageBox.Show($"The crusher flap would\nhave just opened to\naccept sample {barcode}", "Crusher Input");
+            }
+            else if (barcode != "")
+            {
+                if (!SerialReady)
+                {
+                    SetupSerial();
+                }
+                SendSerial(barcode);
+            }
+        }
+
+        /// <summary>
+        /// Setup and test the serial port.
+        /// </summary>
+        private static void SetupSerial()
+        {
+            //TODO: use settings for connection
+            _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
+            _serialPort.Handshake = Handshake.None;
+
+            try
+            {
+                // Opens serial port as a quick test
+                _serialPort.Open();
+            }
+            catch (Exception ex)
+            {
+                Logging.Append("Error opening serial port :: " + ex.Message);
+                MessageBox.Show("Error opening serial port :: " + ex.Message, "Error!");
+            }
+            finally
+            {
+                _serialPort.Close();
+                SerialReady = true;
+            }
+        }
+
+        /// <summary>
+        /// Send data to the serial connection.
+        /// </summary>
+        /// <param name="data">The sample ID to send to Prepmaster.</param>
+        private static void SendSerial(string data)
+        {
+
+            if (_serialPort == null)
+            {
+                Logging.Append("Error opening serial port :: _serialPort is null");
+                MessageBox.Show("Error opening serial port :: Serial device may have been unpluged.", "Error!");
+                SerialReady = false;
+                return;
+            }
+
+            // Makes sure serial port is open before trying to write  
+            try
+            {
+                if (!_serialPort.IsOpen)
+                {
+                    _serialPort.Open();
+                    _serialPort.Write(data + "\r\n");
+                }
+            }
+            catch (Exception ex)
             {
-                MessageBox.Show($"The crusher flap would\nhave just opened to\naccept sample {barcode}","Crusher Input");
+                Logging.Append("Error opening serial port :: " + ex.Message);
+                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
             }
-            return;
         }
 
     }