Forráskód Böngészése

Merge ComportSelector into features/Bart + added exception handler

Bart Reedijk 10 éve
szülő
commit
79c24c9fe7

+ 40 - 8
Proftaak Remote Healthcare/Fietsclient/KettlerBikeComm.cs

@@ -42,6 +42,9 @@ namespace Fietsclient
         public delegate void DataDelegate(string[] data);
         public static event DataDelegate IncomingDataEvent;
 
+        public delegate void DebugDelegate(string debugData);
+        public static event DebugDelegate IncomingDebugLineEvent;
+
         public KettlerBikeComm()
         {
             
@@ -53,6 +56,12 @@ namespace Fietsclient
             if (handler != null) handler(data);
         }
 
+        private static void OnIncomingDebugLineEvent(string debugData)
+        {
+            DebugDelegate handler = IncomingDebugLineEvent;
+            if (handler != null) handler(debugData);
+        }
+
         public void initComm(string portname)
         {
             if (ComPort != null)
@@ -60,13 +69,26 @@ namespace Fietsclient
                 ComPort.Close();
             }
             _portname = portname;
-            ComPort = new SerialPort(_portname, this.baudrate);
-            ComPort.Open();
-            Console.WriteLine("test");
-            ComPort.WriteLine(RESET);
-            Console.Write(ComPort.ReadLine());
-            Console.WriteLine("end of message");
-            ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
+            try
+            {
+                ComPort = new SerialPort(_portname, this.baudrate);
+                ComPort.Open();
+                ComPort.WriteLine(RESET);
+                ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                OnIncomingDebugLineEvent("ERROR: UnauthorizedAccessException throwed");
+            }
+            catch (InvalidOperationException)
+            {
+                OnIncomingDebugLineEvent("ERROR: InvalidOperationException throwed");
+            }
+            finally
+            {
+                try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
+            }
+
         }
 
         public void closeComm()
@@ -86,8 +108,8 @@ namespace Fietsclient
             switch(buffer) //kijk wat er binnenkomt
             {
                 case "ERROR": //wanneer "Error"
-                    //if (_bufferOut == "RS") sendData("RS"); //gewoon nog een keer proberen...
                     returnData = ReturnData.ERROR;
+                    handleError();
                     break;
                 case "ACK": // ACK betekent acknowledged.
                     returnData = ReturnData.ACK;
@@ -102,6 +124,16 @@ namespace Fietsclient
             }
         }
 
+        int trycount = 0;
+        private void handleError()
+        {
+            if (_bufferOut == "RS" && trycount < 3)
+            {
+                sendData("RS");  //gewoon nog een keer proberen tot 3 keer toe, net zolang totdat hij werkt.
+                trycount++;
+            }
+        }
+
         private void handleBikeValues(string buffer)
         {
             buffer = buffer.TrimEnd('\r');

+ 20 - 19
Proftaak Remote Healthcare/Fietsclient/MainForm.Designer.cs

@@ -36,11 +36,11 @@
             this.button2 = new System.Windows.Forms.Button();
             this.button3 = new System.Windows.Forms.Button();
             this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
-            this.comboBox1 = new System.Windows.Forms.ComboBox();
+            this.cmbComport = new System.Windows.Forms.ComboBox();
             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
             this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
-            this.comPortProgressBar = new System.Windows.Forms.ProgressBar();
+            this.pgbComport = new System.Windows.Forms.ProgressBar();
             this.menuStrip1.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
             this.SuspendLayout();
@@ -95,15 +95,16 @@
             this.checkedListBox1.Size = new System.Drawing.Size(210, 124);
             this.checkedListBox1.TabIndex = 4;
             // 
-            // comboBox1
+            // cmbComport
             // 
-            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
-            this.comboBox1.FormattingEnabled = true;
-            this.comboBox1.Location = new System.Drawing.Point(11, 39);
-            this.comboBox1.Margin = new System.Windows.Forms.Padding(2);
-            this.comboBox1.Name = "comboBox1";
-            this.comboBox1.Size = new System.Drawing.Size(100, 21);
-            this.comboBox1.TabIndex = 5;
+            this.cmbComport.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
+            this.cmbComport.FormattingEnabled = true;
+            this.cmbComport.Location = new System.Drawing.Point(11, 39);
+            this.cmbComport.Margin = new System.Windows.Forms.Padding(2);
+            this.cmbComport.Name = "cmbComport";
+            this.cmbComport.Size = new System.Drawing.Size(100, 21);
+            this.cmbComport.TabIndex = 5;
+            this.cmbComport.SelectionChangeCommitted += new System.EventHandler(this.cmbComport_SelectionChangeCommitted);
             // 
             // menuStrip1
             // 
@@ -140,12 +141,12 @@
             this.chart1.TabIndex = 7;
             this.chart1.Text = "chart1";
             // 
-            // comPortProgressBar
+            // pgbComport
             // 
-            this.comPortProgressBar.Location = new System.Drawing.Point(13, 66);
-            this.comPortProgressBar.Name = "comPortProgressBar";
-            this.comPortProgressBar.Size = new System.Drawing.Size(207, 23);
-            this.comPortProgressBar.TabIndex = 8;
+            this.pgbComport.Location = new System.Drawing.Point(13, 66);
+            this.pgbComport.Name = "pgbComport";
+            this.pgbComport.Size = new System.Drawing.Size(207, 23);
+            this.pgbComport.TabIndex = 8;
             // 
             // MainForm
             // 
@@ -153,9 +154,9 @@
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.AutoScroll = true;
             this.ClientSize = new System.Drawing.Size(794, 519);
-            this.Controls.Add(this.comPortProgressBar);
+            this.Controls.Add(this.pgbComport);
             this.Controls.Add(this.chart1);
-            this.Controls.Add(this.comboBox1);
+            this.Controls.Add(this.cmbComport);
             this.Controls.Add(this.checkedListBox1);
             this.Controls.Add(this.button3);
             this.Controls.Add(this.button2);
@@ -178,11 +179,11 @@
         private System.Windows.Forms.Button button2;
         private System.Windows.Forms.Button button3;
         private System.Windows.Forms.CheckedListBox checkedListBox1;
-        private System.Windows.Forms.ComboBox comboBox1;
+        private System.Windows.Forms.ComboBox cmbComport;
         private System.Windows.Forms.MenuStrip menuStrip1;
         private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem;
         private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
-        private System.Windows.Forms.ProgressBar comPortProgressBar;
+        private System.Windows.Forms.ProgressBar pgbComport;
     }
 }
 

+ 19 - 0
Proftaak Remote Healthcare/Fietsclient/MainForm.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
+using System.IO.Ports;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -19,6 +20,8 @@ namespace Fietsclient
             InitializeComponent();
             _global = global;
             KettlerBikeComm.IncomingDataEvent += HandleBikeData;
+            KettlerBikeComm.IncomingDebugLineEvent += addTextToLog;
+            getAvailablePorts();
         }
 
         private void button1_Click(object sender, EventArgs e)
@@ -61,5 +64,21 @@ namespace Fietsclient
         {
             _global.closeComPort();
         }
+
+        private void getAvailablePorts()
+        {
+            string[] ports = SerialPort.GetPortNames();
+            cmbComport.Items.AddRange(ports);
+        }
+
+        private void cmbComport_SelectionChangeCommitted(object sender, EventArgs e)
+        {
+            if (!(cmbComport.Text == ""))
+            {
+                _global.startComPort(cmbComport.Text);
+                pgbComport.Value = 100;
+            }
+        }
+
     }
 }