You are on page 1of 6

#pragma once

namespace WindowsFormsApplication3 {

using namespace System;


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

/// <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;
}
serialPort1->Close();
timer1->Stop();
}
private: System::IO::Ports::SerialPort^ serialPort1;
protected:
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button1;

private: System::Windows::Forms::Timer^ timer1;


private: System::ComponentModel::IContainer^ components;
private: System::Windows::Forms::DataVisualization::Charting::Chart^ chart1;

private:
/// <summary>
/// Required designer variable.
String^ mStr;
String^ speed;
int i;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
/// </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());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this-
>components));
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->timer1 = (gcnew System::Windows::Forms::Timer(this-
>components));
this->chart1 = (gcnew
System::Windows::Forms::DataVisualization::Charting::Chart());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->label6 = (gcnew System::Windows::Forms::Label());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->chart1))->BeginInit();
this->SuspendLayout();
//
// serialPort1
//
this->serialPort1->PortName = L"COM6";
this->serialPort1->DataReceived += gcnew
System::IO::Ports::SerialDataReceivedEventHandler(this,
&Form1::serialPort1_DataReceived);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(214, 100);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(19, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"00";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(111, 28);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 1;
this->textBox1->Text = L"0";
//
// button1
//
this->button1->Location = System::Drawing::Point(111, 54);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"send";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this,
&Form1::timer1_Tick);
//
// chart1
//
chartArea1->Name = L"ChartArea1";
this->chart1->ChartAreas->Add(chartArea1);
legend1->Name = L"Legend1";
this->chart1->Legends->Add(legend1);
this->chart1->Location = System::Drawing::Point(12, 100);
this->chart1->Name = L"chart1";
series1->ChartArea = L"ChartArea1";
series1->ChartType =
System::Windows::Forms::DataVisualization::Charting::SeriesChartType::Spline;
series1->Legend = L"Legend1";
series1->Name = L"Series1";
this->chart1->Series->Add(series1);
this->chart1->Size = System::Drawing::Size(568, 159);
this->chart1->TabIndex = 3;
this->chart1->Text = L"chart1";
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(217, 28);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(58, 20);
this->textBox2->TabIndex = 4;
this->textBox2->Text = L"0.07";
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(281, 28);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(58, 20);
this->textBox3->TabIndex = 5;
this->textBox3->Text = L"0.05";
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(345, 28);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(58, 20);
this->textBox4->TabIndex = 6;
this->textBox4->Text = L"0.03";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(108, 12);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(57, 13);
this->label2->TabIndex = 7;
this->label2->Text = L"Set Speed";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(214, 12);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(20, 13);
this->label3->TabIndex = 8;
this->label3->Text = L"kP";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(278, 12);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(16, 13);
this->label4->TabIndex = 9;
this->label4->Text = L"kI";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(342, 12);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(21, 13);
this->label5->TabIndex = 10;
this->label5->Text = L"kD";
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(108, 100);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(100, 13);
this->label6->TabIndex = 11;
this->label6->Text = L"current Speed (rpm)";
//
// button2
//
this->button2->Location = System::Drawing::Point(30, 28);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 12;
this->button2->Text = L"Start";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this,
&Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(30, 54);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 13;
this->button3->Text = L"Stop";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this,
&Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(592, 287);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->label6);
this->Controls->Add(this->label1);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->chart1);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->chart1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^
e) {
serialPort1->Open();
timer1->Start();
mStr = "0";
i=300;
}
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->WriteLine("vs_set_speed"+textBox1->Text);
//send set_speed to Arduino
serialPort1->WriteLine("vs_kp"+textBox2->Text);
//send kP to Arduino
serialPort1->WriteLine("vs_ki"+textBox3->Text);
//send kI to Arduino
serialPort1->WriteLine("vs_kd"+textBox4->Text);
//send kD to Arduino
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^
e) {
String^ length;
length=mStr->Length.ToString();
if(mStr->Substring(0,5)=="speed"){
speed=mStr-
>Substring(5,System::Convert::ToInt32(length)-6);
label1->Text=speed;
//print motor speed into Chart
this->chart1->Series["Series1"]->Points-
>AddXY(i,System::Convert::ToDouble(speed));
i++;
this->chart1->ChartAreas["ChartArea1"]->AxisX-
>Minimum=i-300; //shift x-axis
}
}
private: System::Void serialPort1_DataReceived(System::Object^ sender,
System::IO::Ports::SerialDataReceivedEventArgs^ e) {
mStr=serialPort1->ReadLine();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
serialPort1->WriteLine("vs_start"); //start motor
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
serialPort1->WriteLine("vs_stop"); //stop motor
}
};
}

You might also like