First working simulator version

This commit is contained in:
2015-09-05 21:41:32 +02:00
parent 021445bb38
commit 854d183637
9 changed files with 253 additions and 88 deletions
@@ -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 "";
}
}
}
@@ -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;
}
}
}
@@ -0,0 +1,77 @@
using System.IO.Ports;
namespace ErgometerSimulator
{
partial class ErgometerSimulator
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}
@@ -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);
}
}
}
@@ -46,16 +46,18 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainWindow.cs">
<Compile Include="CommandParser.cs" />
<Compile Include="ComPort.cs" />
<Compile Include="ErgometerSimulator.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainWindow.Designer.cs">
<DependentUpon>MainWindow.cs</DependentUpon>
<Compile Include="ErgometerSimulator.Designer.cs">
<DependentUpon>ErgometerSimulator.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainWindow.resx">
<DependentUpon>MainWindow.cs</DependentUpon>
<EmbeddedResource Include="ErgometerSimulator.resx">
<DependentUpon>ErgometerSimulator.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
@@ -1,47 +0,0 @@
namespace ErgometerSimulator
{
partial class MainWindow
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}
@@ -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();
}
}
}
@@ -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;
}
}
}