接收数据序列时,c ++窗口关闭窗口

时间:2016-05-13 08:09:10

标签: c++ winforms visual-studio windows-forms-designer

我是新来的,也是Windows窗体的新手。 我正在尝试使窗口等待从arduino接收数据,当收到数据时窗口必须关闭。 到目前为止我能够做到这一点,但是当它打开时我无法在窗口中做任何事情。杜,你有任何建议如何做到这一点。

这里的代码:     

#pragma once

namespace CppWinForm1 {

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;


public ref class password : public System::Windows::Forms::Form
{
public:
    password(void)
    {
        InitializeComponent();
        this->serialPort2->Open();
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~password()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Label^  label1;
public: System::IO::Ports::SerialPort^  serialPort2;
private: System::Windows::Forms::Timer^  timer1;
private: System::Windows::Forms::Button^  button1;
private: System::ComponentModel::IContainer^  components;

private:


  #pragma region Windows Form Designer generated code

    void InitializeComponent(void)
    {
        this->components = (gcnew System::ComponentModel::Container());
        System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(password::typeid));
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->serialPort2 = (gcnew System::IO::Ports::SerialPort(this->components));
        this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Location = System::Drawing::Point(192, 70);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(330, 25);
        this->label1->TabIndex = 0;
        this->label1->Text = L"Indtast password på DE2 boardet";
        this->label1->Click += gcnew System::EventHandler(this, &password::label1_Click);
        // 
        // serialPort2
        // 
        this->serialPort2->PortName = L"COM5";
        // 
        // timer1
        // 
        this->timer1->Enabled = true;
        this->timer1->Interval = 1;
        this->timer1->Tick += gcnew System::EventHandler(this, &password::timer1_Tick);
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(197, 123);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(325, 57);
        this->button1->TabIndex = 1;
        this->button1->Text = L"Tilbage";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &password::button1_Click_1);
        // 
        // password
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(12, 25);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(738, 280);
        this->Controls->Add(this->button1);
        this->Controls->Add(this->label1);
        this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
        this->Name = L"password";
        this->Text = L"password";
        this->Load += gcnew System::EventHandler(this, &password::password_Load);
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
}


private: System::Void password_Load(System::Object^  sender, System::EventArgs^  e) {
    this->timer1->Start();
}

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
    this->serialPort2->WriteLine("3");
    char ch = this->serialPort2->ReadLine()[0];
    int x = ch - 48;
    if (x == 1)
    {
        this->serialPort2->Close();
        this->timer1->Stop();
        Close();
    }
}

private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
    this->serialPort2->Close();
    this->timer1->Stop();
    Close();
}
};
}

0 个答案:

没有答案