Code Windows Form
Code Windows Form
#pragma once
namespace dieukhienxe {
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::Text;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
array<String^>^ tocdotruyen;
MyForm(void)
{
InitializeComponent();
tocdotruyen = gcnew array<String^>{"9600"};
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::IO::Ports::SerialPort^ serialPort1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::PictureBox^ pictureBox2;
private: System::Windows::Forms::PictureBox^ pictureBox3;
private: System::Windows::Forms::PictureBox^ pictureBox4;
private: System::Windows::Forms::PictureBox^ pictureBox5;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::TrackBar^ trackBar1;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::ComboBox^ comboBox2;
private: System::Windows::Forms::Label^ label7;
private: System::Windows::Forms::Button^ button5;
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::ComponentModel::ComponentResourceManager^
resources = (gcnew
System::ComponentModel::ComponentResourceManager(MyForm::typeid));
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this-
>components));
this->pictureBox1 = (gcnew
System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew
System::Windows::Forms::PictureBox());
this->pictureBox3 = (gcnew
System::Windows::Forms::PictureBox());
this->pictureBox4 = (gcnew
System::Windows::Forms::PictureBox());
this->pictureBox5 = (gcnew
System::Windows::Forms::PictureBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->trackBar1 = (gcnew
System::Windows::Forms::TrackBar());
this->label6 = (gcnew System::Windows::Forms::Label());
this->comboBox1 = (gcnew
System::Windows::Forms::ComboBox());
this->comboBox2 = (gcnew
System::Windows::Forms::ComboBox());
this->label7 = (gcnew System::Windows::Forms::Label());
this->button5 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox2))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox3))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox4))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox5))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>trackBar1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Image =
(cli::safe_cast<System::Drawing::Image^>(resources-
>GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location = System::Drawing::Point(674,
310);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(100, 89);
this->pictureBox1->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew
System::EventHandler(this, &MyForm::pictureBox1_Click);
//
// pictureBox2
//
this->pictureBox2->Image =
(cli::safe_cast<System::Drawing::Image^>(resources-
>GetObject(L"pictureBox2.Image")));
this->pictureBox2->Location = System::Drawing::Point(816,
184);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(100, 89);
this->pictureBox2->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
this->pictureBox2->Click += gcnew
System::EventHandler(this, &MyForm::pictureBox2_Click);
//
// pictureBox3
//
this->pictureBox3->Image =
(cli::safe_cast<System::Drawing::Image^>(resources-
>GetObject(L"pictureBox3.Image")));
this->pictureBox3->Location = System::Drawing::Point(816,
435);
this->pictureBox3->Name = L"pictureBox3";
this->pictureBox3->Size = System::Drawing::Size(100, 89);
this->pictureBox3->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox3->TabIndex = 2;
this->pictureBox3->TabStop = false;
this->pictureBox3->Click += gcnew
System::EventHandler(this, &MyForm::pictureBox3_Click);
//
// pictureBox4
//
this->pictureBox4->Image =
(cli::safe_cast<System::Drawing::Image^>(resources-
>GetObject(L"pictureBox4.Image")));
this->pictureBox4->Location = System::Drawing::Point(816,
310);
this->pictureBox4->Name = L"pictureBox4";
this->pictureBox4->Size = System::Drawing::Size(100, 89);
this->pictureBox4->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox4->TabIndex = 3;
this->pictureBox4->TabStop = false;
this->pictureBox4->Click += gcnew
System::EventHandler(this, &MyForm::pictureBox4_Click);
//
// pictureBox5
//
this->pictureBox5->Image =
(cli::safe_cast<System::Drawing::Image^>(resources-
>GetObject(L"pictureBox5.Image")));
this->pictureBox5->Location = System::Drawing::Point(954,
310);
this->pictureBox5->Name = L"pictureBox5";
this->pictureBox5->Size = System::Drawing::Size(100, 89);
this->pictureBox5->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox5->TabIndex = 4;
this->pictureBox5->TabStop = false;
this->pictureBox5->Click += gcnew
System::EventHandler(this, &MyForm::pictureBox5_Click);
//
// button1
//
this->button1->BackColor = System::Drawing::Color::Lime;
this->button1->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Location = System::Drawing::Point(46, 28);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(127, 50);
this->button1->TabIndex = 5;
this->button1->Text = L"Connect";
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this,
&MyForm::button1_Click);
//
// button2
//
this->button2->BackColor = System::Drawing::Color::Red;
this->button2->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(46, 139);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(127, 50);
this->button2->TabIndex = 6;
this->button2->Text = L"Disconnect\r\n";
this->button2->UseVisualStyleBackColor = false;
this->button2->Click += gcnew System::EventHandler(this,
&MyForm::button2_Click);
//
// button3
//
this->button3->BackColor =
System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<Syste
m::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(0)));
this->button3->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->ForeColor = System::Drawing::Color::Black;
this->button3->Location = System::Drawing::Point(1112, 47);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(136, 82);
this->button3->TabIndex = 7;
this->button3->Text = L"Điều Khiển";
this->button3->UseVisualStyleBackColor = false;
this->button3->Click += gcnew System::EventHandler(this,
&MyForm::button3_Click);
//
// button4
//
this->button4->BackColor =
System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<Syste
m::Byte>(128)), static_cast<System::Int32>(static_cast<System::Byte>(128)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button4->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(1325, 47);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(136, 82);
this->button4->TabIndex = 8;
this->button4->Text = L"Auto\r\n";
this->button4->UseVisualStyleBackColor = false;
this->button4->Click += gcnew System::EventHandler(this,
&MyForm::button4_Click);
//
// label1
//
this->label1->AccessibleRole =
System::Windows::Forms::AccessibleRole::None;
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(185, 99);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(98, 20);
this->label1->TabIndex = 9;
this->label1->Text = L"Trạng Thái :";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(289, 103);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(0, 16);
this->label2->TabIndex = 10;
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label3->Location = System::Drawing::Point(42, 277);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(102, 20);
this->label3->TabIndex = 11;
this->label3->Text = L"Cổng COM :";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label4->Location = System::Drawing::Point(23, 352);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(121, 20);
this->label4->TabIndex = 12;
this->label4->Text = L"Tốc độ truyền :";
//
// trackBar1
//
this->trackBar1->BackColor =
System::Drawing::SystemColors::Control;
this->trackBar1->Location = System::Drawing::Point(27, 468);
this->trackBar1->Maximum = 255;
this->trackBar1->Minimum = 150;
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size = System::Drawing::Size(292, 56);
this->trackBar1->TabIndex = 14;
this->trackBar1->Value = 150;
this->trackBar1->Scroll += gcnew System::EventHandler(this,
&MyForm::trackBar1_Scroll);
//
// label6
//
this->label6->AutoSize = true;
this->label6->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label6->Location = System::Drawing::Point(337, 486);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(18, 20);
this->label6->TabIndex = 15;
this->label6->Text = L"0";
//
// comboBox1
//
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(168,
273);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 24);
this->comboBox1->TabIndex = 16;
//
// comboBox2
//
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Location = System::Drawing::Point(168,
348);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(121, 24);
this->comboBox2->TabIndex = 17;
//
// label7
//
this->label7->AutoSize = true;
this->label7->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label7->Location = System::Drawing::Point(289, 99);
this->label7->Name = L"label7";
this->label7->Size = System::Drawing::Size(0, 20);
this->label7->TabIndex = 18;
//
// button5
//
this->button5->BackColor =
System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<Syste
m::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(128)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button5->Font = (gcnew System::Drawing::Font(L"Times
New Roman", 10.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button5->Location = System::Drawing::Point(27, 399);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(117, 50);
this->button5->TabIndex = 19;
this->button5->Text = L"Speed";
this->button5->UseVisualStyleBackColor = false;
this->button5->Click += gcnew System::EventHandler(this,
&MyForm::button5_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->BackColor =
System::Drawing::SystemColors::ControlLightLight;
this->ClientSize = System::Drawing::Size(1501, 697);
this->Controls->Add(this->button5);
this->Controls->Add(this->label7);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->label6);
this->Controls->Add(this->trackBar1);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox5);
this->Controls->Add(this->pictureBox4);
this->Controls->Add(this->pictureBox3);
this->Controls->Add(this->pictureBox2);
this->Controls->Add(this->pictureBox1);
this->ForeColor = System::Drawing::Color::Black;
this->Name = L"MyForm";
this->Text = L"Điều Khiển Xe";
this->Load += gcnew System::EventHandler(this,
&MyForm::MyForm_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox2))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox3))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox4))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>pictureBox5))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>trackBar1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender,
System::EventArgs^ e) {
array<Object^>^ Comport = SerialPort::GetPortNames();//tạo 1
Object chứa cổng COM
this->comboBox1->DataSource = Comport;//dữ liệu trong comboBox
là cổng COM
comboBox2->DataSource = tocdotruyen;
}
private: System::Void trackBar1_Scroll(System::Object^ sender,
System::EventArgs^ e) {
label6->Text = trackBar1->Value.ToString();
}
private: System::Void button5_Click(System::Object ^ sender, System::EventArgs
^ e) {
label6->Text = trackBar1->Value.ToString();
int s = Convert::ToInt32(label6->Text);
if (s > 150 && s <= 170)
serialPort1->Write("0");
else if (s > 170 && s <= 200)
serialPort1->Write("1");
else if (s > 200 && s <= 220)
serialPort1->Write("2");
else if (s > 220 && s <= 240)
serialPort1->Write("3");
else if (s > 240 && s <= 255)
serialPort1->Write("4");
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^
e) {
this->serialPort1->Write("e");
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^
e) {
this->serialPort1->Write("f");
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^
e) {
label7->Text = "CONNECT";
label7->ForeColor = Color::Green;
this->serialPort1->BaudRate = System::Convert::ToInt32(this-
>comboBox2->Text->ToString());
this->serialPort1->BaudRate = 9600;
this->serialPort1->PortName = comboBox1->Text;
this->serialPort1-> Open();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^
e) {
this->label7->Text = "DISCONNECT";
this->label7->ForeColor = Color::Red;
this->serialPort1->Write("e");
this->serialPort1->Close();
}
private: System::Void pictureBox2_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->Write("a");
}
private: System::Void pictureBox3_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->Write("b");
}
private: System::Void pictureBox1_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->Write("c");
}
private: System::Void pictureBox5_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->Write("d");
}
private: System::Void pictureBox4_Click(System::Object^ sender,
System::EventArgs^ e) {
serialPort1->Write("e");
}
};
}