D
desperateneedofhelp
Guest
hi anyone have any idea how to create the y axis and correspond to my grid view graph. mine are made out of labels and its wrong cos it does not tally with the graph. anyone any idea? pls help!! im desperate for answers.
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;
using System.IO;
using System.IO.Ports;
using humidity;
//using HeartRateMonitor.Properties;
//using System.Threading;
namespace HeartRateMonitor
{
public partial class mainForm : Form
{
#region Public Enumerations
public enum DataMode { Text, Hex }
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
public byte[] DataBuffer;
#endregion
#region Local Variables
private SerialPort comport = new SerialPort();
private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
private bool Keyhandled = false;
public ProfilingGraph graph = new ProfilingGraph();
#endregion
public mainForm()
{
InitializeComponent();
InitializeCommunication();
RefreshComPortList();
RefreshComPortControl();
comport.DataReceived += new SerialDataReceivedEventHandler(ProcessDataReceived);
InitializeGraph();
}
#region Methods for Communication Port
private void InitializeCommunication()
{
string[] baudrate = new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
string[] databit = new string[] { "7", "8", "9" };
RefreshComPortList();
comBaudComboBox.Items.Clear();
comBaudComboBox.Items.AddRange(baudrate);
comParityComboBox.Items.Clear();
comParityComboBox.Items.AddRange(Enum.GetNames(typeof(Parity)));
comDataComboBox.Items.Clear();
comDataComboBox.Items.AddRange(databit);
comStopComboBox.Items.Clear();
comStopComboBox.Items.AddRange(Enum.GetNames(typeof(StopBits)));
comBaudComboBox.Text = ("9600");
comParityComboBox.Text = ("None");
comDataComboBox.Text = ("8");
comStopComboBox.Text = ("One");
}
private string[] OrderedPortNames()
{
int num;
return SerialPort.GetPortNames().OrderBy(a => a.Length > 3 && int.TryParse(a.Substring(3), out num) ? num : 0).ToArray();
}
private void RefreshComPortList()
{
string selected = RefreshComPortList(comPortComboBox.Items.Cast<string>(), comPortComboBox.SelectedItem as string, comport.IsOpen);
if (!String.IsNullOrEmpty(selected))
{
comPortComboBox.Items.Clear();
comPortComboBox.Items.AddRange(OrderedPortNames());
comPortComboBox.SelectedItem = selected;
}
}
private string RefreshComPortList(IEnumerable<string> PreviousPortNames, string CurrentSelection, bool PortOpen)
{
string selected = null;
string[] ports = SerialPort.GetPortNames();
bool updated = PreviousPortNames.Except(ports).Count() > 0 || ports.Except(PreviousPortNames).Count() > 0;
if (updated)
{
ports = OrderedPortNames();
string newest = SerialPort.GetPortNames().Except(PreviousPortNames).OrderBy(a => a).LastOrDefault();
if (PortOpen)
{
if (ports.Contains(CurrentSelection)) selected = CurrentSelection;
else if (!String.IsNullOrEmpty(newest)) selected = newest;
else selected = ports.LastOrDefault();
}
else
{
if (!String.IsNullOrEmpty(newest)) selected = newest;
else if (ports.Contains(CurrentSelection)) selected = CurrentSelection;
else selected = ports.LastOrDefault();
}
}
return selected;
}
private void RefreshComPortControl()
{
if (comport.IsOpen) comOpenButton.Text = "&Close Port";
else comOpenButton.Text = "&Open Port";;
}
#endregion
#region Methods for Logging
private void Log(LogMsgType msgtype, string msg)
{
logRichTextBox.Invoke(new EventHandler(delegate
{
logRichTextBox.SelectedText = string.Empty;
logRichTextBox.SelectionFont = new Font(logRichTextBox.SelectionFont, FontStyle.Bold);
logRichTextBox.SelectionColor = LogMsgTypeColor[(int)msgtype];
logRichTextBox.AppendText(msg);
logRichTextBox.ScrollToCaret();
}));
}
private void BPM(Color color, string msg)
{
heartrateRichTextBox.Invoke(new EventHandler(delegate
{
heartrateRichTextBox.SelectedText = string.Empty;
heartrateRichTextBox.SelectionFont = new Font(heartrateRichTextBox.SelectionFont, FontStyle.Bold );
heartrateRichTextBox.BackColor = color;
heartrateRichTextBox.AppendText(msg);
heartrateRichTextBox.ScrollToCaret();
}));
}
private void logClearButton_Click(object sender, EventArgs e)
{
logRichTextBox.Clear();
}
#endregion
#region Methods for Data Processing
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, 0).PadRight(3, ));
return sb.ToString().ToUpper();
}
private void ProcessDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!comport.IsOpen) return;
int bytes = comport.BytesToRead;
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
Log(LogMsgType.Incoming, ByteArrayToHexString(buffer) + "\n");
if (Keyhandled == true && buffer[0] == 128 && buffer[1] == 07 && buffer[2] == 04 && buffer[3] == 05 && buffer[4] == 00)
{
int MHR;
int LightMax;
int LightMin;
int HardMax;
int HardMin;
int test = Convert.ToInt16(detailsAgeTextBox.Text);
MHR = 220 - test;
LightMax = 70 * MHR;
LightMax = LightMax / 100;
LightMin = 60 * MHR;
LightMin = LightMin / 100;
HardMax = 90 * MHR;
HardMax = HardMax / 100;
HardMin = 80 * MHR;
HardMin = HardMin / 100;
if (buffer[10] >= LightMin && buffer[10] <= LightMax)
{
BPM(Color.Blue, "\n" + buffer[10] + " bpm");
}
else if (buffer[10] >= LightMax && buffer[10] <= HardMin)
{
BPM(Color.Green, "\n" + buffer[10] + " bpm");
}
else if (buffer[10] >= HardMin && buffer[10] <= HardMax)
{
BPM(Color.Yellow, "\n" + buffer[10] + " bpm");
}
else
{
BPM(Color.White, "\n" + buffer[10] + " bpm");
}
StreamWriter sw = new StreamWriter(detailsFileTextBox.Text, true);
sw.WriteLine(buffer[10] + "bpm");
sw.Close();
graph.Invoke(new EventHandler(delegate{
graph.UpdateGraph(buffer[10]);
}));
}
}
#endregion
#region Methods for Events
private void comOpenButton_Click(object sender, EventArgs e)
{
bool comError = false;
if (comport.IsOpen)
{
comport.Close();
}
else
{
comport.BaudRate = int.Parse(comBaudComboBox.Text);
comport.DataBits = int.Parse(comDataComboBox.Text);
comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comStopComboBox.Text);
comport.Parity = (Parity)Enum.Parse(typeof(Parity), comParityComboBox.Text);
comport.PortName = comPortComboBox.Text;
try
{
comport.Open();
deviceGroupBox.Enabled = true;
}
catch (UnauthorizedAccessException) { comError = true; }
catch (IOException) { comError = true; }
catch (ArgumentException) { comError = true; }
if (comError)
MessageBox.Show(this, "Could not open the COM port. Most likely it is already in use or has been removed or is unavailable.", "COM Port Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
RefreshComPortControl();
}
private void deviceScanButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("0001060201");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
string[] device = new string[] { "f93e73f8c8ec", "7fd7076d8ae0", "6904054012cd", };
deviceComboBox.Items.Clear();
deviceComboBox.Items.AddRange(device);
deviceComboBox.SelectedIndex = 0;
deviceSelectButton.Enabled = true;
comGroupBox.Enabled = false;
deviceSelectButton.Enabled = true;
}
private void deviceSelectButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("000f0603" + deviceComboBox.Text + "013c003c0064000000");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
deviceScanButton.Enabled = false;
detailsGroupBox.Enabled = true;
}
private void deviceDisconnectButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("0001030000");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
comGroupBox.Enabled = true;
deviceScanButton.Enabled = true;
deviceComboBox.Enabled = true;
deviceSelectButton.Enabled = true;
detailsGroupBox.Enabled = false;
deviceDisconnectButton.Enabled = false;
}
private void heartRateStartButton_Click(object sender, EventArgs e)
{
if (detailsNameTextBox.Text == "")
{
MessageBox.Show("Please enter Name!");
}
else if (detailsAgeTextBox.Text == "")
{
MessageBox.Show("Please enter Age!");
}
else
{
if (detailsFileTextBox.Text == "") {
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Text Documents|*.txt";
savefile.Title = "Save a Text Documents";
savefile.ShowDialog();
detailsFileTextBox.Text = savefile.FileName;
}
if (detailsFileTextBox.Text == "")
{
MessageBox.Show("Error saving file, please try again!");
return;
}
else
{
StreamWriter sw = new StreamWriter(detailsFileTextBox.Text, true);
sw.WriteLine("Name : " + detailsNameTextBox.Text);
sw.WriteLine("Age : " + detailsAgeTextBox.Text + "\n");
sw.Close();
}
byte[] data = HexStringToByteArray("000504050018000101");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
Keyhandled = true;
deviceScanButton.Enabled = false;
deviceSelectButton.Enabled = false;
deviceComboBox.Enabled = false;
deviceDisconnectButton.Enabled = true;
detailsGroupBox.Enabled = false;
}
}
private void detailsAgeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
MessageBox.Show("Please enter numbers!");
}
}
#endregion
#region Method for Graphing
private void InitializeGraph()
{
foreach (Control control in this.Controls)
{
MdiClient client = control as MdiClient;
if (!(client == null))
{
client.BackColor = this.BackColor;
}
}
graph.MdiParent = this;
graph.FormBorderStyle = FormBorderStyle.None;
graph.StartPosition = FormStartPosition.Manual;
graph.Location = new Point(50, 270);
graph.Size = new Size(475, 280);
graph.Show();
}
#endregion
private void buttonCheck_Click(object sender, EventArgs e)
{
var myForm = new BMI();
myForm.Show();
}
}
}
i have actually added another code block. it is visual basic. i actually combined them in my project. my display name is desperateneedforhelp.
Continue reading...
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;
using System.IO;
using System.IO.Ports;
using humidity;
//using HeartRateMonitor.Properties;
//using System.Threading;
namespace HeartRateMonitor
{
public partial class mainForm : Form
{
#region Public Enumerations
public enum DataMode { Text, Hex }
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
public byte[] DataBuffer;
#endregion
#region Local Variables
private SerialPort comport = new SerialPort();
private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
private bool Keyhandled = false;
public ProfilingGraph graph = new ProfilingGraph();
#endregion
public mainForm()
{
InitializeComponent();
InitializeCommunication();
RefreshComPortList();
RefreshComPortControl();
comport.DataReceived += new SerialDataReceivedEventHandler(ProcessDataReceived);
InitializeGraph();
}
#region Methods for Communication Port
private void InitializeCommunication()
{
string[] baudrate = new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
string[] databit = new string[] { "7", "8", "9" };
RefreshComPortList();
comBaudComboBox.Items.Clear();
comBaudComboBox.Items.AddRange(baudrate);
comParityComboBox.Items.Clear();
comParityComboBox.Items.AddRange(Enum.GetNames(typeof(Parity)));
comDataComboBox.Items.Clear();
comDataComboBox.Items.AddRange(databit);
comStopComboBox.Items.Clear();
comStopComboBox.Items.AddRange(Enum.GetNames(typeof(StopBits)));
comBaudComboBox.Text = ("9600");
comParityComboBox.Text = ("None");
comDataComboBox.Text = ("8");
comStopComboBox.Text = ("One");
}
private string[] OrderedPortNames()
{
int num;
return SerialPort.GetPortNames().OrderBy(a => a.Length > 3 && int.TryParse(a.Substring(3), out num) ? num : 0).ToArray();
}
private void RefreshComPortList()
{
string selected = RefreshComPortList(comPortComboBox.Items.Cast<string>(), comPortComboBox.SelectedItem as string, comport.IsOpen);
if (!String.IsNullOrEmpty(selected))
{
comPortComboBox.Items.Clear();
comPortComboBox.Items.AddRange(OrderedPortNames());
comPortComboBox.SelectedItem = selected;
}
}
private string RefreshComPortList(IEnumerable<string> PreviousPortNames, string CurrentSelection, bool PortOpen)
{
string selected = null;
string[] ports = SerialPort.GetPortNames();
bool updated = PreviousPortNames.Except(ports).Count() > 0 || ports.Except(PreviousPortNames).Count() > 0;
if (updated)
{
ports = OrderedPortNames();
string newest = SerialPort.GetPortNames().Except(PreviousPortNames).OrderBy(a => a).LastOrDefault();
if (PortOpen)
{
if (ports.Contains(CurrentSelection)) selected = CurrentSelection;
else if (!String.IsNullOrEmpty(newest)) selected = newest;
else selected = ports.LastOrDefault();
}
else
{
if (!String.IsNullOrEmpty(newest)) selected = newest;
else if (ports.Contains(CurrentSelection)) selected = CurrentSelection;
else selected = ports.LastOrDefault();
}
}
return selected;
}
private void RefreshComPortControl()
{
if (comport.IsOpen) comOpenButton.Text = "&Close Port";
else comOpenButton.Text = "&Open Port";;
}
#endregion
#region Methods for Logging
private void Log(LogMsgType msgtype, string msg)
{
logRichTextBox.Invoke(new EventHandler(delegate
{
logRichTextBox.SelectedText = string.Empty;
logRichTextBox.SelectionFont = new Font(logRichTextBox.SelectionFont, FontStyle.Bold);
logRichTextBox.SelectionColor = LogMsgTypeColor[(int)msgtype];
logRichTextBox.AppendText(msg);
logRichTextBox.ScrollToCaret();
}));
}
private void BPM(Color color, string msg)
{
heartrateRichTextBox.Invoke(new EventHandler(delegate
{
heartrateRichTextBox.SelectedText = string.Empty;
heartrateRichTextBox.SelectionFont = new Font(heartrateRichTextBox.SelectionFont, FontStyle.Bold );
heartrateRichTextBox.BackColor = color;
heartrateRichTextBox.AppendText(msg);
heartrateRichTextBox.ScrollToCaret();
}));
}
private void logClearButton_Click(object sender, EventArgs e)
{
logRichTextBox.Clear();
}
#endregion
#region Methods for Data Processing
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, 0).PadRight(3, ));
return sb.ToString().ToUpper();
}
private void ProcessDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!comport.IsOpen) return;
int bytes = comport.BytesToRead;
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
Log(LogMsgType.Incoming, ByteArrayToHexString(buffer) + "\n");
if (Keyhandled == true && buffer[0] == 128 && buffer[1] == 07 && buffer[2] == 04 && buffer[3] == 05 && buffer[4] == 00)
{
int MHR;
int LightMax;
int LightMin;
int HardMax;
int HardMin;
int test = Convert.ToInt16(detailsAgeTextBox.Text);
MHR = 220 - test;
LightMax = 70 * MHR;
LightMax = LightMax / 100;
LightMin = 60 * MHR;
LightMin = LightMin / 100;
HardMax = 90 * MHR;
HardMax = HardMax / 100;
HardMin = 80 * MHR;
HardMin = HardMin / 100;
if (buffer[10] >= LightMin && buffer[10] <= LightMax)
{
BPM(Color.Blue, "\n" + buffer[10] + " bpm");
}
else if (buffer[10] >= LightMax && buffer[10] <= HardMin)
{
BPM(Color.Green, "\n" + buffer[10] + " bpm");
}
else if (buffer[10] >= HardMin && buffer[10] <= HardMax)
{
BPM(Color.Yellow, "\n" + buffer[10] + " bpm");
}
else
{
BPM(Color.White, "\n" + buffer[10] + " bpm");
}
StreamWriter sw = new StreamWriter(detailsFileTextBox.Text, true);
sw.WriteLine(buffer[10] + "bpm");
sw.Close();
graph.Invoke(new EventHandler(delegate{
graph.UpdateGraph(buffer[10]);
}));
}
}
#endregion
#region Methods for Events
private void comOpenButton_Click(object sender, EventArgs e)
{
bool comError = false;
if (comport.IsOpen)
{
comport.Close();
}
else
{
comport.BaudRate = int.Parse(comBaudComboBox.Text);
comport.DataBits = int.Parse(comDataComboBox.Text);
comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comStopComboBox.Text);
comport.Parity = (Parity)Enum.Parse(typeof(Parity), comParityComboBox.Text);
comport.PortName = comPortComboBox.Text;
try
{
comport.Open();
deviceGroupBox.Enabled = true;
}
catch (UnauthorizedAccessException) { comError = true; }
catch (IOException) { comError = true; }
catch (ArgumentException) { comError = true; }
if (comError)
MessageBox.Show(this, "Could not open the COM port. Most likely it is already in use or has been removed or is unavailable.", "COM Port Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
RefreshComPortControl();
}
private void deviceScanButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("0001060201");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
string[] device = new string[] { "f93e73f8c8ec", "7fd7076d8ae0", "6904054012cd", };
deviceComboBox.Items.Clear();
deviceComboBox.Items.AddRange(device);
deviceComboBox.SelectedIndex = 0;
deviceSelectButton.Enabled = true;
comGroupBox.Enabled = false;
deviceSelectButton.Enabled = true;
}
private void deviceSelectButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("000f0603" + deviceComboBox.Text + "013c003c0064000000");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
deviceScanButton.Enabled = false;
detailsGroupBox.Enabled = true;
}
private void deviceDisconnectButton_Click(object sender, EventArgs e)
{
byte[] data = HexStringToByteArray("0001030000");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
comGroupBox.Enabled = true;
deviceScanButton.Enabled = true;
deviceComboBox.Enabled = true;
deviceSelectButton.Enabled = true;
detailsGroupBox.Enabled = false;
deviceDisconnectButton.Enabled = false;
}
private void heartRateStartButton_Click(object sender, EventArgs e)
{
if (detailsNameTextBox.Text == "")
{
MessageBox.Show("Please enter Name!");
}
else if (detailsAgeTextBox.Text == "")
{
MessageBox.Show("Please enter Age!");
}
else
{
if (detailsFileTextBox.Text == "") {
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Text Documents|*.txt";
savefile.Title = "Save a Text Documents";
savefile.ShowDialog();
detailsFileTextBox.Text = savefile.FileName;
}
if (detailsFileTextBox.Text == "")
{
MessageBox.Show("Error saving file, please try again!");
return;
}
else
{
StreamWriter sw = new StreamWriter(detailsFileTextBox.Text, true);
sw.WriteLine("Name : " + detailsNameTextBox.Text);
sw.WriteLine("Age : " + detailsAgeTextBox.Text + "\n");
sw.Close();
}
byte[] data = HexStringToByteArray("000504050018000101");
comport.Write(data, 0, data.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
Keyhandled = true;
deviceScanButton.Enabled = false;
deviceSelectButton.Enabled = false;
deviceComboBox.Enabled = false;
deviceDisconnectButton.Enabled = true;
detailsGroupBox.Enabled = false;
}
}
private void detailsAgeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
MessageBox.Show("Please enter numbers!");
}
}
#endregion
#region Method for Graphing
private void InitializeGraph()
{
foreach (Control control in this.Controls)
{
MdiClient client = control as MdiClient;
if (!(client == null))
{
client.BackColor = this.BackColor;
}
}
graph.MdiParent = this;
graph.FormBorderStyle = FormBorderStyle.None;
graph.StartPosition = FormStartPosition.Manual;
graph.Location = new Point(50, 270);
graph.Size = new Size(475, 280);
graph.Show();
}
#endregion
private void buttonCheck_Click(object sender, EventArgs e)
{
var myForm = new BMI();
myForm.Show();
}
}
}
i have actually added another code block. it is visual basic. i actually combined them in my project. my display name is desperateneedforhelp.
Continue reading...