Ver Mensaje Individual
  #10 (permalink)  
Antiguo 26/02/2015, 08:20
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 11 meses
Puntos: 8
Respuesta: Intentar traducir este código a C++.

Ya lo conseguí.

Les dejo el código completo por si alguien le pueda ayudar o interesar.

Código C++:
Ver original
  1. #pragma once
  2.  
  3. namespace InterDuinoCPP {
  4.  
  5.     using namespace System;
  6.     using namespace System::ComponentModel;
  7.     using namespace System::Collections;
  8.     using namespace System::Windows::Forms;
  9.     using namespace System::Data;
  10.     using namespace System::Drawing;
  11.  
  12.     using namespace System::IO::Ports; // No olvidar.
  13.     using namespace System::Text;
  14.  
  15.     /// <summary>
  16.     /// Resumen de Form_Principal
  17.     /// </summary>
  18.     public ref class Form_Principal : public System::Windows::Forms::Form
  19.     {
  20.     public:
  21.         Form_Principal(void)
  22.         {
  23.             InitializeComponent();
  24.             //
  25.             //TODO: agregar código de constructor aquí
  26.             //
  27.  
  28.             // Abrir puerto miestras se ejecuta la aplicación.
  29.             if (!serialPort1->IsOpen)
  30.             {
  31.                 try
  32.                 {
  33.                     serialPort1->Open();
  34.                 }
  35.                 catch (Exception^ex)
  36.                 {
  37.                     MessageBox::Show(ex->ToString());
  38.                 }
  39.             }
  40.         }
  41.  
  42.     protected:
  43.         /// <summary>
  44.         /// Limpiar los recursos que se estén utilizando.
  45.         /// </summary>
  46.         ~Form_Principal()
  47.         {
  48.             if (components)
  49.             {
  50.                 delete components;
  51.             }
  52.         }
  53.     private: System::Windows::Forms::Button^  button_Led_8_ON;
  54.     private: System::Windows::Forms::Button^  button_Led_8_OFF;
  55.     protected:
  56.  
  57.     protected:
  58.  
  59.     private: System::Windows::Forms::Label^  label1;
  60.     private: System::Windows::Forms::Button^  button_Led_13_ON;
  61.     private: System::Windows::Forms::Button^  button_Led_13_OFF;
  62.  
  63.  
  64.     private: System::Windows::Forms::Label^  label2;
  65.     private: System::Windows::Forms::RichTextBox^  richTextBox_Mensajes;
  66.  
  67.     private: System::Windows::Forms::Label^  label3;
  68.     private: System::IO::Ports::SerialPort^  serialPort1;
  69.     private: System::ComponentModel::IContainer^  components;
  70.  
  71.     private:
  72.         /// <summary>
  73.         /// Variable del diseñador requerida.
  74.         /// </summary>
  75.  
  76.  
  77. #pragma region Windows Form Designer generated code
  78.         /// <summary>
  79.         /// Método necesario para admitir el Diseñador. No se puede modificar
  80.         /// el contenido del método con el editor de código.
  81.         /// </summary>
  82.         void InitializeComponent(void)
  83.         {
  84.             this->components = (gcnew System::ComponentModel::Container());
  85.             this->button_Led_8_ON = (gcnew System::Windows::Forms::Button());
  86.             this->button_Led_8_OFF = (gcnew System::Windows::Forms::Button());
  87.             this->label1 = (gcnew System::Windows::Forms::Label());
  88.             this->button_Led_13_ON = (gcnew System::Windows::Forms::Button());
  89.             this->button_Led_13_OFF = (gcnew System::Windows::Forms::Button());
  90.             this->label2 = (gcnew System::Windows::Forms::Label());
  91.             this->richTextBox_Mensajes = (gcnew System::Windows::Forms::RichTextBox());
  92.             this->label3 = (gcnew System::Windows::Forms::Label());
  93.             this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
  94.             this->SuspendLayout();
  95.             //
  96.             // button_Led_8_ON
  97.             //
  98.             this->button_Led_8_ON->Location = System::Drawing::Point(43, 37);
  99.             this->button_Led_8_ON->Name = L"button_Led_8_ON";
  100.             this->button_Led_8_ON->Size = System::Drawing::Size(75, 23);
  101.             this->button_Led_8_ON->TabIndex = 0;
  102.             this->button_Led_8_ON->Text = L"ON";
  103.             this->button_Led_8_ON->UseVisualStyleBackColor = true;
  104.             this->button_Led_8_ON->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_8_ON_Click);
  105.             //
  106.             // button_Led_8_OFF
  107.             //
  108.             this->button_Led_8_OFF->Location = System::Drawing::Point(43, 77);
  109.             this->button_Led_8_OFF->Name = L"button_Led_8_OFF";
  110.             this->button_Led_8_OFF->Size = System::Drawing::Size(75, 23);
  111.             this->button_Led_8_OFF->TabIndex = 1;
  112.             this->button_Led_8_OFF->Text = L"OFF";
  113.             this->button_Led_8_OFF->UseVisualStyleBackColor = true;
  114.             this->button_Led_8_OFF->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_8_OFF_Click);
  115.             //
  116.             // label1
  117.             //
  118.             this->label1->AutoSize = true;
  119.             this->label1->Location = System::Drawing::Point(62, 21);
  120.             this->label1->Name = L"label1";
  121.             this->label1->Size = System::Drawing::Size(34, 13);
  122.             this->label1->TabIndex = 2;
  123.             this->label1->Text = L"Led 8";
  124.             //
  125.             // button_Led_13_ON
  126.             //
  127.             this->button_Led_13_ON->Location = System::Drawing::Point(166, 37);
  128.             this->button_Led_13_ON->Name = L"button_Led_13_ON";
  129.             this->button_Led_13_ON->Size = System::Drawing::Size(75, 23);
  130.             this->button_Led_13_ON->TabIndex = 3;
  131.             this->button_Led_13_ON->Text = L"ON";
  132.             this->button_Led_13_ON->UseVisualStyleBackColor = true;
  133.             this->button_Led_13_ON->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_13_ON_Click);
  134.             //
  135.             // button_Led_13_OFF
  136.             //
  137.             this->button_Led_13_OFF->Location = System::Drawing::Point(166, 77);
  138.             this->button_Led_13_OFF->Name = L"button_Led_13_OFF";
  139.             this->button_Led_13_OFF->Size = System::Drawing::Size(75, 23);
  140.             this->button_Led_13_OFF->TabIndex = 4;
  141.             this->button_Led_13_OFF->Text = L"OFF";
  142.             this->button_Led_13_OFF->UseVisualStyleBackColor = true;
  143.             this->button_Led_13_OFF->Click += gcnew System::EventHandler(this, &Form_Principal::button_Led_13_OFF_Click);
  144.             //
  145.             // label2
  146.             //
  147.             this->label2->AutoSize = true;
  148.             this->label2->Location = System::Drawing::Point(186, 21);
  149.             this->label2->Name = L"label2";
  150.             this->label2->Size = System::Drawing::Size(40, 13);
  151.             this->label2->TabIndex = 5;
  152.             this->label2->Text = L"Led 13";
  153.             //
  154.             // richTextBox_Mensajes
  155.             //
  156.             this->richTextBox_Mensajes->Dock = System::Windows::Forms::DockStyle::Bottom;
  157.             this->richTextBox_Mensajes->Location = System::Drawing::Point(0, 129);
  158.             this->richTextBox_Mensajes->Name = L"richTextBox_Mensajes";
  159.             this->richTextBox_Mensajes->Size = System::Drawing::Size(284, 133);
  160.             this->richTextBox_Mensajes->TabIndex = 6;
  161.             this->richTextBox_Mensajes->Text = L"";
  162.             //
  163.             // label3
  164.             //
  165.             this->label3->AutoSize = true;
  166.             this->label3->Location = System::Drawing::Point(12, 113);
  167.             this->label3->Name = L"label3";
  168.             this->label3->Size = System::Drawing::Size(121, 13);
  169.             this->label3->TabIndex = 7;
  170.             this->label3->Text = L"Mensaje desde Arduino:";
  171.             //
  172.             // serialPort1
  173.             //
  174.             this->serialPort1->BaudRate = 115200;
  175.             this->serialPort1->PortName = L"COM4";
  176.             this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form_Principal::serialPort1_DataReceived);
  177.             //
  178.             // Form_Principal
  179.             //
  180.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  181.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  182.             this->ClientSize = System::Drawing::Size(284, 262);
  183.             this->Controls->Add(this->label3);
  184.             this->Controls->Add(this->richTextBox_Mensajes);
  185.             this->Controls->Add(this->label2);
  186.             this->Controls->Add(this->button_Led_13_OFF);
  187.             this->Controls->Add(this->button_Led_13_ON);
  188.             this->Controls->Add(this->label1);
  189.             this->Controls->Add(this->button_Led_8_OFF);
  190.             this->Controls->Add(this->button_Led_8_ON);
  191.             this->Name = L"Form_Principal";
  192.             this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
  193.             this->Text = L"Mini Interfaz C++";
  194.             this->ResumeLayout(false);
  195.             this->PerformLayout();
  196.  
  197.         }
  198. #pragma endregion
  199.     private: System::Void button_Led_8_ON_Click(System::Object^  sender, System::EventArgs^  e) {
  200.         array<Byte> ^miBuffer = Encoding::ASCII->GetBytes("Led_8_ON");
  201.         serialPort1->Write(miBuffer, 0, miBuffer->Length);
  202.     }
  203. private: System::Void button_Led_8_OFF_Click(System::Object^  sender, System::EventArgs^  e) {
  204.     array<Byte> ^miBuffer = Encoding::ASCII->GetBytes("Led_8_OFF");
  205.     serialPort1->Write(miBuffer, 0, miBuffer->Length);
  206. }
  207.  
  208. private: System::Void button_Led_13_ON_Click(System::Object^  sender, System::EventArgs^  e) {
  209.     cli::array<unsigned char> ^miBuffer = gcnew cli::array<unsigned char>(9);
  210.     miBuffer[0] = 0x4C; // ASCII letra "L".
  211.     miBuffer[1] = 0x65; // ASCII letra "e".
  212.     miBuffer[2] = 0x64; // ASCII letra "d".
  213.     miBuffer[3] = 0x5F; // ASCII letra "_".
  214.     miBuffer[4] = 0x31; // ASCII letra "1".
  215.     miBuffer[5] = 0x33; // ASCII letra "3".
  216.     miBuffer[6] = 0x5F; // ASCII letra "_".
  217.     miBuffer[7] = 0x4F; // ASCII letra "O".
  218.     miBuffer[8] = 0x4E; // ASCII letra "N".
  219.     serialPort1->Write(miBuffer, 0, miBuffer->Length);
  220. }
  221.  
  222. private: System::Void button_Led_13_OFF_Click(System::Object^  sender, System::EventArgs^  e) {
  223.     cli::array<unsigned char> ^miBuffer = gcnew cli::array<unsigned char>(10);
  224.     miBuffer[0] = 0x4C; // ASCII letra "L".
  225.     miBuffer[1] = 0x65; // ASCII letra "e".
  226.     miBuffer[2] = 0x64; // ASCII letra "d".
  227.     miBuffer[3] = 0x5F; // ASCII letra "_".
  228.     miBuffer[4] = 0x31; // ASCII letra "1".
  229.     miBuffer[5] = 0x33; // ASCII letra "3".
  230.     miBuffer[6] = 0x5F; // ASCII letra "_".
  231.     miBuffer[7] = 0x4F; // ASCII letra "O".
  232.     miBuffer[8] = 0x46; // ASCII letra "F".
  233.     miBuffer[9] = 0x46; // ASCII letra "F".
  234.     serialPort1->Write(miBuffer, 0, miBuffer->Length);
  235. }
  236.  
  237.          // Declaramos un delegado.
  238.          delegate void Delegado(String ^ Recibidos);
  239.  
  240. private: Void serialPort1_DataReceived(Object^  sender, SerialDataReceivedEventArgs^  e) {
  241.     // Utilizremos un string como buffer de recepción.
  242.     String ^ Recibidos;
  243.  
  244.     if (serialPort1->BytesToRead > 0){ // Si hay carácter que leer...
  245.  
  246.         Recibidos = serialPort1->ReadExisting(); // Acumula los carácteres recibido.
  247.  
  248.         // Invocamos y cargamos los bytes en rictTextBox_Mensajes.
  249.         Delegado ^ Actualizar = gcnew Delegado(this, &Form_Principal::ByteRecibidos);
  250.  
  251.         this->Invoke(Actualizar, Recibidos);
  252.     }
  253. }
  254.  
  255.          void ByteRecibidos(String ^ Data){
  256.              // Los carácteres almacenado en 'Data' se depositan en richTextBox_Mensaje.
  257.              richTextBox_Mensajes->Text += Data;
  258.  
  259.              // Selecciona la posición final para leer los mensajes entrantes.
  260.              richTextBox_Mensajes->SelectionStart = richTextBox_Mensajes->Text->Length;
  261.  
  262.              // Mantiene el scroll en la entrada de cada mensaje.
  263.              richTextBox_Mensajes->ScrollToCaret();
  264.          }
  265.  
  266. };
  267. }

Saludos y gracias por todo.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar