DE Visual C++ a Visual C#...

(1/1)

Meta:
Hola:

Intentando pasar de VC# a VC++ esta parte que parece que no sirve.

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.Windows.Forms;
9 using System.IO.Ports;
10
11 namespace PicRS232
12 {
13    public partial class Form1_Principal : Form
14    {
15        // Utilizaremos un string como buffer de recepcion
16        string Recibidos;
17        public Form1_Principal()
18        {
19            InitializeComponent();
20            // Abrir puerto mientra se ejecute la aplicación
21            if (!serialPort1.IsOpen)
22            {
23                try
24                {
25                    serialPort1.Open();
26                }
27                catch (System.Exception ex)
28                {
29                    MessageBox.Show(ex.ToString());
30                }
31            }
32            // Ejecutar la funcion Recepcion por disparo del Evento 'DataReived'
33            serialPort1.DataReceived += new
34            System.IO.Ports.SerialDataReceivedEventHandler(Recepcion);
35        }
36        // Al recibir los datos
37        private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
38        {
39            // Acumular los carácteres recibidos a nuestro 'buffer' (string)
40            Recibidos += serialPort1.ReadExisting();
41            // Invocar o llamar al proceso de tramas
42            this.Invoke(new EventHandler(Actualizar));
43        }
44        // Procesar los datos recibidos en el buffer y extraer tramas completas
45        private void Actualizar(object s, EventArgs e)
46        {
47            // Asignar el valor de la trama al textBox
48            textBox_visualizar_mensaje.Text = Recibidos;
49        }
50        private void button_t_Click(object sender, EventArgs e)
51        {
52            byte[] mBuffer = new byte[1];
53            mBuffer[0] = 0x74; //ASCII letra "t".
54            serialPort1.Write(mBuffer, 0, mBuffer.Length);
55        }
56
57        private void button_b_Click(object sender, EventArgs e)
58        {
59            byte[] miBuffer = new byte[1];
60            miBuffer[0] = 0x62; //ASCII letra "b".
61            serialPort1.Write(miBuffer, 0, miBuffer.Length);
62        }
63
64        private void button_a_Click(object sender, EventArgs e)
65        {
66            byte[] mBuffer = new byte[1];
67            mBuffer[0] = 0x61; //ASCII letra "a".
68            serialPort1.Write(mBuffer, 0, mBuffer.Length);
69        }
70
71        private void button_l_Click(object sender, EventArgs e)
72        {
73            byte[] mBuffer = new byte[1];
74            mBuffer[0] = 0x6C; //ASCII letra "l".
75            serialPort1.Write(mBuffer, 0, mBuffer.Length);
76        }
77
78        private void button_Espacio_Click(object sender, EventArgs e)
79        {
80            byte[] mBuffer = new byte[1];
81            mBuffer[0] = 0x20; //ASCII letra "Espacio".
82            serialPort1.Write(mBuffer, 0, mBuffer.Length);
83        }
84
85        private void timer1_Tick(object sender, EventArgs e)
86        {
87            statusStrip1.Items[0].Text = DateTime.Now.ToLongTimeString();
88        }
89    }
90 }

Código completo de Visual C# Express. Descargar aquí.

Por ahora he hecho el código en Visual C++ pero me faltan hacer cosas que no he incuido.
Código
1 #pragma once
2
3
4 namespace PicRS232VCPP_Prueba01 {
5
6    using namespace System;
7    using namespace System::ComponentModel;
8    using namespace System::Collections;
9    using namespace System::Windows::Forms;
10    using namespace System::Data;
11    using namespace System::Drawing;
12    using namespace System::IO::Ports;
13
14    /// <summary>
15    /// Resumen de Form1
16    ///
17    /// ADVERTENCIA: si cambia el nombre de esta clase, deberá cambiar la
18    ///          propiedad 'Nombre de archivos de recursos' de la herramienta de compilación de recursos administrados
19    ///          asociada con todos los archivos .resx de los que depende esta clase. De lo contrario,
20    ///          los diseñadores no podrán interactuar correctamente con los
21    ///          recursos adaptados asociados con este formulario.
22    /// </summary>
23    public ref class Form1 : public System::Windows::Forms::Form
24    {
25    public:
26        Form1(void)
27        {
28            InitializeComponent();
29            //
30            //TODO: agregar código de constructor aquí
31            //
32            // Abrir puerto mientrase ejecute la aplicación
33            if(!serialPort1->IsOpen)
34            {
35                try
36                {
37                serialPort1->Open();
38                }
39                catch (Exception ^ex)
40                {
41                    MessageBox::Show(ex->ToString());
42                }
43            }
44        }
45
46    protected:
47        /// <summary>
48        /// Limpiar los recursos que se estén utilizando.
49        /// </summary>
50        ~Form1()
51        {
52            if (components)
53            {
54                delete components;
55            }
56        }
57    private: System::Windows::Forms::Button^  button_t;
58    private: System::Windows::Forms::Button^  button_b;
59    private: System::Windows::Forms::Button^  button_a;
60    private: System::Windows::Forms::Button^  button_l;
61    private: System::Windows::Forms::Button^  button_Espacio;
62    private: System::IO::Ports::SerialPort^  serialPort1;
63    private: System::ComponentModel::IContainer^  components;
64
65    protected:  
66
67    private:
68        /// <summary>
69        /// Variable del diseñador requerida.
70        /// </summary>
71
72
73 #pragma region Windows Form Designer generated code
74        /// <summary>
75        /// Método necesario para admitir el Diseñador. No se puede modificar
76        /// el contenido del método con el editor de código.
77        /// </summary>
78        void InitializeComponent(void)
79        {
80            this->components = (gcnew System::ComponentModel::Container());
81            this->button_t = (gcnew System::Windows::Forms::Button());
82            this->button_b = (gcnew System::Windows::Forms::Button());
83            this->button_a = (gcnew System::Windows::Forms::Button());
84            this->button_l = (gcnew System::Windows::Forms::Button());
85            this->button_Espacio = (gcnew System::Windows::Forms::Button());
86            this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
87            this->SuspendLayout();
88            //  
89            // button_t
90            //  
91            this->button_t->Location = System::Drawing::Point(109, 38);
92            this->button_t->Name = L"button_t";
93            this->button_t->Size = System::Drawing::Size(75, 23);
94            this->button_t->TabIndex = 0;
95            this->button_t->Text = L"t";
96            this->button_t->UseVisualStyleBackColor = true;
97            this->button_t->Click += gcnew System::EventHandler(this, &Form1::button_t_Click);
98            //  
99            // button_b
100            //  
101            this->button_b->Location = System::Drawing::Point(109, 67);
102            this->button_b->Name = L"button_b";
103            this->button_b->Size = System::Drawing::Size(75, 23);
104            this->button_b->TabIndex = 1;
105            this->button_b->Text = L"b";
106            this->button_b->UseVisualStyleBackColor = true;
107            this->button_b->Click += gcnew System::EventHandler(this, &Form1::button_b_Click);
108            //  
109            // button_a
110            //  
111            this->button_a->Location = System::Drawing::Point(28, 67);
112            this->button_a->Name = L"button_a";
113            this->button_a->Size = System::Drawing::Size(75, 23);
114            this->button_a->TabIndex = 2;
115            this->button_a->Text = L"a";
116            this->button_a->UseVisualStyleBackColor = true;
117            this->button_a->Click += gcnew System::EventHandler(this, &Form1::button_a_Click);
118            //  
119            // button_l
120            //  
121            this->button_l->Location = System::Drawing::Point(190, 67);
122            this->button_l->Name = L"button_l";
123            this->button_l->Size = System::Drawing::Size(75, 23);
124            this->button_l->TabIndex = 3;
125            this->button_l->Text = L"l";
126            this->button_l->UseVisualStyleBackColor = true;
127            this->button_l->Click += gcnew System::EventHandler(this, &Form1::button_l_Click);
128            //  
129            // button_Espacio
130            //  
131            this->button_Espacio->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(128)),  
132                static_cast<System::Int32>(static_cast<System::Byte>(0)));
133            this->button_Espacio->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,  
134                static_cast<System::Byte>(0)));
135            this->button_Espacio->Location = System::Drawing::Point(190, 96);
136            this->button_Espacio->Name = L"button_Espacio";
137            this->button_Espacio->Size = System::Drawing::Size(75, 23);
138            this->button_Espacio->TabIndex = 4;
139            this->button_Espacio->Text = L"Espacio";
140            this->button_Espacio->UseVisualStyleBackColor = false;
141            this->button_Espacio->Click += gcnew System::EventHandler(this, &Form1::button_Espacio_Click);
142            //  
143            // serialPort1
144            //  
145            this->serialPort1->StopBits = System::IO::Ports::StopBits::Two;
146            //  
147            // Form1
148            //  
149            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
150            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
151            this->ClientSize = System::Drawing::Size(292, 266);
152            this->Controls->Add(this->button_Espacio);
153            this->Controls->Add(this->button_l);
154            this->Controls->Add(this->button_a);
155            this->Controls->Add(this->button_b);
156            this->Controls->Add(this->button_t);
157            this->Name = L"Form1";
158            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
159            this->Text = L"PicRS232VCPP";
160            this->ResumeLayout(false);
161
162        }
163 #pragma endregion
164    private: System::Void button_t_Click(System::Object^  sender, System::EventArgs^  e) {
165                 cli::array<unsigned char> ^uno = gcnew cli::array<unsigned char>(1);
166                 uno[0] = 0x74; //ASCII letra "t".
167                 serialPort1->Write(uno, 0, 1);
168             }
169 private: System::Void button_b_Click(System::Object^  sender, System::EventArgs^  e) {
170                 cli::array<unsigned char> ^uno = gcnew cli::array<unsigned char>(1);
171                 uno[0] = 0x62; //ASCII letra "b".
172                 serialPort1->Write(uno, 0, 1);
173         }
174 private: System::Void button_a_Click(System::Object^  sender, System::EventArgs^  e) {
175             cli::array<unsigned char> ^uno = gcnew cli::array<unsigned char>(1);
176                 uno[0] = 0x61; //ASCII letra "a".
177                 serialPort1->Write(uno, 0, 1);
178         }
179 private: System::Void button_l_Click(System::Object^  sender, System::EventArgs^  e) {
180             cli::array<unsigned char> ^uno = gcnew cli::array<unsigned char>(1);
181                 uno[0] = 0x6C; //ASCII letra "l".
182                 serialPort1->Write(uno, 0, 1);
183         }
184 private: System::Void button_Espacio_Click(System::Object^  sender, System::EventArgs^  e) {
185             cli::array<unsigned char> ^uno = gcnew cli::array<unsigned char>(1);
186                 uno[0] = 0x20; //ASCII letra "Espacio".
187                 serialPort1->Write(uno, 0, 1);
188         }
189 };
190 }
191
   


Saludos.

Navegación

[0] Índice de Mensajes