瀏覽代碼

nog wat kinderziektes eruit gehaald

Bart Reedijk 10 年之前
父節點
當前提交
e58e68a612

+ 25 - 0
Proftaak Remote Healthcare/Fietsclient/AppGlobal.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Fietsclient
@@ -9,6 +10,8 @@ namespace Fietsclient
     public class AppGlobal
     {
         private static AppGlobal _instance;
+
+        Thread workerThread;
         public static AppGlobal Instance
         {
             get { return _instance ?? (_instance = new AppGlobal()); }
@@ -34,12 +37,34 @@ namespace Fietsclient
             _bikeComm.initComm(portname);
         }
 
+        public void startAskingData()
+        {
+            workerThread = new Thread(() => workerThreadLoop());
+            workerThread.Start();
+        }
+
+        private void workerThreadLoop()
+        {
+            while(true)
+            {
+                Thread.Sleep(1000);
+                _bikeComm.sendData(KettlerBikeComm.STATUS);
+            }
+        }
+
         //event handler
         private void HandleBikeData(string[] data) 
         {
             //doe iets ermee...
         }
 
+        public void closeComPort()
+        {
+            if (workerThread != null)
+                workerThread.Suspend();
+            _bikeComm.closeComm();
+        }
+
 
 
 

+ 15 - 12
Proftaak Remote Healthcare/Fietsclient/KettlerBikeComm.cs

@@ -12,13 +12,13 @@ namespace Fietsclient
     {
 
         // vaste waarden
-        private readonly string COMMAND = "CM";
-        private readonly string CMD_TIME = "PT";
-        private readonly string CMD_DISTANCE = "PD";
-        private readonly string CMD_POWER = "PW";
-        private readonly string CMD_ENERGY = "PE";
-        private readonly string RESET = "RS";
-        private readonly string STATUS = "ST";
+        public static readonly string COMMAND = "CM";
+        public static readonly string CMD_TIME = "PT";
+        public static readonly string CMD_DISTANCE = "PD";
+        public static readonly string CMD_POWER = "PW";
+        public static readonly string CMD_ENERGY = "PE";
+        public static readonly string RESET = "RS";
+        public static readonly string STATUS = "ST";
 
         // private fields
         private string _portname;
@@ -52,6 +52,10 @@ namespace Fietsclient
 
         public void initComm(string portname)
         {
+            if (ComPort != null)
+            {
+                ComPort.Close();
+            }
             _portname = portname;
             ComPort = new SerialPort(_portname, this.baudrate);
             ComPort.Open();
@@ -60,12 +64,11 @@ namespace Fietsclient
             Console.Write(ComPort.ReadLine());
             Console.WriteLine("end of message");
             ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
+        }
 
-            while(true)
-            {
-                Thread.Sleep(1000);
-                ComPort.WriteLine("ST");
-            }
+        public void closeComm()
+        {
+            ComPort.Close();
         }
 
         public void sendData(string data)

+ 29 - 3
Proftaak Remote Healthcare/Fietsclient/MainForm.Designer.cs

@@ -30,6 +30,8 @@
         {
             this.button1 = new System.Windows.Forms.Button();
             this.textBox1 = new System.Windows.Forms.TextBox();
+            this.button2 = new System.Windows.Forms.Button();
+            this.button3 = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // button1
@@ -46,19 +48,41 @@
             // 
             this.textBox1.BackColor = System.Drawing.Color.Black;
             this.textBox1.ForeColor = System.Drawing.Color.White;
-            this.textBox1.Location = new System.Drawing.Point(12, 237);
+            this.textBox1.Location = new System.Drawing.Point(12, 187);
             this.textBox1.Multiline = true;
             this.textBox1.Name = "textBox1";
             this.textBox1.ReadOnly = true;
-            this.textBox1.Size = new System.Drawing.Size(544, 92);
+            this.textBox1.Size = new System.Drawing.Size(806, 142);
             this.textBox1.TabIndex = 1;
             // 
+            // button2
+            // 
+            this.button2.Location = new System.Drawing.Point(12, 42);
+            this.button2.Name = "button2";
+            this.button2.Size = new System.Drawing.Size(118, 23);
+            this.button2.TabIndex = 2;
+            this.button2.Text = "start asking for status";
+            this.button2.UseVisualStyleBackColor = true;
+            this.button2.Click += new System.EventHandler(this.button2_Click);
+            // 
+            // button3
+            // 
+            this.button3.Location = new System.Drawing.Point(94, 13);
+            this.button3.Name = "button3";
+            this.button3.Size = new System.Drawing.Size(88, 23);
+            this.button3.TabIndex = 3;
+            this.button3.Text = "close comport";
+            this.button3.UseVisualStyleBackColor = true;
+            this.button3.Click += new System.EventHandler(this.button3_Click);
+            // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.AutoScroll = true;
-            this.ClientSize = new System.Drawing.Size(568, 341);
+            this.ClientSize = new System.Drawing.Size(830, 343);
+            this.Controls.Add(this.button3);
+            this.Controls.Add(this.button2);
             this.Controls.Add(this.textBox1);
             this.Controls.Add(this.button1);
             this.Name = "MainForm";
@@ -72,6 +96,8 @@
 
         private System.Windows.Forms.Button button1;
         private System.Windows.Forms.TextBox textBox1;
+        private System.Windows.Forms.Button button2;
+        private System.Windows.Forms.Button button3;
     }
 }
 

+ 11 - 1
Proftaak Remote Healthcare/Fietsclient/MainForm.cs

@@ -40,7 +40,17 @@ namespace Fietsclient
                 return;
             }
 
-            textBox1.AppendText(text);
+            textBox1.AppendText(text + "\n");
+        }
+
+        private void button2_Click(object sender, EventArgs e)
+        {
+            _global.startAskingData();
+        }
+
+        private void button3_Click(object sender, EventArgs e)
+        {
+            _global.closeComPort();
         }
     }
 }