#pragma once

#include <time.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

namespace power_supply {

	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::Ports;
	using namespace System::IO;
	using namespace msclr::interop;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TabControl^  tabControl1;
	protected: 
	private: System::Windows::Forms::TabPage^  tabPage1;
	private: System::Windows::Forms::TabPage^  tabPage2;
	private: System::Windows::Forms::TabPage^  tabPage3;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  menuToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  connectionToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  connectToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  disconnectToolStripMenuItem;
	private: System::Windows::Forms::CheckBox^  checkBox1;
	private: System::Windows::Forms::GroupBox^  groupBox2;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::RadioButton^  radioButton2;
	private: System::Windows::Forms::RadioButton^  radioButton1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Label^  label2;

	private: System::Windows::Forms::CheckBox^  checkBox2;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::TextBox^  textBox7;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::StatusStrip^  statusStrip1;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::Label^  label9;
	private: System::Windows::Forms::TextBox^  textBox9;
	private: System::Windows::Forms::TextBox^  textBox8;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::GroupBox^  groupBox3;
	private: System::Windows::Forms::GroupBox^  groupBox4;
	private: System::Windows::Forms::TextBox^  textBox11;
	private: System::Windows::Forms::TextBox^  textBox10;
	private: System::Windows::Forms::Label^  label12;
	private: System::Windows::Forms::Label^  label11;
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::ToolStripStatusLabel^  toolStripStatusLabel1;
	private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;

	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());
			this->tabControl1 = (gcnew System::Windows::Forms::TabControl());
			this->tabPage1 = (gcnew System::Windows::Forms::TabPage());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->tabPage2 = (gcnew System::Windows::Forms::TabPage());
			this->groupBox4 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox11 = (gcnew System::Windows::Forms::TextBox());
			this->textBox10 = (gcnew System::Windows::Forms::TextBox());
			this->label12 = (gcnew System::Windows::Forms::Label());
			this->label11 = (gcnew System::Windows::Forms::Label());
			this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->textBox7 = (gcnew System::Windows::Forms::TextBox());
			this->textBox9 = (gcnew System::Windows::Forms::TextBox());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->textBox8 = (gcnew System::Windows::Forms::TextBox());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->tabPage3 = (gcnew System::Windows::Forms::TabPage());
			this->checkBox2 = (gcnew System::Windows::Forms::CheckBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->menuToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->connectionToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->connectToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->disconnectToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip());
			this->toolStripStatusLabel1 = (gcnew System::Windows::Forms::ToolStripStatusLabel());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
			this->tabControl1->SuspendLayout();
			this->tabPage1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->groupBox1->SuspendLayout();
			this->tabPage2->SuspendLayout();
			this->groupBox4->SuspendLayout();
			this->groupBox3->SuspendLayout();
			this->tabPage3->SuspendLayout();
			this->menuStrip1->SuspendLayout();
			this->statusStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// tabControl1
			// 
			this->tabControl1->Controls->Add(this->tabPage1);
			this->tabControl1->Controls->Add(this->tabPage2);
			this->tabControl1->Controls->Add(this->tabPage3);
			this->tabControl1->Location = System::Drawing::Point(12, 27);
			this->tabControl1->Name = L"tabControl1";
			this->tabControl1->SelectedIndex = 0;
			this->tabControl1->Size = System::Drawing::Size(512, 485);
			this->tabControl1->TabIndex = 0;
			// 
			// tabPage1
			// 
			this->tabPage1->Controls->Add(this->groupBox2);
			this->tabPage1->Controls->Add(this->groupBox1);
			this->tabPage1->Location = System::Drawing::Point(4, 22);
			this->tabPage1->Name = L"tabPage1";
			this->tabPage1->Padding = System::Windows::Forms::Padding(3);
			this->tabPage1->Size = System::Drawing::Size(504, 459);
			this->tabPage1->TabIndex = 0;
			this->tabPage1->Text = L"Ouptut";
			this->tabPage1->UseVisualStyleBackColor = true;
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->textBox5);
			this->groupBox2->Controls->Add(this->label5);
			this->groupBox2->Controls->Add(this->label4);
			this->groupBox2->Controls->Add(this->textBox4);
			this->groupBox2->Controls->Add(this->textBox3);
			this->groupBox2->Controls->Add(this->label3);
			this->groupBox2->Location = System::Drawing::Point(6, 180);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(491, 106);
			this->groupBox2->TabIndex = 4;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"Read";
			// 
			// textBox5
			// 
			this->textBox5->Location = System::Drawing::Point(92, 75);
			this->textBox5->MaxLength = 5;
			this->textBox5->Name = L"textBox5";
			this->textBox5->ReadOnly = true;
			this->textBox5->Size = System::Drawing::Size(100, 20);
			this->textBox5->TabIndex = 12;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(7, 78);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(63, 13);
			this->label5->TabIndex = 11;
			this->label5->Text = L"Power [W] :";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(7, 51);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(71, 13);
			this->label4->TabIndex = 10;
			this->label4->Text = L"Current [mA] :";
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(92, 48);
			this->textBox4->MaxLength = 5;
			this->textBox4->Name = L"textBox4";
			this->textBox4->ReadOnly = true;
			this->textBox4->Size = System::Drawing::Size(100, 20);
			this->textBox4->TabIndex = 9;
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(92, 22);
			this->textBox3->MaxLength = 5;
			this->textBox3->Name = L"textBox3";
			this->textBox3->ReadOnly = true;
			this->textBox3->Size = System::Drawing::Size(100, 20);
			this->textBox3->TabIndex = 8;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(7, 25);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(65, 13);
			this->label3->TabIndex = 5;
			this->label3->Text = L"Voltage [V] :";
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->button1);
			this->groupBox1->Controls->Add(this->radioButton2);
			this->groupBox1->Controls->Add(this->radioButton1);
			this->groupBox1->Controls->Add(this->textBox2);
			this->groupBox1->Controls->Add(this->label2);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Controls->Add(this->checkBox1);
			this->groupBox1->Controls->Add(this->textBox1);
			this->groupBox1->Location = System::Drawing::Point(6, 6);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(492, 168);
			this->groupBox1->TabIndex = 3;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Set";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(94, 70);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 10;
			this->button1->Text = L"Send data";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// radioButton2
			// 
			this->radioButton2->AutoSize = true;
			this->radioButton2->Location = System::Drawing::Point(10, 134);
			this->radioButton2->Name = L"radioButton2";
			this->radioButton2->Size = System::Drawing::Size(73, 17);
			this->radioButton2->TabIndex = 9;
			this->radioButton2->Text = L"DC output";
			this->radioButton2->UseVisualStyleBackColor = true;
			// 
			// radioButton1
			// 
			this->radioButton1->AutoSize = true;
			this->radioButton1->Checked = true;
			this->radioButton1->Location = System::Drawing::Point(10, 111);
			this->radioButton1->Name = L"radioButton1";
			this->radioButton1->Size = System::Drawing::Size(72, 17);
			this->radioButton1->TabIndex = 8;
			this->radioButton1->TabStop = true;
			this->radioButton1->Text = L"AC output";
			this->radioButton1->UseVisualStyleBackColor = true;
			this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(92, 44);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(100, 20);
			this->textBox2->TabIndex = 7;
			this->textBox2->Text = L"3";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(7, 47);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(65, 13);
			this->label2->TabIndex = 4;
			this->label2->Text = L"Voltage [V] :";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(7, 20);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(85, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Frequency [Hz] :";
			// 
			// checkBox1
			// 
			this->checkBox1->AutoSize = true;
			this->checkBox1->Location = System::Drawing::Point(94, 111);
			this->checkBox1->Name = L"checkBox1";
			this->checkBox1->Size = System::Drawing::Size(73, 17);
			this->checkBox1->TabIndex = 2;
			this->checkBox1->Text = L"Output on";
			this->checkBox1->UseVisualStyleBackColor = true;
			this->checkBox1->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox1_CheckedChanged);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(92, 17);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 0;
			this->textBox1->Text = L"60";
			// 
			// tabPage2
			// 
			this->tabPage2->Controls->Add(this->groupBox4);
			this->tabPage2->Controls->Add(this->groupBox3);
			this->tabPage2->Location = System::Drawing::Point(4, 22);
			this->tabPage2->Name = L"tabPage2";
			this->tabPage2->Padding = System::Windows::Forms::Padding(3);
			this->tabPage2->Size = System::Drawing::Size(504, 459);
			this->tabPage2->TabIndex = 1;
			this->tabPage2->Text = L"Dip";
			this->tabPage2->UseVisualStyleBackColor = true;
			// 
			// groupBox4
			// 
			this->groupBox4->Controls->Add(this->textBox11);
			this->groupBox4->Controls->Add(this->textBox10);
			this->groupBox4->Controls->Add(this->label12);
			this->groupBox4->Controls->Add(this->label11);
			this->groupBox4->Location = System::Drawing::Point(6, 188);
			this->groupBox4->Name = L"groupBox4";
			this->groupBox4->Size = System::Drawing::Size(492, 78);
			this->groupBox4->TabIndex = 12;
			this->groupBox4->TabStop = false;
			this->groupBox4->Text = L"Read";
			// 
			// textBox11
			// 
			this->textBox11->Location = System::Drawing::Point(99, 45);
			this->textBox11->Name = L"textBox11";
			this->textBox11->ReadOnly = true;
			this->textBox11->Size = System::Drawing::Size(100, 20);
			this->textBox11->TabIndex = 3;
			this->textBox11->Text = L"0";
			// 
			// textBox10
			// 
			this->textBox10->Location = System::Drawing::Point(99, 19);
			this->textBox10->Name = L"textBox10";
			this->textBox10->ReadOnly = true;
			this->textBox10->Size = System::Drawing::Size(100, 20);
			this->textBox10->TabIndex = 2;
			this->textBox10->Text = L"0";
			// 
			// label12
			// 
			this->label12->AutoSize = true;
			this->label12->Location = System::Drawing::Point(6, 48);
			this->label12->Name = L"label12";
			this->label12->Size = System::Drawing::Size(67, 13);
			this->label12->TabIndex = 1;
			this->label12->Text = L"Time left [s] :";
			// 
			// label11
			// 
			this->label11->AutoSize = true;
			this->label11->Location = System::Drawing::Point(6, 22);
			this->label11->Name = L"label11";
			this->label11->Size = System::Drawing::Size(83, 13);
			this->label11->TabIndex = 0;
			this->label11->Text = L"Cycles done [-] :";
			// 
			// groupBox3
			// 
			this->groupBox3->Controls->Add(this->label6);
			this->groupBox3->Controls->Add(this->button3);
			this->groupBox3->Controls->Add(this->label10);
			this->groupBox3->Controls->Add(this->textBox6);
			this->groupBox3->Controls->Add(this->label9);
			this->groupBox3->Controls->Add(this->textBox7);
			this->groupBox3->Controls->Add(this->textBox9);
			this->groupBox3->Controls->Add(this->comboBox1);
			this->groupBox3->Controls->Add(this->textBox8);
			this->groupBox3->Controls->Add(this->label7);
			this->groupBox3->Controls->Add(this->label8);
			this->groupBox3->Location = System::Drawing::Point(6, 6);
			this->groupBox3->Name = L"groupBox3";
			this->groupBox3->Size = System::Drawing::Size(492, 176);
			this->groupBox3->TabIndex = 11;
			this->groupBox3->TabStop = false;
			this->groupBox3->Text = L"Set";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(6, 16);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(56, 13);
			this->label6->TabIndex = 4;
			this->label6->Text = L"Level [%] :";
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(6, 146);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(75, 23);
			this->button3->TabIndex = 3;
			this->button3->Text = L"Start";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->Location = System::Drawing::Point(6, 121);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(84, 13);
			this->label10->TabIndex = 10;
			this->label10->Text = L"Repeat time [s] :";
			// 
			// textBox6
			// 
			this->textBox6->Location = System::Drawing::Point(99, 13);
			this->textBox6->Name = L"textBox6";
			this->textBox6->Size = System::Drawing::Size(100, 20);
			this->textBox6->TabIndex = 0;
			this->textBox6->Text = L"40";
			// 
			// label9
			// 
			this->label9->AutoSize = true;
			this->label9->Location = System::Drawing::Point(6, 95);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(87, 13);
			this->label9->TabIndex = 9;
			this->label9->Text = L"Repat cycles [-] :";
			// 
			// textBox7
			// 
			this->textBox7->Location = System::Drawing::Point(99, 39);
			this->textBox7->Name = L"textBox7";
			this->textBox7->Size = System::Drawing::Size(100, 20);
			this->textBox7->TabIndex = 1;
			this->textBox7->Text = L"1";
			// 
			// textBox9
			// 
			this->textBox9->Location = System::Drawing::Point(99, 118);
			this->textBox9->Name = L"textBox9";
			this->textBox9->Size = System::Drawing::Size(100, 20);
			this->textBox9->TabIndex = 8;
			this->textBox9->Text = L"60";
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(8) {
				L"0", L"45", L"90", L"135", L"180", L"225", L"270",
					L"315"
			});
			this->comboBox1->Location = System::Drawing::Point(99, 65);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(100, 21);
			this->comboBox1->TabIndex = 2;
			this->comboBox1->Text = L"0";
			// 
			// textBox8
			// 
			this->textBox8->Location = System::Drawing::Point(99, 92);
			this->textBox8->Name = L"textBox8";
			this->textBox8->Size = System::Drawing::Size(100, 20);
			this->textBox8->TabIndex = 7;
			this->textBox8->Text = L"1";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Location = System::Drawing::Point(6, 42);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(86, 13);
			this->label7->TabIndex = 5;
			this->label7->Text = L"Periods [0-xxxx] :";
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Location = System::Drawing::Point(6, 68);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(79, 13);
			this->label8->TabIndex = 6;
			this->label8->Text = L"Phase start [°] :";
			// 
			// tabPage3
			// 
			this->tabPage3->Controls->Add(this->checkBox2);
			this->tabPage3->Location = System::Drawing::Point(4, 22);
			this->tabPage3->Name = L"tabPage3";
			this->tabPage3->Size = System::Drawing::Size(504, 459);
			this->tabPage3->TabIndex = 2;
			this->tabPage3->Text = L"Settings";
			this->tabPage3->UseVisualStyleBackColor = true;
			// 
			// checkBox2
			// 
			this->checkBox2->AutoSize = true;
			this->checkBox2->Checked = true;
			this->checkBox2->CheckState = System::Windows::Forms::CheckState::Checked;
			this->checkBox2->Location = System::Drawing::Point(13, 15);
			this->checkBox2->Name = L"checkBox2";
			this->checkBox2->Size = System::Drawing::Size(110, 17);
			this->checkBox2->TabIndex = 0;
			this->checkBox2->Text = L"Voltage feedback";
			this->checkBox2->UseVisualStyleBackColor = true;
			this->checkBox2->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox2_CheckedChanged);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {
				this->menuToolStripMenuItem,
					this->connectionToolStripMenuItem
			});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(527, 24);
			this->menuStrip1->TabIndex = 1;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// menuToolStripMenuItem
			// 
			this->menuToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) { this->exitToolStripMenuItem });
			this->menuToolStripMenuItem->Name = L"menuToolStripMenuItem";
			this->menuToolStripMenuItem->Size = System::Drawing::Size(50, 20);
			this->menuToolStripMenuItem->Text = L"Menu";
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(92, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::exitToolStripMenuItem_Click);
			// 
			// connectionToolStripMenuItem
			// 
			this->connectionToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {
				this->connectToolStripMenuItem,
					this->disconnectToolStripMenuItem
			});
			this->connectionToolStripMenuItem->Name = L"connectionToolStripMenuItem";
			this->connectionToolStripMenuItem->Size = System::Drawing::Size(81, 20);
			this->connectionToolStripMenuItem->Text = L"Connection";
			// 
			// connectToolStripMenuItem
			// 
			this->connectToolStripMenuItem->Name = L"connectToolStripMenuItem";
			this->connectToolStripMenuItem->Size = System::Drawing::Size(133, 22);
			this->connectToolStripMenuItem->Text = L"Connect";
			this->connectToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::connectToolStripMenuItem_Click);
			// 
			// disconnectToolStripMenuItem
			// 
			this->disconnectToolStripMenuItem->Name = L"disconnectToolStripMenuItem";
			this->disconnectToolStripMenuItem->Size = System::Drawing::Size(133, 22);
			this->disconnectToolStripMenuItem->Text = L"Disconnect";
			this->disconnectToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::disconnectToolStripMenuItem_Click);
			// 
			// statusStrip1
			// 
			this->statusStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) { this->toolStripStatusLabel1 });
			this->statusStrip1->Location = System::Drawing::Point(0, 519);
			this->statusStrip1->Name = L"statusStrip1";
			this->statusStrip1->Size = System::Drawing::Size(527, 22);
			this->statusStrip1->TabIndex = 3;
			this->statusStrip1->Text = L"statusStrip1";
			// 
			// toolStripStatusLabel1
			// 
			this->toolStripStatusLabel1->Name = L"toolStripStatusLabel1";
			this->toolStripStatusLabel1->Size = System::Drawing::Size(118, 17);
			this->toolStripStatusLabel1->Text = L"toolStripStatusLabel1";
			// 
			// serialPort1
			// 
			this->serialPort1->BaudRate = 19200;
			this->serialPort1->ReadTimeout = 200;
			this->serialPort1->WriteTimeout = 200;
			// 
			// backgroundWorker1
			// 
			this->backgroundWorker1->WorkerReportsProgress = true;
			this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
			this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &Form1::backgroundWorker1_RunWorkerCompleted);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(527, 541);
			this->Controls->Add(this->statusStrip1);
			this->Controls->Add(this->tabControl1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->tabControl1->ResumeLayout(false);
			this->tabPage1->ResumeLayout(false);
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->tabPage2->ResumeLayout(false);
			this->groupBox4->ResumeLayout(false);
			this->groupBox4->PerformLayout();
			this->groupBox3->ResumeLayout(false);
			this->groupBox3->PerformLayout();
			this->tabPage3->ResumeLayout(false);
			this->tabPage3->PerformLayout();
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->statusStrip1->ResumeLayout(false);
			this->statusStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
//GLOBAL VARIABLES
unsigned char first_connect = 1;



//------------------------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------------------------
//################################################################################################
// CRC STUFF
//################################################################################################

unsigned char calculate_crc(const char *string)
{
    unsigned char crc=0;
    unsigned char i=0;
   
    for(i;i<strlen(string);i++)
    {
        crc ^= string[i];
    }
    
    return crc;
}

unsigned char check_crc(const char *string)
{
	char new_str[30];
	unsigned char i = 0;
	char first = 0;
	char second = 0;
	unsigned char crc_1 = 0;
	unsigned char crc_2 = 0;

	if ((strlen(string) <= 3) || (strlen(string) >30)) return 1;

    //crc1
    for(i;i<(strlen(string)-3);i++)
    {
        new_str[i] = string[i];
    }
    new_str[i] = NULL;
    crc_1 = calculate_crc(new_str);  

    //crc2
    first=string[strlen(string)-3];
    second=string[strlen(string)-2];
    if((first >= 48)&&(first <= 57))crc_2 += (first-48)*16;// is *hexString Between 0-9
    else if((first >= 65)&&(first <= 70))crc_2 += (first-55)*16;// is *hexString Between A-F
    else if((first >= 97)&&(first <= 102))crc_2 += (first-87)*16;// is *hexString Between a-f
    else crc_2 = 0;
    if((second >= 48)&&(second <= 57))crc_2 += (second-48);// is *hexString Between 0-9
    else if((second >= 65)&&(second <= 70))crc_2 += (second-55);// is *hexString Between A-F
    else if((second >= 97)&&(second <= 102))crc_2 += (second-87);// is *hexString Between a-f
    else crc_2 = 0;

    if(crc_1==crc_2)return 0;
    
    return 1;
}

char my_strcmp(const char *string1,const char *string2,unsigned char begin,unsigned char end)
{
    unsigned char i=begin;
    char equal=0;
    for(i;i<=(end-begin);i++)
    {
        if(string1[i]!=string2[i])equal=1;        
    }
    if(begin>end)equal=-1;
    
    return equal;
}
unsigned int limit_val(unsigned int original, unsigned int min, unsigned int max)
{
	if (original > max)original = max;
	if (original < min)original = min;

	return original;
}


//################################################################################################
// SERIAL PORT
//################################################################################################


//*************************************************************************************************
//ping device
//*************************************************************************************************
//*************************************************************************************************
private: bool ping(void)
{
	unsigned char i=0;
	String^ buff="PING";
	send_serial(buff);
	//-----------------------------get
	String^ GET = get_serial();
	marshal_context context;
	const char* GET_array = context.marshal_as<const char*>(GET);

	if(( check_crc(GET_array) == 0 ) && (my_strcmp(GET_array,"WRONG_CRC",0,8) == 1))
	{
		if(my_strcmp(GET_array,"PONG",0,3)==0) return true;
		else return false;
	}
	else
	{
		if(i==3)return false;
		i++;
	}
	return false;
}

//*************************************************************************************************
//try find device and connect
//*************************************************************************************************
private: void ping_and_connect(void)
{
	array<String^>^ serialPorts = nullptr;
	serialPorts = SerialPort::GetPortNames();

	for each(String^ port in serialPorts)
	{
		open_serial(port);
		if( ping() )
		{
			this->toolStripStatusLabel1->Text= port+" opened";
			first_connect = 1;
			return;
		}		
	}
	this->toolStripStatusLabel1->Text= "Port NOT opened";
	return; 
}


//*************************************************************************************************
//open serial port
//*************************************************************************************************
private: void open_serial(String^ port)
{
	String^ response=String::Empty;
	close_serial();

	while(1)
	{
		try
		{
			if(!this->serialPort1->IsOpen){
				this->serialPort1->PortName=port;
				//this->serialPort1->BaudRate=19200;
				//open serial port 
				this->serialPort1->Open();
				//this->toolStripProgressBar1->Value=100;
				//this->toolStripStatusLabel1->Text= port+" opened";
			}
			else
				//show_message("Openning port failed");
				response="Openning port failed";
				break;
		}
		catch(UnauthorizedAccessException^)
		{
			//show_message("Unauthorized access");
			response="Unauthorized access";
			break;
		}
		catch(System::IO::IOException^) 
		{
			//show_message("Port not exist!");
			response="Port not exist!";
			break;
		}
		//if((response != String::Empty) && (port != String::Empty)) MessageBox::Show(response);
		MessageBox::Show(response);
		break;
	}
			 
	/*			for(int i; i<4; i++)
	{
		if( !ping() )
		{	
			this->toolStripProgressBar1->Value=0;
			this->toolStripStatusLabel1->Text="Device not found";
		}
	}
	*/			 return;
}
//*************************************************************************************************
//close serial port
//*************************************************************************************************
private: void close_serial(void)
{
	//close serialPort
	this->serialPort1->Close();
	// update progress bar
	//this->toolStripProgressBar1->Value=0;
	this->toolStripStatusLabel1->Text="Port closed";
	return;
}

//*************************************************************************************************
//send data to serial port
//*************************************************************************************************

private: void send_serial(String ^data)
{
	char crc[4];
	//char out[3];

	marshal_context context;
	const char* data_array = context.marshal_as<const char*>(data);

	sprintf_s(crc, "%02x\r", calculate_crc(data_array) & 0xff);
	String ^ crc_str = gcnew String(crc);

	data += crc_str;// +"\r";

	if(this->serialPort1->IsOpen)
	{
		this->serialPort1->DiscardInBuffer();
		this->serialPort1->DiscardOutBuffer();
		this->serialPort1->Write(data);
		/*		
		for each (char c in data)
		{
			out[0] = c;
			out[1] = NULL;
			String ^ character = gcnew String(out);
			this->serialPort1->Write(character);
			Sleep(50);
		}*/
	}
	else
		this->toolStripStatusLabel1->Text="Port is NOT opened!";

	return;
}
private: String^ send_command(String ^data)
{
	unsigned char i=0;
	unsigned char tryes = 20;
	String^ GET;
	while(i!= tryes)		//pocet pokusu o komunikaci
	{
		send_serial(data);
		GET = get_serial();
		marshal_context context;
		const char* GET_array = context.marshal_as<const char*>(GET);

		if(( check_crc(GET_array) == 0 ) && (my_strcmp(GET_array,"WRONG_CRC",0,8) == 1))
		{
			this->toolStripStatusLabel1->Text = "";
			break;
		}
		i++;
		this->toolStripStatusLabel1->Text = "Wrong data";
		GET="0";
		if (i == tryes)
		{
			close_serial();
			this->toolStripStatusLabel1->Text = "Disconnected";
		}
		Sleep(200);
	}
	if(GET!="0")GET=GET->Substring(0, GET->Length-3);
	return GET;
}

//*************************************************************************************************
//get data from serial port
//*************************************************************************************************
private: String^ get_serial(void)
{
	// check if port is ready for reading
	String^ data=String::Empty;
	String ^ recieved;
	char ch[2];

	if(this->serialPort1->IsOpen)
	{
		try
		{	
			do
			{
				sprintf_s(ch, "%c", this->serialPort1->ReadChar());
				recieved = gcnew String(ch);
				data+=recieved;
			}
			while( recieved!=("\r"));
		}
		catch(TimeoutException^)
		{
			//this->toolStripStatusLabel1->Text="Timeout Exception";
			return "TE";
		}
	}
	else
	{
		return "PNO";
	}
	return data;
}

//################################################################################################
// DATA SET/READ
//################################################################################################
void send_output(void)
{
	unsigned int freq = Math::Round(Convert::ToDouble((this->textBox1->Text)->Replace(",", ".")));
	unsigned int volt = Math::Round(Convert::ToDouble((this->textBox2->Text)->Replace(",", ".")));

	freq = limit_val(freq, 50, 60);
	volt = limit_val(volt, 1, 230);

	send_command("FREQ "+ Convert::ToString(freq));
	this->textBox1->Text = Convert::ToString(freq);
	send_command("VOLT "+ Convert::ToString(volt));
	this->textBox2->Text = Convert::ToString(volt);

	return;
}

void send_dip(void)
{
	//additional variables
	unsigned int freq = Convert::ToDouble(send_command("FREQ?"));

	//send data
	unsigned int dipl = Math::Round(Convert::ToDouble((this->textBox6->Text)->Replace(",", ".")));
	unsigned int dipc = Math::Round(Convert::ToDouble((this->textBox7->Text)->Replace(",", ".")) * 2);
	unsigned int repn = Math::Round(Convert::ToDouble((this->textBox8->Text)->Replace(",", ".")));
	unsigned int rept = Math::Round(Convert::ToDouble((this->textBox9->Text)->Replace(",", ".")));
	unsigned int dipp = Math::Round(Convert::ToDouble((this->comboBox1->Text)->Replace(",", ".")));

	//limiting values
	dipl = limit_val(dipl, 0, 100); //0-100%
	dipc = limit_val(dipc, 1, 600); //0.5-30 period
	repn = limit_val(repn, 1, 10); // 1-10 opakovani
	rept = limit_val(rept, ((1/freq)*dipc),3000);
	dipp = limit_val(dipp, 0, 315); // 0°-315° faze

	//setting values
	send_command("DIPL "+ Convert::ToString(dipl));
	this->textBox6->Text = Convert::ToString(dipl);
	send_command("DIPC "+ Convert::ToString(dipc));
	this->textBox7->Text = Convert::ToString(dipc/2);
	send_command("REPN "+ Convert::ToString(repn));
	this->textBox8->Text = Convert::ToString(repn);
	send_command("REPT "+ Convert::ToString(rept));
	this->textBox9->Text = Convert::ToString(rept);
	send_command("DIPP "+ Convert::ToString(dipp));
	this->comboBox1->Text = Convert::ToString(dipp);

	//send start/stop
	if (Convert::ToUInt16(send_command("DIPE?")))
		send_command("DIPE 0");
	else
		send_command("DIPE 1");

	return;
}


void send_pid(void)
{
	String ^ pid_en = ((this->checkBox2->Checked == TRUE) ? "1" : "0");

	send_command("PIDE " + pid_en);

	return;
}

void send_output_en(void)
{
	String ^ ouptut_en = ((this->checkBox1->Checked == TRUE)?"1":"0");

	send_command("OUTP "+ouptut_en);

	return;
}

void send_acdc(void)
{
	String ^ acdc = ((this->radioButton1->Checked == TRUE) ?"1":"0");

	send_command("ACDC "+acdc);

	return;
}

void read_data(void)
{
	//OUTPUT MENU
	String ^ acdc = this->radioButton1->Checked?"1":"0";
	double voltage= Convert::ToDouble(send_command("VOLT?")->Replace(".", ","));
	voltage *= acdc=="1" ? (1 / sqrt(2.0)) : 1;
	double current = Convert::ToDouble(send_command("CURR?")->Replace(".", ","));
	double power= current*voltage;

	this->textBox3->Text=Convert::ToString(Math::Round(voltage,3));
	this->textBox4->Text=Convert::ToString(Math::Round(current, 3));
	this->textBox5->Text=Convert::ToString(Math::Round(power, 3));

	this->radioButton1->Checked = Convert::ToUInt16(send_command("ACDC?"));
	if (!this->radioButton1->Checked)this->radioButton2->Checked = 1;
	this->checkBox1->Checked = Convert::ToUInt16(send_command("OUTP?"));

	if (first_connect == 1)
	{
		this->textBox2->Text = Convert::ToString(Math::Round(voltage));
		this->textBox1->Text = send_command("FREQ?");
	}
	//DIP MENU
	String^ cycles = send_command("REPN?")->Replace(".", ",");
	this->textBox10->Text = send_command("REPA?")->Replace(".", ",")+"/"+ cycles;
	this->textBox11->Text = send_command("REPD?")->Replace(".", ",")+"/"+ Convert::ToString(Math::Round(Convert::ToDouble(send_command("REPT?")->Replace(".", ",")) * Convert::ToDouble(cycles),2));
	this->button3->Text = Convert::ToUInt16(send_command("DIPE?"))?"STOP":"Start";

	if (first_connect == 1)
	{
		this->textBox6->Text = Convert::ToString(Math::Round(100 * (Convert::ToDouble(send_command("DIPL?")->Replace(".", ","))),2));
		this->textBox7->Text = Convert::ToString(Math::Round((Convert::ToDouble(send_command("DIPC?")->Replace(".", ","))) / 2,2));
		this->textBox8->Text = cycles;
		this->textBox9->Text = send_command("REPT?")->Replace(".", ",");
		this->comboBox1->Text = send_command("DIPP?")->Replace(".", ",");
	}
	//SETTINGS MENU
	this->checkBox2->Checked = Convert::ToUInt16(send_command("PIDE?"));

	first_connect = 0;
	return;
}

//################################################################################################
// BUTTONS
//################################################################################################

	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
		backgroundWorker1->RunWorkerAsync();
			 }

private: System::Void disconnectToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			send_command("END");
			close_serial();
		 }
private: System::Void connectToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 ping_and_connect();
		 }
private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 send_command("END");
			 Application::Exit();
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 send_output();
		 }
private: System::Void checkBox1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 send_output_en();
		 }
private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 send_acdc();
		 }
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
			Sleep(1000);
		 }
private: System::Void backgroundWorker1_RunWorkerCompleted( Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
	{
		if(this->serialPort1->IsOpen) read_data();
		backgroundWorker1->RunWorkerAsync();
	}
private: System::Void checkBox2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			send_pid();
}
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			send_dip();
}
};
}

