Просмотр исходного кода

established coms between simulator and new UI

Mauro de Lyon 10 лет назад
Родитель
Сommit
cb0e37dfad

+ 8 - 5
Proftaak Remote Healthcare/FietsClientV2/DataHandler.cs

@@ -63,10 +63,10 @@ namespace FietsClientV2
 
         public void initComm(string portname)
         {
-            this.portname = portname;
-            if (ComPort.IsOpen)
+            if (ComPort != null)
                 ComPort.Close();
 
+            this.portname = portname;
             try
             {
                 ComPort = new SerialPort(this.portname, this.baudrate);
@@ -116,11 +116,14 @@ namespace FietsClientV2
             }
         }
 
+        int trycount = 0;
         private void handleError()
         {
-            for (int i = 0; i < 3; i++)
-                if (bufferOut == "RS")
-                    sendData("RS");  //gewoon nog een keer proberen tot 3 keer toe, net zolang totdat hij werkt.
+            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)

+ 60 - 50
Proftaak Remote Healthcare/FietsClientV2/PatientForm.Designer.cs

@@ -28,15 +28,15 @@
         /// </summary>
         private void InitializeComponent()
         {
-            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea10 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
-            System.Windows.Forms.DataVisualization.Charting.Legend legend10 = new System.Windows.Forms.DataVisualization.Charting.Legend();
-            System.Windows.Forms.DataVisualization.Charting.Series series10 = new System.Windows.Forms.DataVisualization.Charting.Series();
-            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea11 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
-            System.Windows.Forms.DataVisualization.Charting.Legend legend11 = new System.Windows.Forms.DataVisualization.Charting.Legend();
-            System.Windows.Forms.DataVisualization.Charting.Series series11 = new System.Windows.Forms.DataVisualization.Charting.Series();
-            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea12 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
-            System.Windows.Forms.DataVisualization.Charting.Legend legend12 = new System.Windows.Forms.DataVisualization.Charting.Legend();
-            System.Windows.Forms.DataVisualization.Charting.Series series12 = new System.Windows.Forms.DataVisualization.Charting.Series();
+            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
+            System.Windows.Forms.DataVisualization.Charting.Legend legend4 = new System.Windows.Forms.DataVisualization.Charting.Legend();
+            System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
+            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
+            System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend();
+            System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series();
+            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
+            System.Windows.Forms.DataVisualization.Charting.Legend legend6 = new System.Windows.Forms.DataVisualization.Charting.Legend();
+            System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series();
             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
             this.archiefToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.selectSessionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@@ -44,6 +44,8 @@
             this.bicycleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.selectPortToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.toolStripComboBox1 = new System.Windows.Forms.ToolStripComboBox();
+            this.requestDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.closePortToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.speedBox = new System.Windows.Forms.GroupBox();
             this.speedChart = new System.Windows.Forms.DataVisualization.Charting.Chart();
             this.bpmBox = new System.Windows.Forms.GroupBox();
@@ -83,8 +85,7 @@
             this.messageBox = new System.Windows.Forms.TextBox();
             this.chatArea = new System.Windows.Forms.GroupBox();
             this.sendButton = new System.Windows.Forms.Button();
-            this.requestDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
-            this.closePortToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.openPortToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.menuStrip1.SuspendLayout();
             this.speedBox.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.speedChart)).BeginInit();
@@ -141,6 +142,7 @@
             // 
             this.selectPortToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
             this.toolStripComboBox1,
+            this.openPortToolStripMenuItem,
             this.requestDataToolStripMenuItem,
             this.closePortToolStripMenuItem});
             this.selectPortToolStripMenuItem.Name = "selectPortToolStripMenuItem";
@@ -149,9 +151,25 @@
             // 
             // toolStripComboBox1
             // 
+            this.toolStripComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
             this.toolStripComboBox1.Name = "toolStripComboBox1";
             this.toolStripComboBox1.Size = new System.Drawing.Size(121, 23);
-            this.toolStripComboBox1.Click += new System.EventHandler(this.toolStripComboBox1_Click);
+            // 
+            // requestDataToolStripMenuItem
+            // 
+            this.requestDataToolStripMenuItem.Enabled = false;
+            this.requestDataToolStripMenuItem.Name = "requestDataToolStripMenuItem";
+            this.requestDataToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
+            this.requestDataToolStripMenuItem.Text = "Request data";
+            this.requestDataToolStripMenuItem.Click += new System.EventHandler(this.requestDataToolStripMenuItem_Click);
+            // 
+            // closePortToolStripMenuItem
+            // 
+            this.closePortToolStripMenuItem.Enabled = false;
+            this.closePortToolStripMenuItem.Name = "closePortToolStripMenuItem";
+            this.closePortToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
+            this.closePortToolStripMenuItem.Text = "Close port";
+            this.closePortToolStripMenuItem.Click += new System.EventHandler(this.closePortToolStripMenuItem_Click);
             // 
             // speedBox
             // 
@@ -165,16 +183,16 @@
             // 
             // speedChart
             // 
-            chartArea10.Name = "ChartArea1";
-            this.speedChart.ChartAreas.Add(chartArea10);
-            legend10.Name = "Legend1";
-            this.speedChart.Legends.Add(legend10);
+            chartArea4.Name = "ChartArea1";
+            this.speedChart.ChartAreas.Add(chartArea4);
+            legend4.Name = "Legend1";
+            this.speedChart.Legends.Add(legend4);
             this.speedChart.Location = new System.Drawing.Point(6, 19);
             this.speedChart.Name = "speedChart";
-            series10.ChartArea = "ChartArea1";
-            series10.Legend = "Legend1";
-            series10.Name = "Series1";
-            this.speedChart.Series.Add(series10);
+            series4.ChartArea = "ChartArea1";
+            series4.Legend = "Legend1";
+            series4.Name = "Series1";
+            this.speedChart.Series.Add(series4);
             this.speedChart.Size = new System.Drawing.Size(388, 285);
             this.speedChart.TabIndex = 0;
             this.speedChart.Text = "Speed chart";
@@ -191,16 +209,16 @@
             // 
             // bpmChart
             // 
-            chartArea11.Name = "ChartArea1";
-            this.bpmChart.ChartAreas.Add(chartArea11);
-            legend11.Name = "Legend1";
-            this.bpmChart.Legends.Add(legend11);
+            chartArea5.Name = "ChartArea1";
+            this.bpmChart.ChartAreas.Add(chartArea5);
+            legend5.Name = "Legend1";
+            this.bpmChart.Legends.Add(legend5);
             this.bpmChart.Location = new System.Drawing.Point(6, 19);
             this.bpmChart.Name = "bpmChart";
-            series11.ChartArea = "ChartArea1";
-            series11.Legend = "Legend1";
-            series11.Name = "Series1";
-            this.bpmChart.Series.Add(series11);
+            series5.ChartArea = "ChartArea1";
+            series5.Legend = "Legend1";
+            series5.Name = "Series1";
+            this.bpmChart.Series.Add(series5);
             this.bpmChart.Size = new System.Drawing.Size(388, 285);
             this.bpmChart.TabIndex = 1;
             this.bpmChart.Text = "beats per second";
@@ -506,16 +524,16 @@
             // 
             // rpmChart
             // 
-            chartArea12.Name = "ChartArea1";
-            this.rpmChart.ChartAreas.Add(chartArea12);
-            legend12.Name = "Legend1";
-            this.rpmChart.Legends.Add(legend12);
+            chartArea6.Name = "ChartArea1";
+            this.rpmChart.ChartAreas.Add(chartArea6);
+            legend6.Name = "Legend1";
+            this.rpmChart.Legends.Add(legend6);
             this.rpmChart.Location = new System.Drawing.Point(6, 19);
             this.rpmChart.Name = "rpmChart";
-            series12.ChartArea = "ChartArea1";
-            series12.Legend = "Legend1";
-            series12.Name = "Series1";
-            this.rpmChart.Series.Add(series12);
+            series6.ChartArea = "ChartArea1";
+            series6.Legend = "Legend1";
+            series6.Name = "Series1";
+            this.rpmChart.Series.Add(series6);
             this.rpmChart.Size = new System.Drawing.Size(388, 285);
             this.rpmChart.TabIndex = 2;
             this.rpmChart.Text = "rounds per minute";
@@ -557,21 +575,12 @@
             this.sendButton.Text = "send";
             this.sendButton.UseVisualStyleBackColor = true;
             // 
-            // requestDataToolStripMenuItem
+            // openPortToolStripMenuItem
             // 
-            this.requestDataToolStripMenuItem.Enabled = false;
-            this.requestDataToolStripMenuItem.Name = "requestDataToolStripMenuItem";
-            this.requestDataToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
-            this.requestDataToolStripMenuItem.Text = "Request data";
-            this.requestDataToolStripMenuItem.Click += new System.EventHandler(this.requestDataToolStripMenuItem_Click);
-            // 
-            // closePortToolStripMenuItem
-            // 
-            this.closePortToolStripMenuItem.Enabled = false;
-            this.closePortToolStripMenuItem.Name = "closePortToolStripMenuItem";
-            this.closePortToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
-            this.closePortToolStripMenuItem.Text = "Close port";
-            this.closePortToolStripMenuItem.Click += new System.EventHandler(this.closePortToolStripMenuItem_Click);
+            this.openPortToolStripMenuItem.Name = "openPortToolStripMenuItem";
+            this.openPortToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
+            this.openPortToolStripMenuItem.Text = "Open port";
+            this.openPortToolStripMenuItem.Click += new System.EventHandler(this.openPortToolStripMenuItem_Click);
             // 
             // PatientForm
             // 
@@ -656,6 +665,7 @@
         private System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
         private System.Windows.Forms.ToolStripMenuItem requestDataToolStripMenuItem;
         private System.Windows.Forms.ToolStripMenuItem closePortToolStripMenuItem;
+        private System.Windows.Forms.ToolStripMenuItem openPortToolStripMenuItem;
     }
 }
 

+ 7 - 8
Proftaak Remote Healthcare/FietsClientV2/PatientForm.cs

@@ -26,14 +26,6 @@ namespace FietsClientV2
             toolStripComboBox1.Items.AddRange(ports);
         }
 
-        private void toolStripComboBox1_Click(object sender, EventArgs e)
-        {
-            if (!(toolStripComboBox1.Text == ""))
-                patienModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
-            requestDataToolStripMenuItem.Enabled = true;
-            closePortToolStripMenuItem.Enabled = true;
-        }
-
         private void requestDataToolStripMenuItem_Click(object sender, EventArgs e)
         {
             patienModel.startAskingData();
@@ -43,5 +35,12 @@ namespace FietsClientV2
         {
             patienModel.closeComPort();
         }
+
+        private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            patienModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
+            requestDataToolStripMenuItem.Enabled = true;
+            closePortToolStripMenuItem.Enabled = true;
+        }
     }
 }

+ 5 - 2
Proftaak Remote Healthcare/FietsClientV2/PatientModel.cs

@@ -9,7 +9,10 @@ namespace FietsClientV2
 {
     class PatientModel
     {
-        public static PatientModel patientModel { get; private set; } = new PatientModel();
+
+        private static PatientModel _patientModel;
+        public static PatientModel patientModel { get { return _patientModel ?? (_patientModel = new PatientModel()); } }
+
         private DataHandler dataHandler;
         private Thread workerThread;
 
@@ -17,7 +20,7 @@ namespace FietsClientV2
         {
             dataHandler = new DataHandler();
             DataHandler.IncomingDataEvent += HandleBikeData; //initialize event
-        } 
+        }
 
         public void startComPort(string portname)
         {

+ 82 - 84
Proftaak Remote Healthcare/FietsSimulator/FietsSimulator.cs

@@ -12,39 +12,43 @@ namespace FietsSimulator
 {
     class FietsSimulator
     {
+        //connection with PC
         private SerialPort comport;
-        private Mode curmode; 
-        private int _power, _heartbeat, rpm, speed, distance, energy;
+
+        //bicycle modes
+        private Mode curmode;
+        private enum Mode
+        {
+            NONE,
+            CONSOLE,
+            DISTANCE,
+            TIME
+        }
+
+        //bicycle variables
+        private int power, heartbeat, rpm, speed, distance, energy;
         private long maxtime;
         private Stopwatch stopwatch;
         private Random r = new Random();
 
         public int Power
         {
-            get { return _power; }
+            get { return this.power; }
             set
             {
                 if (value >= 25 && value <= 400)
-                    _power = value;
+                    this.power = value;
                 if (value < 25)
-                    _power = 25;
+                    this.power = 25;
                 if (value > 400)
-                    _power = 400;
+                    this.power = 400;
             }
         }
 
         public int Heartbeat
         {
-           get { return r.Next(60, 160); }
-        }
-
-
-        private enum Mode
-        {
-            NONE,
-            CONSOLE,
-            DISTANCE,
-            TIME
+            //generate random heartbeat between 60 and 160 bpm
+            get { return r.Next(60, 160); }
         }
 
         public FietsSimulator(String addr)
@@ -52,7 +56,7 @@ namespace FietsSimulator
             this.comport = new SerialPort(addr, 9600);
             comport.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
             comport.Open();
-            stopwatch= new Stopwatch();
+            stopwatch = new Stopwatch();
             stopwatch.Stop();
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
@@ -62,71 +66,63 @@ namespace FietsSimulator
 
         private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
         {
-            string message = comport.ReadLine().Trim();
+            string message = comport.ReadLine();
+            string command = message.Substring(0, 2);
             Console.WriteLine(message);
-            if (message == "RS")
-            {
-                curmode = Mode.NONE;
-                rpm = speed = distance = energy = 0;
-                Power = 25;
-                stopwatch.Stop();
-                SendData("ACK");
-            }
-            else if (message == "CU")
-            {
-                curmode = Mode.CONSOLE;
-                SendData("ACK");
-            }else if (message.Contains("PD"))
-            {
-                if (curmode == Mode.CONSOLE && message.Split().Length == 2)
-                {
-                    distance = Int32.Parse(message.Split(' ')[1]);
-                    curmode = Mode.DISTANCE;
-                    stopwatch.Reset();
-                    stopwatch.Start();
-                    rpm = 100;
-                    speed = 10;
-                }
-                else
-                {
-                    SendData("ERROR");
-                }
-            }
-            else if (message.Contains("PT"))
-            {
-                if (curmode == Mode.CONSOLE && message.Split().Length == 2)
-                {
-                    string[] time = message.Split(' ')[1].Split(':');
-                    maxtime = Int32.Parse(time[0]) * 60000 + Int32.Parse(time[1]) * 1000;
-                    curmode = Mode.TIME;
-                    stopwatch.Reset();
-                    stopwatch.Start();
-                    rpm = 100;
-                    speed = 10;
-                }
-                else
-                {
-                    SendData("ERROR");
-                }
-            }
-            else if (message.Contains("PW"))
+
+            switch (command)
             {
-                if (curmode != Mode.NONE && message.Split().Length == 2)
-                {
-                    this.Power = Int32.Parse(message.Split(' ')[1]);
-                }
-                else
-                {
+                case "RS":
+                    curmode = Mode.NONE;
+                    rpm = speed = distance = energy = 0;
+                    Power = 25;
+                    stopwatch.Stop();
+                    SendData("ACK");
+                    break;
+                case "CU":
+                    curmode = Mode.CONSOLE;
+                    SendData("ACK");
+                    break;
+                case "PD":
+                    if (curmode == Mode.CONSOLE)
+                    {
+                        distance = Int32.Parse(message.Split(' ')[1]);
+                        curmode = Mode.DISTANCE;
+                        stopwatch.Reset();
+                        stopwatch.Start();
+                        rpm = 100;
+                        speed = 10;
+                    }
+                    else
+                        SendData("ERROR");
+                    break;
+                case "PT":
+                    if (curmode == Mode.CONSOLE)
+                    {
+                        string[] time = message.Split(' ')[1].Split(':');
+                        maxtime = Int32.Parse(time[0]) * 60000 + Int32.Parse(time[1]) * 1000;
+                        curmode = Mode.TIME;
+                        stopwatch.Reset();
+                        stopwatch.Start();
+                        rpm = 100;
+                        speed = 10;
+                    }
+                    else
+                        SendData("ERROR");
+                    break;
+                case "PW":
+                    if (curmode != Mode.NONE)
+                        this.Power = Int32.Parse(message.Split(' ')[1]);
+                    else
+                        SendData("ERROR");
+                    break;
+                case "ST":
+                    if (curmode != Mode.NONE)
+                        SendStatus();
+                    break;
+                default:
                     SendData("ERROR");
-                }
-            }
-            else if (message == "ST" && curmode != Mode.NONE)
-            {
-                SendStatus();
-            }
-            else
-            {
-                SendData("ERROR");
+                    break;
             }
         }
 
@@ -135,22 +131,24 @@ namespace FietsSimulator
             Console.WriteLine("RETURN:" + message);
             this.comport.WriteLine(message);
         }
+
         private void SendStatus()
         {
-            SendData(Heartbeat.ToString() + "\t"+rpm +"\t"+speed*10+"\t"+distance+"\t" + Power.ToString() + "\t600\t"+getTimeElapsed()+"\t200\r");
+            SendData(Heartbeat.ToString() + "\t" + rpm + "\t" + speed * 10 + "\t" + distance + "\t" + Power.ToString() + "\t600\t" + getTimeElapsed() + "\t200\r");
         }
 
         private string getTimeElapsed()
         {
-            if(curmode == Mode.DISTANCE)
+            if (curmode == Mode.DISTANCE)
             {
                 long seconds = (stopwatch.ElapsedMilliseconds / 1000) % 60;
                 long minutes = ((stopwatch.ElapsedMilliseconds - seconds) / 1000) / 60;
                 return minutes + ":" + seconds;
-            }else if(curmode == Mode.TIME)
+            }
+            else if (curmode == Mode.TIME)
             {
                 long seconds = ((maxtime - stopwatch.ElapsedMilliseconds) / 1000) % 60;
-                long minutes = (((maxtime -stopwatch.ElapsedMilliseconds) - seconds) / 1000) / 60;
+                long minutes = (((maxtime - stopwatch.ElapsedMilliseconds) - seconds) / 1000) / 60;
                 return minutes + ":" + seconds;
             }
             else
@@ -160,7 +158,7 @@ namespace FietsSimulator
         }
         private void OnTimedEvent(object source, ElapsedEventArgs e)
         {
-           if(curmode == Mode.DISTANCE)
+            if (curmode == Mode.DISTANCE)
             {
                 distance -= 1;
             }

+ 8 - 6
Proftaak Remote Healthcare/FietsSimulator/Program.cs

@@ -12,16 +12,18 @@ namespace FietsSimulator
     {
         static void Main(string[] args)
         {
-            string[] ports = SerialPort.GetPortNames();
-            Console.WriteLine("Availabe Comports: \n" + String.Join(" \n", ports));
+            //show available ports
+            Console.WriteLine("Availabe Comports: \n" + String.Join(" \n", SerialPort.GetPortNames()));
+
+            //ask for port
             Console.WriteLine("Enter Comport:");
-            string port = Console.ReadLine();
-            new FietsSimulator(port);
+
+            //start simulator on entered port 
+            new FietsSimulator(Console.ReadLine());
             Console.WriteLine("Started Simulator");
             while (true)
-            {
                 Thread.Sleep(10);
-            }
+            
         }
     }