Чтение данных из Ардуинo при помощи C++ VS 2010 Windows Forms
Код:
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(int x = 1; x < 10000; x = x + 1)
{
Serial.println(x);
delay(1000);
}
}
{
Serial.begin(9600);
}
void loop()
{
for(int x = 1; x < 10000; x = x + 1)
{
Serial.println(x);
delay(1000);
}
}
Так же, есть код написанный на С++ в VS 2010 Express (Windows Forms), который читает данные с Ардуины.
Код:
#pragma once
namespace Arduino_Read {
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;
/// <summary>
/// Сводка для Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button2;
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::IO::Ports::SerialPort^ serialPort1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button2 = (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->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 31);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 50);
this->button1->TabIndex = 0;
this->button1->Text = L"Open Port";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button3
//
this->button3->Enabled = false;
this->button3->Location = System::Drawing::Point(197, 186);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 50);
this->button3->TabIndex = 2;
this->button3->Text = L"Read Port";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button2
//
this->button2->Enabled = false;
this->button2->Location = System::Drawing::Point(197, 108);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 50);
this->button2->TabIndex = 1;
this->button2->Text = L"Close Port";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 31);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(81, 13);
this->label1->TabIndex = 3;
this->label1->Text = L"Temperature IN";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(140, 31);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(37, 13);
this->label2->TabIndex = 4;
this->label2->Text = L"read...";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(12, 67);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(93, 13);
this->label3->TabIndex = 5;
this->label3->Text = L"Temperature OUT";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(140, 67);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(37, 13);
this->label4->TabIndex = 6;
this->label4->Text = L"read...";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if(!this->serialPort1->IsOpen)
{
this->serialPort1->PortName="COM3";
this->serialPort1->BaudRate=Int32::Parse("9600");
this->serialPort1->Open();
this->button1->Enabled = false;
this->button2->Enabled = true;
this->button3->Enabled = true;
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
this->serialPort1->Close();
this->button1->Enabled = true;
this->button2->Enabled = false;
this->button3->Enabled = false;
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
if(this->serialPort1->IsOpen){
try{
this->label2->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^){
this->label2->Text="Timeout Exception";
}
}
else
this->label2->Text="Port Not Opened";
}
};
}
namespace Arduino_Read {
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;
/// <summary>
/// Сводка для Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button2;
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::IO::Ports::SerialPort^ serialPort1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button2 = (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->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 31);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 50);
this->button1->TabIndex = 0;
this->button1->Text = L"Open Port";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button3
//
this->button3->Enabled = false;
this->button3->Location = System::Drawing::Point(197, 186);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 50);
this->button3->TabIndex = 2;
this->button3->Text = L"Read Port";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button2
//
this->button2->Enabled = false;
this->button2->Location = System::Drawing::Point(197, 108);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 50);
this->button2->TabIndex = 1;
this->button2->Text = L"Close Port";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 31);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(81, 13);
this->label1->TabIndex = 3;
this->label1->Text = L"Temperature IN";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(140, 31);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(37, 13);
this->label2->TabIndex = 4;
this->label2->Text = L"read...";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(12, 67);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(93, 13);
this->label3->TabIndex = 5;
this->label3->Text = L"Temperature OUT";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(140, 67);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(37, 13);
this->label4->TabIndex = 6;
this->label4->Text = L"read...";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if(!this->serialPort1->IsOpen)
{
this->serialPort1->PortName="COM3";
this->serialPort1->BaudRate=Int32::Parse("9600");
this->serialPort1->Open();
this->button1->Enabled = false;
this->button2->Enabled = true;
this->button3->Enabled = true;
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
this->serialPort1->Close();
this->button1->Enabled = true;
this->button2->Enabled = false;
this->button3->Enabled = false;
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
if(this->serialPort1->IsOpen){
try{
this->label2->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^){
this->label2->Text="Timeout Exception";
}
}
else
this->label2->Text="Port Not Opened";
}
};
}
Все работает. НО!!! Не так, как хочется! Данные с Ардуины отображаются (обновляются) только при нажатии кнопки Read Port. А хочется, чтобы обновлялись автоматически.
Вот посоветовали мне поставить таймер и в таймере читать serialPort1. Сделал так:
Код:
private: System::Void timer1_Tick_1(System::Object^ sender, System::EventArgs^ e)
{
if(this->serialPort1->IsOpen)
{
try
{
this->label2->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^)
{
this->label2->Text="Timeout Exception";
}
}
else
this->label2->Text="Port Not Opened";
}
{
if(this->serialPort1->IsOpen)
{
try
{
this->label2->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^)
{
this->label2->Text="Timeout Exception";
}
}
else
this->label2->Text="Port Not Opened";
}
Почитал о потоках. Может можно как-то и в отдельный поток засунуть обработку чтения порта, но не знаю как...
Почитай в сторону SerialPort::DataReceived Event. Не знаю, будет ли прочитанная строка в SerialDataReceivedEventHandler, но может и быть.