elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Pasar de C# a C++ CLR.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Pasar de C# a C++ CLR.  (Leído 1,883 veces)
Meta


Desconectado Desconectado

Mensajes: 3.438



Ver Perfil WWW
Pasar de C# a C++ CLR.
« en: 8 Febrero 2016, 13:14 pm »

Hola:

Tengo el código en C# de esta manera. Este código solo recibe datos por el puerto serie.
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.IO.Ports; // No olvidar.
  12. using System.Threading;
  13.  
  14. namespace Entrada_Arduino_AWF_1_CS
  15. {
  16.    public partial class Form1 : Form
  17.    {
  18.        // Utilizaremos un string como buffer de recepción.
  19.        string Recibidos;
  20.  
  21.        public Form1()
  22.        {
  23.            InitializeComponent();
  24.  
  25.            if (!serialPort1.IsOpen)
  26.            {
  27.                try
  28.                {
  29.                    serialPort1.Open();
  30.                }
  31.                catch (System.Exception ex)
  32.                {
  33.                    MessageBox.Show(ex.ToString());
  34.                }
  35.  
  36.                serialPort1.DataReceived += new SerialDataReceivedEventHandler(Recepcion);
  37.            }
  38.        }
  39.  
  40.        // Al recibir datos.
  41.        private void Recepcion(object sender, SerialDataReceivedEventArgs e)
  42.        {
  43.            // Acumula los caracteres recibidos a nuestro 'buffer' (string).
  44.            Recibidos += serialPort1.ReadExisting();
  45.  
  46.            // Invocar o llamar al proceso de tramas.
  47.            Invoke(new EventHandler(Actualizar));
  48.        }
  49.  
  50.        // Procesar los datos recibidos en el bufer y extraer tramas completas.
  51.        private void Actualizar(object sender, EventArgs e)
  52.        {
  53.  
  54.            switch (Recibidos)
  55.            {
  56.                case "ON":
  57.                    panel1.BackColor = Color.Green;
  58.                    label_Lectura.Text = "Activado";
  59.                    pictureBox_Dibujo.Image = Properties.Resources.Led_rojo_encendido;
  60.                    Recibidos = "";
  61.                    break;
  62.  
  63.                case "OFF":
  64.                    panel1.BackColor = Color.Red;
  65.                    label_Lectura.Text = "Desactivado";
  66.                    pictureBox_Dibujo.Image = Properties.Resources.Led_rojo_apagado;
  67.                    Recibidos = "";
  68.                    break;
  69.            }
  70.        }
  71.  
  72.        // Cuando cierre la aplicación.
  73.        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  74.        {
  75.            if (serialPort1.IsOpen) // ¿El puerto está abierto?
  76.            {
  77.                serialPort1.Close(); // Cerrar puerto.
  78.            }
  79.        }
  80.    }
  81. }

He intentado imigar el código de C# a C++/CLR. Pero que va.
Código
  1. #pragma once
  2.  
  3. namespace Entrada_Arduino_CPP_CLR_1 {
  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.  
  14. /// <summary>
  15. /// Resumen de Form_Principal
  16. /// </summary>
  17. public ref class Form_Principal : public System::Windows::Forms::Form
  18. {
  19. // Utilizaremos un string como buffer de recepción.
  20. String^ Recibidos;
  21.  
  22. public:
  23. Form_Principal(void)
  24. {
  25. InitializeComponent();
  26. //
  27. //TODO: agregar código de constructor aquí
  28. //
  29.  
  30. if (!serialPort1->IsOpen)
  31. {
  32. try
  33. {
  34. serialPort1->Open();
  35. }
  36. catch (Exception ^ex)
  37. {
  38. MessageBox::Show(ex->ToString());
  39. }
  40.  
  41. serialPort1->DataReceived += new SerialDataReceivedEventHandler(Recepcion);
  42. }
  43. }
  44.  
  45. // Al recibir datos.
  46. private void Recepcion(object sender, SerialDataReceivedEventArgs e)
  47. {
  48. // Acumula los caracteres recibidos a nuestro 'buffer' (string).
  49. Recibidos += serialPort1->ReadExisting();
  50.  
  51. // Invocar o llamar al proceso de tramas.
  52. Invoke(new EventHandler(Actualizar));
  53. }
  54.  
  55. // Procesar los datos recibidos en el bufer y extraer tramas completas.
  56. private void Actualizar(object sender, EventArgs e)
  57. {
  58.  
  59. switch (Recibidos)
  60. {
  61. case "ON":
  62. panel1->BackColor = Color::Green;
  63. label_Lectura->Text = "Activado";
  64. pictureBox_Dibujo->Image = Properties::Resources::Led_rojo_encendido;
  65. Recibidos = "";
  66. break;
  67.  
  68. case "OFF":
  69. panel1->BackColor = Color::Red;
  70. label_Lectura->Text = "Desactivado";
  71. pictureBox_Dibujo->Image = Properties::Resources::Led_rojo_apagado;
  72. Recibidos = "";
  73. break;
  74. }
  75. }
  76.  
  77. protected:
  78. /// <summary>
  79. /// Limpiar los recursos que se estén usando.
  80. /// </summary>
  81. ~Form_Principal()
  82. {
  83. if (components)
  84. {
  85. delete components;
  86. }
  87. }
  88. private: System::Windows::Forms::Label^  label_titulo;
  89. protected:
  90. private: System::Windows::Forms::Panel^  panel1;
  91. private: System::Windows::Forms::Label^  label_Lectura;
  92. private: System::Windows::Forms::PictureBox^  pictureBox_Dibujo;
  93. private: System::IO::Ports::SerialPort^  serialPort1;
  94. private: System::Windows::Forms::Label^  label1;
  95. private: System::ComponentModel::IContainer^  components;
  96.  
  97. private:
  98. /// <summary>
  99. /// Variable del diseñador necesaria.
  100. /// </summary>
  101.  
  102.  
  103. #pragma region Windows Form Designer generated code
  104. /// <summary>
  105. /// Método necesario para admitir el Diseñador. No se puede modificar
  106. /// el contenido de este método con el editor de código.
  107. /// </summary>
  108. void InitializeComponent(void)
  109. {
  110. this->components = (gcnew System::ComponentModel::Container());
  111. System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form_Principal::typeid));
  112. this->label_titulo = (gcnew System::Windows::Forms::Label());
  113. this->panel1 = (gcnew System::Windows::Forms::Panel());
  114. this->label_Lectura = (gcnew System::Windows::Forms::Label());
  115. this->pictureBox_Dibujo = (gcnew System::Windows::Forms::PictureBox());
  116. this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
  117. this->label1 = (gcnew System::Windows::Forms::Label());
  118. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox_Dibujo))->BeginInit();
  119. this->SuspendLayout();
  120. //
  121. // label_titulo
  122. //
  123. this->label_titulo->AutoSize = true;
  124. this->label_titulo->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
  125. static_cast<System::Byte>(0)));
  126. this->label_titulo->Location = System::Drawing::Point(29, 26);
  127. this->label_titulo->Name = L"label_titulo";
  128. this->label_titulo->Size = System::Drawing::Size(382, 55);
  129. this->label_titulo->TabIndex = 0;
  130. this->label_titulo->Text = L"Visual C++ CLR";
  131. //
  132. // panel1
  133. //
  134. this->panel1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
  135. this->panel1->Location = System::Drawing::Point(23, 97);
  136. this->panel1->Name = L"panel1";
  137. this->panel1->Size = System::Drawing::Size(100, 100);
  138. this->panel1->TabIndex = 1;
  139. //
  140. // label_Lectura
  141. //
  142. this->label_Lectura->AutoSize = true;
  143. this->label_Lectura->Location = System::Drawing::Point(183, 138);
  144. this->label_Lectura->Name = L"label_Lectura";
  145. this->label_Lectura->Size = System::Drawing::Size(48, 13);
  146. this->label_Lectura->TabIndex = 2;
  147. this->label_Lectura->Text = L"Leyendo";
  148. //
  149. // pictureBox_Dibujo
  150. //
  151. this->pictureBox_Dibujo->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBox_Dibujo.Image")));
  152. this->pictureBox_Dibujo->Location = System::Drawing::Point(311, 97);
  153. this->pictureBox_Dibujo->Name = L"pictureBox_Dibujo";
  154. this->pictureBox_Dibujo->Size = System::Drawing::Size(100, 100);
  155. this->pictureBox_Dibujo->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
  156. this->pictureBox_Dibujo->TabIndex = 3;
  157. this->pictureBox_Dibujo->TabStop = false;
  158. //
  159. // serialPort1
  160. //
  161. this->serialPort1->BaudRate = 115200;
  162. this->serialPort1->PortName = L"COM4";
  163. this->serialPort1->StopBits = System::IO::Ports::StopBits::Two;
  164. //
  165. // label1
  166. //
  167. this->label1->AutoSize = true;
  168. this->label1->Location = System::Drawing::Point(349, 200);
  169. this->label1->Name = L"label1";
  170. this->label1->Size = System::Drawing::Size(25, 13);
  171. this->label1->TabIndex = 4;
  172. this->label1->Text = L"Led";
  173. //
  174. // Form_Principal
  175. //
  176. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  177. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  178. this->ClientSize = System::Drawing::Size(436, 262);
  179. this->Controls->Add(this->label1);
  180. this->Controls->Add(this->pictureBox_Dibujo);
  181. this->Controls->Add(this->label_Lectura);
  182. this->Controls->Add(this->panel1);
  183. this->Controls->Add(this->label_titulo);
  184. this->Name = L"Form_Principal";
  185. this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
  186. this->Text = L"Electrónica PIC - C++ 2015";
  187. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox_Dibujo))->EndInit();
  188. this->ResumeLayout(false);
  189. this->PerformLayout();
  190.  
  191. }
  192. #pragma endregion
  193. };
  194. }

¿Alguna ayuda?

Muchas gracias.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Pasar dvd a cd
Multimedia
Ariel 6 3,304 Último mensaje 22 Enero 2004, 18:49 pm
por Ariel
pasar de avi o mpg a dvd
Multimedia
mrbrown 1 3,857 Último mensaje 29 Julio 2004, 10:42 am
por Songoku
pasar un jpg a ico « 1 2 »
Multimedia
Badcode 11 9,099 Último mensaje 26 Agosto 2004, 10:32 am
por Songoku
pasar de wmv a mpg
Multimedia
sontk 1 2,057 Último mensaje 8 Septiembre 2004, 23:51 pm
por Songoku
pasar vqf a mp3
Multimedia
Cobac 3 2,489 Último mensaje 24 Septiembre 2004, 05:51 am
por Songoku
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines