From 854d183637343d3e415ba4d3d8d43f34d3a86787 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Sat, 5 Sep 2015 21:41:32 +0200 Subject: [PATCH] First working simulator version --- .../ErgometerSimulator/ComPort.cs | 74 ++++++++++++++++++ .../ErgometerSimulator/CommandParser.cs | 40 ++++++++++ .../ErgometerSimulator.Designer.cs | 77 +++++++++++++++++++ .../ErgometerSimulator/ErgometerSimulator.cs | 54 +++++++++++++ .../ErgometerSimulator.csproj | 12 +-- ...ainWindow.resx => ErgometerSimulator.resx} | 0 .../ErgometerSimulator/MainWindow.Designer.cs | 47 ----------- .../ErgometerSimulator/MainWindow.cs | 20 ----- .../ErgometerSimulator/Program.cs | 17 +--- 9 files changed, 253 insertions(+), 88 deletions(-) create mode 100644 ErgometerSimulator/ErgometerSimulator/ComPort.cs create mode 100644 ErgometerSimulator/ErgometerSimulator/CommandParser.cs create mode 100644 ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.Designer.cs create mode 100644 ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.cs rename ErgometerSimulator/ErgometerSimulator/{MainWindow.resx => ErgometerSimulator.resx} (100%) delete mode 100644 ErgometerSimulator/ErgometerSimulator/MainWindow.Designer.cs delete mode 100644 ErgometerSimulator/ErgometerSimulator/MainWindow.cs diff --git a/ErgometerSimulator/ErgometerSimulator/ComPort.cs b/ErgometerSimulator/ErgometerSimulator/ComPort.cs new file mode 100644 index 0000000..71d9548 --- /dev/null +++ b/ErgometerSimulator/ErgometerSimulator/ComPort.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ErgometerSimulator +{ + class ComPort + { + private SerialPort comPort; + private ErgometerSimulator ergs; + + public ComPort(ErgometerSimulator ergs) + { + comPort = null; + this.ergs = ergs; + } + + public Boolean Connect(string port) + { + comPort = new SerialPort(); + comPort.PortName = port; + comPort.DataBits = 8; + comPort.Parity = Parity.None; + comPort.StopBits = StopBits.One; + comPort.BaudRate = 9600; + + comPort.DataReceived += ComPort_DataReceived; + + comPort.Open(); + + return comPort.IsOpen; + } + + private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + ergs.ReceivedCommand(Read()); + } + + public Boolean Disconnect() + { + comPort.Close(); + return !comPort.IsOpen; + } + + public Boolean IsOpen() + { + if (comPort != null) + return comPort.IsOpen; + else + return false; + } + + public void Write(string input) + { + if (IsOpen()) + { + comPort.WriteLine(input); + } + } + + public string Read() + { + if (IsOpen()) + { + return comPort.ReadLine(); + } + + return ""; + } + } +} \ No newline at end of file diff --git a/ErgometerSimulator/ErgometerSimulator/CommandParser.cs b/ErgometerSimulator/ErgometerSimulator/CommandParser.cs new file mode 100644 index 0000000..7b4b1f7 --- /dev/null +++ b/ErgometerSimulator/ErgometerSimulator/CommandParser.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ErgometerSimulator +{ + class CommandParser + { + private static Random rand = new Random(); + + public static string Parse(string input) + { + string response = ""; + + switch(input) + { + case "CM": + case "RS": + response = "ACK"; + break; + case "ST": + response += rand.Next(90, 140) + "\t"; //Heartbeat + response += rand.Next(20, 40) + "\t"; //RPM + response += rand.Next(50, 350) + "\t"; //Speed + response += rand.Next(10, 999) + "\t"; //Distance + response += rand.Next(25, 400) + "\t"; //Power + response += rand.Next(5, 55) + "\t"; //Energy + response += rand.Next(0, 24) + ":" + rand.Next(0, 60) + "\t"; //Time + response += rand.Next(25, 400); //ActualPower + break; + default: + break; + } + + return response; + } + } +} diff --git a/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.Designer.cs b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.Designer.cs new file mode 100644 index 0000000..9d50884 --- /dev/null +++ b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.Designer.cs @@ -0,0 +1,77 @@ +using System.IO.Ports; + +namespace ErgometerSimulator +{ + partial class ErgometerSimulator + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.connectButton = new System.Windows.Forms.Button(); + this.ComPortBox = new System.Windows.Forms.ComboBox(); + this.SuspendLayout(); + // + // connectButton + // + this.connectButton.Location = new System.Drawing.Point(139, 12); + this.connectButton.Name = "connectButton"; + this.connectButton.Size = new System.Drawing.Size(101, 27); + this.connectButton.TabIndex = 0; + this.connectButton.Text = "Connect"; + this.connectButton.UseVisualStyleBackColor = true; + this.connectButton.Click += new System.EventHandler(this.connectButton_Click); + // + // ComPortBox + // + this.ComPortBox.FormattingEnabled = true; + this.ComPortBox.Items.AddRange(new object[] { + "COM5", + "COM4"}); + this.ComPortBox.Location = new System.Drawing.Point(12, 12); + this.ComPortBox.Name = "ComPortBox"; + this.ComPortBox.Size = new System.Drawing.Size(121, 24); + this.ComPortBox.TabIndex = 1; + // + // ErgometerSimulator + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(351, 87); + this.Controls.Add(this.ComPortBox); + this.Controls.Add(this.connectButton); + this.Name = "ErgometerSimulator"; + this.Text = "Ergometer Simulator"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button connectButton; + private System.Windows.Forms.ComboBox ComPortBox; + } +} + diff --git a/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.cs b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.cs new file mode 100644 index 0000000..375c94b --- /dev/null +++ b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.cs @@ -0,0 +1,54 @@ +using System; +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; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ErgometerSimulator +{ + public partial class ErgometerSimulator : Form + { + ComPort comPort; + + public ErgometerSimulator() + { + InitializeComponent(); + comPort = new ComPort(this); + } + + private void connectButton_Click(object sender, EventArgs e) + { + if (!comPort.IsOpen()) + { + if (comPort.Connect(ComPortBox.Text)) + { + connectButton.Text = "Disconnect"; + ComPortBox.Enabled = false; + } + } + else + { + if (comPort.Disconnect()) + { + connectButton.Text = "Connect"; + ComPortBox.Enabled = true; + + } + } + } + + public void ReceivedCommand(string command) + { + string response = CommandParser.Parse(command); + string display = command + " :_: " + response; + Console.WriteLine(display); + comPort.Write(response); + } + } +} diff --git a/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.csproj b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.csproj index db58146..5e97215 100644 --- a/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.csproj +++ b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.csproj @@ -46,16 +46,18 @@ - + + + Form - - MainWindow.cs + + ErgometerSimulator.cs - - MainWindow.cs + + ErgometerSimulator.cs ResXFileCodeGenerator diff --git a/ErgometerSimulator/ErgometerSimulator/MainWindow.resx b/ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.resx similarity index 100% rename from ErgometerSimulator/ErgometerSimulator/MainWindow.resx rename to ErgometerSimulator/ErgometerSimulator/ErgometerSimulator.resx diff --git a/ErgometerSimulator/ErgometerSimulator/MainWindow.Designer.cs b/ErgometerSimulator/ErgometerSimulator/MainWindow.Designer.cs deleted file mode 100644 index e8d5ffb..0000000 --- a/ErgometerSimulator/ErgometerSimulator/MainWindow.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -namespace ErgometerSimulator -{ - partial class MainWindow - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.SuspendLayout(); - // - // MainWindow - // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(370, 253); - this.Name = "MainWindow"; - this.Text = "Ergometer Simulator"; - this.ResumeLayout(false); - - } - - #endregion - } -} - diff --git a/ErgometerSimulator/ErgometerSimulator/MainWindow.cs b/ErgometerSimulator/ErgometerSimulator/MainWindow.cs deleted file mode 100644 index d1ed613..0000000 --- a/ErgometerSimulator/ErgometerSimulator/MainWindow.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace ErgometerSimulator -{ - public partial class MainWindow : Form - { - public MainWindow() - { - InitializeComponent(); - } - } -} diff --git a/ErgometerSimulator/ErgometerSimulator/Program.cs b/ErgometerSimulator/ErgometerSimulator/Program.cs index 78759d6..0dac9e5 100644 --- a/ErgometerSimulator/ErgometerSimulator/Program.cs +++ b/ErgometerSimulator/ErgometerSimulator/Program.cs @@ -15,25 +15,10 @@ namespace ErgometerSimulator [STAThread] static void Main() { - Console.WriteLine("Application launching..."); - StartComportConnection(); - Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainWindow()); + Application.Run(new ErgometerSimulator()); } - - private static void StartComportConnection() - { - SerialPort comPort = new SerialPort(); - comPort.PortName = "COM6"; - comPort.DataBits = 8; - comPort.Parity = Parity.None; - comPort.StopBits = StopBits.One; - comPort.BaudRate = 9600; - } - - } }