#pragma once
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "Filter.h"

HANDLE PortCOM;
HANDLE MainThread;

bool OnlineFilter = false;

bool AxeInverse = false;

float PlotsData[6];
bool PlotsReady = false;

struct TeleLog
{
	short GyrX, GyrY, GyrZ;
	short AccX, AccY, AccZ;
};

int TeleAxe = 0;

void SaveLogFile(unsigned char data[12], unsigned long size)
{
	TeleLog log;
	memcpy(&log, data, size);

	PlotsData[0] = log.GyrX; PlotsData[3] = log.AccX;
	PlotsData[1] = log.GyrY; PlotsData[4] = log.AccY;
	PlotsData[2] = log.GyrZ; PlotsData[5] = log.AccZ;

	PlotsReady = true;
}

unsigned long __stdcall RecvThread(void*)
{
	long wait = 0;

	private enum class RecvModeEnum : char { Begin, Data };

	bool mode = false;

	unsigned char data[13];
	unsigned long size;

	unsigned long len;

	while (1)
	{
		if (!PortCOM)
		{
			CloseHandle(MainThread);
			MainThread = 0;
			return 0;
		}

		if (wait < GetTickCount()) mode = false;

		if (!mode)
		{
			wait = GetTickCount() + 20;

			ReadFile(PortCOM, data, 1, &len, 0);
			if (!len) continue;
			if (data[0] == 'Z') mode = true;
			size = 0;
		}

		if (mode)
		{
			ReadFile(PortCOM, data + size, sizeof(data) - size, &len, 0);
			if (!len) continue;
			size += len;
			if (size < sizeof(data)) continue;

			if (data[12] == 'V') SaveLogFile(data, sizeof(data) - 1);

			mode = false;
		}

	}
}

namespace HexapodControl
{

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		
		String^ RecordsFolder = "";
	private: System::Windows::Forms::OpenFileDialog^ openFileDialog_Open;

	private: System::Windows::Forms::GroupBox^ groupBox1;
	private: System::Windows::Forms::CheckedListBox^ checkedListBox_List;
	private: System::Windows::Forms::CheckBox^ checkBox_Auto;
	private: System::Windows::Forms::Label^ label2;
	private: System::Windows::Forms::Label^ label1;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Graph_MinY;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Track;
	private: System::Windows::Forms::Label^ label3;




























































	private: System::Windows::Forms::Panel^ panel3;
	private: System::Windows::Forms::Panel^ panel4;


	private: System::Windows::Forms::Button^ button_Connect;

	private: System::Windows::Forms::ComboBox^ comboBox_Port;
	private: System::Windows::Forms::Timer^ timer_Tick;



	private: System::Windows::Forms::ComboBox^ comboBox_Baud;





	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Graph_MaxY;

	protected:

	protected:
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::DataVisualization::Charting::Chart^ chart_Graph;
	protected:


	private: System::Windows::Forms::Panel^ panel1;



	private: System::Windows::Forms::GroupBox^ groupBox_P1;







	private: System::ComponentModel::IContainer^ components;
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma 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>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::Windows::Forms::DataVisualization::Charting::ChartArea^ chartArea1 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
			System::Windows::Forms::DataVisualization::Charting::Legend^ legend1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Legend());
			System::Windows::Forms::DataVisualization::Charting::Series^ series1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series2 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series3 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series4 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series5 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series6 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			this->chart_Graph = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->panel3 = (gcnew System::Windows::Forms::Panel());
			this->comboBox_Baud = (gcnew System::Windows::Forms::ComboBox());
			this->button_Connect = (gcnew System::Windows::Forms::Button());
			this->comboBox_Port = (gcnew System::Windows::Forms::ComboBox());
			this->groupBox_P1 = (gcnew System::Windows::Forms::GroupBox());
			this->openFileDialog_Open = (gcnew System::Windows::Forms::OpenFileDialog());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->panel4 = (gcnew System::Windows::Forms::Panel());
			this->checkedListBox_List = (gcnew System::Windows::Forms::CheckedListBox());
			this->checkBox_Auto = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown_Track = (gcnew System::Windows::Forms::NumericUpDown());
			this->numericUpDown_Graph_MaxY = (gcnew System::Windows::Forms::NumericUpDown());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Graph_MinY = (gcnew System::Windows::Forms::NumericUpDown());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->timer_Tick = (gcnew System::Windows::Forms::Timer(this->components));
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Graph))->BeginInit();
			this->panel1->SuspendLayout();
			this->panel3->SuspendLayout();
			this->groupBox_P1->SuspendLayout();
			this->groupBox1->SuspendLayout();
			this->panel4->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Track))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MaxY))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MinY))->BeginInit();
			this->SuspendLayout();
			// 
			// chart_Graph
			// 
			chartArea1->Name = L"ChartArea1";
			this->chart_Graph->ChartAreas->Add(chartArea1);
			this->chart_Graph->Dock = System::Windows::Forms::DockStyle::Fill;
			legend1->Name = L"Legend1";
			this->chart_Graph->Legends->Add(legend1);
			this->chart_Graph->Location = System::Drawing::Point(3, 16);
			this->chart_Graph->Name = L"chart_Graph";
			series1->ChartArea = L"ChartArea1";
			series1->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series1->Color = System::Drawing::Color::Blue;
			series1->Legend = L"Legend1";
			series1->Name = L"GyrX";
			series1->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series1->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series2->ChartArea = L"ChartArea1";
			series2->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series2->Color = System::Drawing::Color::Red;
			series2->Legend = L"Legend1";
			series2->Name = L"GyrY";
			series2->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series2->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series3->ChartArea = L"ChartArea1";
			series3->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series3->Color = System::Drawing::Color::Fuchsia;
			series3->Legend = L"Legend1";
			series3->Name = L"GyrZ";
			series3->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series3->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series4->BorderWidth = 2;
			series4->ChartArea = L"ChartArea1";
			series4->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series4->Color = System::Drawing::Color::Black;
			series4->Legend = L"Legend1";
			series4->Name = L"AccX";
			series5->ChartArea = L"ChartArea1";
			series5->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series5->Color = System::Drawing::Color::Green;
			series5->Legend = L"Legend1";
			series5->Name = L"AccY";
			series6->BorderWidth = 2;
			series6->ChartArea = L"ChartArea1";
			series6->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series6->Color = System::Drawing::Color::Tomato;
			series6->Legend = L"Legend1";
			series6->Name = L"AccZ";
			this->chart_Graph->Series->Add(series1);
			this->chart_Graph->Series->Add(series2);
			this->chart_Graph->Series->Add(series3);
			this->chart_Graph->Series->Add(series4);
			this->chart_Graph->Series->Add(series5);
			this->chart_Graph->Series->Add(series6);
			this->chart_Graph->Size = System::Drawing::Size(689, 669);
			this->chart_Graph->TabIndex = 1;
			this->chart_Graph->Text = L"chart1";
			// 
			// panel1
			// 
			this->panel1->Controls->Add(this->panel3);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Left;
			this->panel1->Location = System::Drawing::Point(0, 0);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(135, 688);
			this->panel1->TabIndex = 2;
			// 
			// panel3
			// 
			this->panel3->Controls->Add(this->comboBox_Baud);
			this->panel3->Controls->Add(this->button_Connect);
			this->panel3->Controls->Add(this->comboBox_Port);
			this->panel3->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel3->Location = System::Drawing::Point(0, 0);
			this->panel3->Name = L"panel3";
			this->panel3->Size = System::Drawing::Size(135, 75);
			this->panel3->TabIndex = 9;
			// 
			// comboBox_Baud
			// 
			this->comboBox_Baud->FormattingEnabled = true;
			this->comboBox_Baud->Items->AddRange(gcnew cli::array< System::Object^  >(3) { L"57600", L"115200", L"230400" });
			this->comboBox_Baud->Location = System::Drawing::Point(5, 43);
			this->comboBox_Baud->Name = L"comboBox_Baud";
			this->comboBox_Baud->Size = System::Drawing::Size(124, 21);
			this->comboBox_Baud->TabIndex = 26;
			this->comboBox_Baud->Text = L"115200";
			// 
			// button_Connect
			// 
			this->button_Connect->Location = System::Drawing::Point(5, 14);
			this->button_Connect->Name = L"button_Connect";
			this->button_Connect->Size = System::Drawing::Size(64, 23);
			this->button_Connect->TabIndex = 10;
			this->button_Connect->Text = L"Connect";
			this->button_Connect->UseVisualStyleBackColor = true;
			this->button_Connect->Click += gcnew System::EventHandler(this, &Form1::button_Connect_Click);
			// 
			// comboBox_Port
			// 
			this->comboBox_Port->FormattingEnabled = true;
			this->comboBox_Port->Location = System::Drawing::Point(75, 16);
			this->comboBox_Port->Name = L"comboBox_Port";
			this->comboBox_Port->Size = System::Drawing::Size(54, 21);
			this->comboBox_Port->TabIndex = 9;
			this->comboBox_Port->DropDown += gcnew System::EventHandler(this, &Form1::comboBox_Port_DropDown);
			// 
			// groupBox_P1
			// 
			this->groupBox_P1->Controls->Add(this->chart_Graph);
			this->groupBox_P1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->groupBox_P1->Location = System::Drawing::Point(135, 0);
			this->groupBox_P1->MinimumSize = System::Drawing::Size(676, 688);
			this->groupBox_P1->Name = L"groupBox_P1";
			this->groupBox_P1->Size = System::Drawing::Size(695, 688);
			this->groupBox_P1->TabIndex = 3;
			this->groupBox_P1->TabStop = false;
			// 
			// openFileDialog_Open
			// 
			this->openFileDialog_Open->FileName = L"openFileDialog1";
			this->openFileDialog_Open->Multiselect = true;
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->panel4);
			this->groupBox1->Dock = System::Windows::Forms::DockStyle::Right;
			this->groupBox1->Location = System::Drawing::Point(830, 0);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(128, 688);
			this->groupBox1->TabIndex = 4;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L" options ";
			// 
			// panel4
			// 
			this->panel4->Controls->Add(this->checkedListBox_List);
			this->panel4->Controls->Add(this->checkBox_Auto);
			this->panel4->Controls->Add(this->numericUpDown_Track);
			this->panel4->Controls->Add(this->numericUpDown_Graph_MaxY);
			this->panel4->Controls->Add(this->label3);
			this->panel4->Controls->Add(this->numericUpDown_Graph_MinY);
			this->panel4->Controls->Add(this->label2);
			this->panel4->Controls->Add(this->label1);
			this->panel4->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel4->Location = System::Drawing::Point(3, 16);
			this->panel4->Name = L"panel4";
			this->panel4->Size = System::Drawing::Size(122, 245);
			this->panel4->TabIndex = 9;
			// 
			// checkedListBox_List
			// 
			this->checkedListBox_List->Dock = System::Windows::Forms::DockStyle::Top;
			this->checkedListBox_List->FormattingEnabled = true;
			this->checkedListBox_List->Location = System::Drawing::Point(0, 0);
			this->checkedListBox_List->Name = L"checkedListBox_List";
			this->checkedListBox_List->Size = System::Drawing::Size(122, 109);
			this->checkedListBox_List->TabIndex = 0;
			this->checkedListBox_List->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::checkedListBox_List_SelectedIndexChanged);
			this->checkedListBox_List->DoubleClick += gcnew System::EventHandler(this, &Form1::checkedListBox_List_SelectedIndexChanged);
			// 
			// checkBox_Auto
			// 
			this->checkBox_Auto->AutoSize = true;
			this->checkBox_Auto->Checked = true;
			this->checkBox_Auto->CheckState = System::Windows::Forms::CheckState::Checked;
			this->checkBox_Auto->Location = System::Drawing::Point(20, 118);
			this->checkBox_Auto->Name = L"checkBox_Auto";
			this->checkBox_Auto->Size = System::Drawing::Size(47, 17);
			this->checkBox_Auto->TabIndex = 1;
			this->checkBox_Auto->Text = L"auto";
			this->checkBox_Auto->UseVisualStyleBackColor = true;
			this->checkBox_Auto->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Auto_CheckedChanged);
			// 
			// numericUpDown_Track
			// 
			this->numericUpDown_Track->Location = System::Drawing::Point(44, 203);
			this->numericUpDown_Track->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1000, 0, 0, 0 });
			this->numericUpDown_Track->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Track->Name = L"numericUpDown_Track";
			this->numericUpDown_Track->Size = System::Drawing::Size(57, 20);
			this->numericUpDown_Track->TabIndex = 7;
			this->numericUpDown_Track->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 200, 0, 0, 0 });
			// 
			// numericUpDown_Graph_MaxY
			// 
			this->numericUpDown_Graph_MaxY->Location = System::Drawing::Point(53, 143);
			this->numericUpDown_Graph_MaxY->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10000, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->Name = L"numericUpDown_Graph_MaxY";
			this->numericUpDown_Graph_MaxY->Size = System::Drawing::Size(60, 20);
			this->numericUpDown_Graph_MaxY->TabIndex = 2;
			this->numericUpDown_Graph_MaxY->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 3, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Graph_MaxY_ValueChanged);
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(6, 205);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(38, 13);
			this->label3->TabIndex = 6;
			this->label3->Text = L"Track:";
			// 
			// numericUpDown_Graph_MinY
			// 
			this->numericUpDown_Graph_MinY->Location = System::Drawing::Point(53, 169);
			this->numericUpDown_Graph_MinY->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10000, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->Name = L"numericUpDown_Graph_MinY";
			this->numericUpDown_Graph_MinY->Size = System::Drawing::Size(60, 20);
			this->numericUpDown_Graph_MinY->TabIndex = 3;
			this->numericUpDown_Graph_MinY->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 3, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Graph_MaxY_ValueChanged);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(9, 171);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(37, 13);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Min Y:";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(6, 145);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(40, 13);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Max Y:";
			// 
			// timer_Tick
			// 
			this->timer_Tick->Interval = 1;
			this->timer_Tick->Tick += gcnew System::EventHandler(this, &Form1::timer_Tick_Tick);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(958, 688);
			this->Controls->Add(this->groupBox_P1);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->panel1);
			this->MinimumSize = System::Drawing::Size(974, 727);
			this->Name = L"Form1";
			this->Text = L"Filter Tuning GPS";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Graph))->EndInit();
			this->panel1->ResumeLayout(false);
			this->panel3->ResumeLayout(false);
			this->groupBox_P1->ResumeLayout(false);
			this->groupBox1->ResumeLayout(false);
			this->panel4->ResumeLayout(false);
			this->panel4->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Track))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MaxY))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MinY))->EndInit();
			this->ResumeLayout(false);

		}
#pragma endregion

	  private: void ToCharString(String^ str1, char* str2)
	  {
	  	for (int a = 0; a < str1->Length; a++) *str2++ = str1[a];
	  	*str2 = '\0';
	  }

    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) 
		{
			comboBox_Port_DropDown(sender, e);
			if (comboBox_Port->Items->Count > 0) comboBox_Port->Text = comboBox_Port->Items[0]->ToString();

			for each (auto s in chart_Graph->Series)
			{
				checkedListBox_List->Items->Add(s->Name, true);
			}

			numericUpDown_Graph_MaxY_ValueChanged(sender, e);
    }

		private: System::Collections::Generic::List<float>^ MainDataVaildGPS = gcnew System::Collections::Generic::List<float>();
		private: System::Collections::Generic::List<array<float>^>^ MainDataRecord = gcnew System::Collections::Generic::List<array<float>^>();


	  private: System::Void checkedListBox_List_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) 
		{
			for (int a = 0; a < checkedListBox_List->Items->Count; a++)
			{
				chart_Graph->Series[a]->Enabled = checkedListBox_List->GetItemChecked(a);
			}
	  }

    private: System::Void checkBox_Auto_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			if (checkBox_Auto->Checked)
			{
				chart_Graph->ChartAreas[0]->AxisY->Interval = double::NaN;
				chart_Graph->ChartAreas[0]->AxisY->Minimum = double::NaN;
				chart_Graph->ChartAreas[0]->AxisY->Maximum = double::NaN;
				numericUpDown_Graph_MinY->Enabled = false;
				numericUpDown_Graph_MaxY->Enabled = false;
			}
			else
			{
				chart_Graph->ChartAreas[0]->AxisY->Interval = double::NaN;
				numericUpDown_Graph_MaxY_ValueChanged(sender, e);
				numericUpDown_Graph_MinY->Enabled = true;
				numericUpDown_Graph_MaxY->Enabled = true;
			}
    }
    private: System::Void numericUpDown_Graph_MaxY_ValueChanged(System::Object^ sender, System::EventArgs^ e)
		{
			if (checkBox_Auto->Checked) return;
			chart_Graph->ChartAreas[0]->AxisY->Minimum = (double)numericUpDown_Graph_MinY->Value;
			chart_Graph->ChartAreas[0]->AxisY->Maximum = (double)numericUpDown_Graph_MaxY->Value;
    }

    private: System::Void comboBox_Port_DropDown(System::Object^ sender, System::EventArgs^ e)
		{
			comboBox_Port->Items->Clear();
			for each (String ^ p in System::IO::Ports::SerialPort::GetPortNames())
			{
				comboBox_Port->Items->Add(p);
			}
    }

    private: System::Void button_Connect_Click(System::Object^ sender, System::EventArgs^ e)
		{
			if (button_Connect->Text == "Connect")
			{
				long baud;
				try
				{
					baud = Convert::ToInt32(comboBox_Baud->Text);
					comboBox_Baud->Text = baud.ToString();
				}
				catch (...)
				{
					MessageBox::Show("Error to set baud", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
					return;
				}

				char text[128] = "\\\\.\\";

				ToCharString(comboBox_Port->Text, text + 4);

				PortCOM = CreateFileA(text, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
				if (PortCOM == INVALID_HANDLE_VALUE)
				{
					PortCOM = 0;
					MessageBox::Show("Error to connect", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
					return;
				}
				//---
				DCB serial = { sizeof(serial) };
				GetCommState(PortCOM, &serial);
				serial.BaudRate = baud;
				serial.ByteSize = 8;
				serial.StopBits = ONESTOPBIT;
				serial.Parity = NOPARITY;
				SetCommState(PortCOM, &serial);
				//---
				COMMTIMEOUTS tim;
				GetCommTimeouts(PortCOM, &tim);
				tim.ReadIntervalTimeout = 1;
				tim.ReadTotalTimeoutConstant = 1;
				tim.ReadTotalTimeoutMultiplier = 1;
				SetCommTimeouts(PortCOM, &tim);
				//---
				timer_Tick->Enabled = true;
				button_Connect->Text = "Disonnect";
				//---
				for (int a = 0; a < chart_Graph->Series->Count; a++) chart_Graph->Series[a]->Points->Clear();
				//---
				MainThread = CreateThread(0, 0, RecvThread, 0, 0, 0);
			}
			else
			{
				CloseHandle(PortCOM);
				PortCOM = 0;
				timer_Tick->Enabled = false;
				button_Connect->Text = "Connect";
			}
    }

    private: System::Void timer_Tick_Tick(System::Object^ sender, System::EventArgs^ e)
		{
			if (!PlotsReady) return;
			PlotsReady = false;

			for (int a = 0; a < chart_Graph->Series->Count; a++)
			{
				chart_Graph->Series[a]->Points->AddY((double)PlotsData[a]);
				while (chart_Graph->Series[a]->Points->Count > numericUpDown_Track->Value) chart_Graph->Series[a]->Points->RemoveAt(0);
			}
    }
};
}
