#pragma once
namespace InterDuinoCPP {
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; // No olvidar.
using namespace System::Text;
/// <summary>
/// Resumen de Form_Principal
/// </summary>
public ref class Form_Principal : public System::Windows::Forms::Form
{
public:
Form_Principal(void)
{
InitializeComponent();
//
//TODO: agregar código de constructor aquí
//
// Abrir puerto miestras se ejecuta la aplicación.
if (!serialPort1->IsOpen)
{
try
{
serialPort1->Open();
}
catch (Exception^ex)
{
MessageBox::Show(ex->ToString());
}
}
}
protected:
/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
~Form_Principal()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button_Led_8_ON;
private: System::Windows::Forms::Button^ button_Led_8_OFF;
protected:
protected:
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button_Led_13_ON;
private: System::Windows::Forms::Button^ button_Led_13_OFF;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::RichTextBox^ richTextBox_Mensajes;
private: System::Windows::Forms::Label^ label3;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Variable del diseñador requerida.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button_Led_8_ON = (gcnew System::Windows::Forms::Button());
this->button_Led_8_OFF = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->button_Led_13_ON = (gcnew System::Windows::Forms::Button());
this->button_Led_13_OFF = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->richTextBox_Mensajes = (gcnew System::Windows::Forms::RichTextBox());
this->label3 = (gcnew System::Windows::Forms::Label());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->SuspendLayout();
//
// button_Led_8_ON
//
this->button_Led_8_ON->Location = System::Drawing::Point(43, 37);
this->button_Led_8_ON->Name = L"button_Led_8_ON";
this->button_Led_8_ON->Size = System::Drawing::Size(75, 23);
this->button_Led_8_ON->TabIndex = 0;
this->button_Led_8_ON->Text = L"ON";
this->button_Led_8_ON->UseVisualStyleBackColor = true;
this->button_Led_8_ON->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_8_ON_Click);
//
// button_Led_8_OFF
//
this->button_Led_8_OFF->Location = System::Drawing::Point(43, 77);
this->button_Led_8_OFF->Name = L"button_Led_8_OFF";
this->button_Led_8_OFF->Size = System::Drawing::Size(75, 23);
this->button_Led_8_OFF->TabIndex = 1;
this->button_Led_8_OFF->Text = L"OFF";
this->button_Led_8_OFF->UseVisualStyleBackColor = true;
this->button_Led_8_OFF->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_8_OFF_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(62, 21);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(34, 13);
this->label1->TabIndex = 2;
this->label1->Text = L"Led 8";
//
// button_Led_13_ON
//
this->button_Led_13_ON->Location = System::Drawing::Point(166, 37);
this->button_Led_13_ON->Name = L"button_Led_13_ON";
this->button_Led_13_ON->Size = System::Drawing::Size(75, 23);
this->button_Led_13_ON->TabIndex = 3;
this->button_Led_13_ON->Text = L"ON";
this->button_Led_13_ON->UseVisualStyleBackColor = true;
this->button_Led_13_ON->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_13_ON_Click);
//
// button_Led_13_OFF
//
this->button_Led_13_OFF->Location = System::Drawing::Point(166, 77);
this->button_Led_13_OFF->Name = L"button_Led_13_OFF";
this->button_Led_13_OFF->Size = System::Drawing::Size(75, 23);
this->button_Led_13_OFF->TabIndex = 4;
this->button_Led_13_OFF->Text = L"OFF";
this->button_Led_13_OFF->UseVisualStyleBackColor = true;
this->button_Led_13_OFF->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_13_OFF_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(186, 21);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(40, 13);
this->label2->TabIndex = 5;
this->label2->Text = L"Led 13";
//
// richTextBox_Mensajes
//
this->richTextBox_Mensajes->Dock = System::Windows::Forms::DockStyle::Bottom;
this->richTextBox_Mensajes->Location = System::Drawing::Point(0, 129);
this->richTextBox_Mensajes->Name = L"richTextBox_Mensajes";
this->richTextBox_Mensajes->Size = System::Drawing::Size(284, 133);
this->richTextBox_Mensajes->TabIndex = 6;
this->richTextBox_Mensajes->Text = L"";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(12, 113);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(121, 13);
this->label3->TabIndex = 7;
this->label3->Text = L"Mensaje desde Arduino:";
//
// serialPort1
//
this->serialPort1->BaudRate = 115200;
this->serialPort1->PortName = L"COM4";
this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form_Principal::serialPort1_DataReceived);
//
// Form_Principal
//
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->label3);
this->Controls->Add(this->richTextBox_Mensajes);
this->Controls->Add(this->label2);
this->Controls->Add(this->button_Led_13_OFF);
this->Controls->Add(this->button_Led_13_ON);
this->Controls->Add(this->label1);
this->Controls->Add(this->button_Led_8_OFF);
this->Controls->Add(this->button_Led_8_ON);
this->Name = L"Form_Principal";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Mini Interfaz C++";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button_Led_8_ON_Click(System::Object^ sender, System::EventArgs^ e) {
array<Byte> ^miBuffer = Encoding::ASCII->GetBytes("Led_8_ON");
serialPort1->Write(miBuffer, 0, miBuffer->Length);
}
private: System::Void button_Led_8_OFF_Click(System::Object^ sender, System::EventArgs^ e) {
array<Byte> ^miBuffer = Encoding::ASCII->GetBytes("Led_8_OFF");
serialPort1->Write(miBuffer, 0, miBuffer->Length);
}
private: System::Void button_Led_13_ON_Click(System::Object^ sender, System::EventArgs^ e) {
cli::array<unsigned char> ^miBuffer = gcnew cli::array<unsigned char>(9);
miBuffer[0] = 0x4C; // ASCII letra "L".
miBuffer[1] = 0x65; // ASCII letra "e".
miBuffer[2] = 0x64; // ASCII letra "d".
miBuffer[3] = 0x5F; // ASCII letra "_".
miBuffer[4] = 0x31; // ASCII letra "1".
miBuffer[5] = 0x33; // ASCII letra "3".
miBuffer[6] = 0x5F; // ASCII letra "_".
miBuffer[7] = 0x4F; // ASCII letra "O".
miBuffer[8] = 0x4E; // ASCII letra "N".
serialPort1->Write(miBuffer, 0, miBuffer->Length);
}
private: System::Void button_Led_13_OFF_Click(System::Object^ sender, System::EventArgs^ e) {
cli::array<unsigned char> ^miBuffer = gcnew cli::array<unsigned char>(10);
miBuffer[0] = 0x4C; // ASCII letra "L".
miBuffer[1] = 0x65; // ASCII letra "e".
miBuffer[2] = 0x64; // ASCII letra "d".
miBuffer[3] = 0x5F; // ASCII letra "_".
miBuffer[4] = 0x31; // ASCII letra "1".
miBuffer[5] = 0x33; // ASCII letra "3".
miBuffer[6] = 0x5F; // ASCII letra "_".
miBuffer[7] = 0x4F; // ASCII letra "O".
miBuffer[8] = 0x46; // ASCII letra "F".
miBuffer[9] = 0x46; // ASCII letra "F".
serialPort1->Write(miBuffer, 0, miBuffer->Length);
}
// Declaramos un delegado.
delegate void Delegado(String ^ Recibidos);
private: Void serialPort1_DataReceived(Object^ sender, SerialDataReceivedEventArgs^ e) {
// Utilizremos un string como buffer de recepción.
String ^ Recibidos;
if (serialPort1->BytesToRead > 0){ // Si hay carácter que leer...
Recibidos = serialPort1->ReadExisting(); // Acumula los carácteres recibido.
// Invocamos y cargamos los bytes en rictTextBox_Mensajes.
Delegado ^ Actualizar = gcnew Delegado(this, &Form_Principal::ByteRecibidos);
this->Invoke(Actualizar, Recibidos);
}
}
void ByteRecibidos(String ^ Data){
// Los carácteres almacenado en 'Data' se depositan en richTextBox_Mensaje.
richTextBox_Mensajes->Text += Data;
// Selecciona la posición final para leer los mensajes entrantes.
richTextBox_Mensajes->SelectionStart = richTextBox_Mensajes->Text->Length;
// Mantiene el scroll en la entrada de cada mensaje.
richTextBox_Mensajes->ScrollToCaret();
}
};
}