Traducir código C# 2013 a C++ 2013
Publicado por Meta (7 intervenciones) el 25/02/2015 13:56:14
Hola:
Tengo este código de C# 2013 que no soy capaz de traducirlo del todo bien a Visual C++ 2013.
Les dejo el código Visual C# 2013 justo abajo.
Lo que he hecho hasta ahora en Visual C++ 2013.
Los errores que me muestran son dos.
1 IntelliSense: inicializador de delegado no válido: la función no coincide con el tipo delegado c:\Users\Meta\Documents\Visual Studio 2013\Projects\Project1\Project1\MyForm.h 41 71 Project1
Y otro más.
2 IntelliSense: inicializador de delegado no válido: la función no coincide con el tipo delegado c:\Users\Meta\Documents\Visual Studio 2013\Projects\Project1\Project1\MyForm.h 52 30 Project1
Tengo este código de C# 2013 que no soy capaz de traducirlo del todo bien a Visual C++ 2013.
Les dejo el código Visual C# 2013 justo abajo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; // No olvidar.
namespace InterDuinoCS
{
public partial class Form_Principal : Form
{
// Utilizremos un string como buffer de recepción.
string Recibidos;
public Form_Principal()
{
InitializeComponent();
// Abrir puerto mientras se ejecute la aplicación.
if(!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
// Ejecutar la función Recepcion por disparo del Evento 'DataReived'.
serialPort1.DataReceived += new SerialDataReceivedEventHandler(Recepcion); // ERROR AQUÍ.
}
}
// Al recibir datos.
private void Recepcion(object sender, SerialDataReceivedEventArgs e)
{
// Acumula los caracteres recibidos a nuestro 'buffer' (string).
Recibidos += serialPort1.ReadExisting();
// Invocar o llamar al proceso de tramas.
Invoke(new EventHandler(Actualizar)); // ERROR AQUÍ.
}
// Procesar los datos recibidos en el bufer y extraer tramas completas.
private void Actualizar(object sender, EventArgs e)
{
// Asignar el valor de la trama al richTextBox.
richTextBox_Mensajes.Text = Recibidos;
// 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();
}
private void button_Led_ON_8_Click(object sender, EventArgs e)
{
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_ON");
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
private void button_Led_OFF_8_Click(object sender, EventArgs e)
{
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_OFF");
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
private void button_Led_ON_13_Click(object sender, EventArgs e)
{
// Enviar tramas de bytes.
byte[] miBuffer = new byte[9]; // Led_13_ON son 9 byte máximo.
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); // Envia las tramas de bytes.
}
private void button_Led_OFF_13_Click(object sender, EventArgs e)
{
// Enviar tramas de bytes.
byte[] miBuffer = new byte[10]; // Led_13_ON son 10 byte máximo.
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); // Envia las tramas de bytes.
}
}
}
Lo que he hecho hasta ahora en Visual C++ 2013.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once
namespace Project1 {
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 MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
// Utilizaremos un string como buffer de recepción.
String^ Recibidos;
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: agregar código de constructor aquí
//
if (!serialPort1->IsOpen)
{
try
{
serialPort1->Open();
}
catch (Exception^ex)
{
MessageBox::Show(ex->ToString());
}
// Ejecutar la función Recepcion por disparo del Evento 'DataReived'.
serialPort1->DataReceived += gcnew SerialDataReceivedEventHandler(Recepcion);
}
}
// Al recibir datos.
private: void Recepcion(Object^ sender, SerialDataReceivedEventArgs^ e)
{
// Acumula los caracteres recibidos a nuestro 'buffer' (sting).
Recibidos += serialPort1->ReadExisting();
// Invocar o llamar al proceso de tramas.
Invoke(gcnew EventHandler(Actualizar));
}
// Procesar los datos recibidos en el buffer y estraer tramas completas.
private: void Actualizar(Object^ sender, EventArgs^ e)
{
// Asignar el valor de la trama al richTextBox.
richTextBox_Mensajes->Text = Recibidos;
// 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();
}
protected:
/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button_Led_8_ON;
protected:
private: System::Windows::Forms::Button^ button_Led_8_OFF;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::Windows::Forms::RichTextBox^ richTextBox_Mensajes;
private: System::ComponentModel::IContainer^ components;
protected:
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->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->richTextBox_Mensajes = (gcnew System::Windows::Forms::RichTextBox());
this->SuspendLayout();
//
// button_Led_8_ON
//
this->button_Led_8_ON->Location = System::Drawing::Point(33, 39);
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, &MyForm::button_Led_8_ON_Click);
//
// button_Led_8_OFF
//
this->button_Led_8_OFF->Location = System::Drawing::Point(33, 81);
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, &MyForm::button_Led_8_OFF_Click);
//
// serialPort1
//
this->serialPort1->BaudRate = 115200;
this->serialPort1->PortName = L"COM4";
//
// richTextBox_Mensajes
//
this->richTextBox_Mensajes->Dock = System::Windows::Forms::DockStyle::Bottom;
this->richTextBox_Mensajes->Location = System::Drawing::Point(0, 137);
this->richTextBox_Mensajes->Name = L"richTextBox_Mensajes";
this->richTextBox_Mensajes->Size = System::Drawing::Size(284, 125);
this->richTextBox_Mensajes->TabIndex = 2;
this->richTextBox_Mensajes->Text = L"";
//
// MyForm
//
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->richTextBox_Mensajes);
this->Controls->Add(this->button_Led_8_OFF);
this->Controls->Add(this->button_Led_8_ON);
this->Name = L"MyForm";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"MyForm";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button_Led_8_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_8_OFF_Click(System::Object^ sender, System::EventArgs^ e) {
array<Byte> ^miBuffer = Encoding::ASCII->GetBytes("Led_13_OFF");
serialPort1->Write(miBuffer, 0, miBuffer->Length);
}
};
}
Los errores que me muestran son dos.
1 IntelliSense: inicializador de delegado no válido: la función no coincide con el tipo delegado c:\Users\Meta\Documents\Visual Studio 2013\Projects\Project1\Project1\MyForm.h 41 71 Project1
Y otro más.
2 IntelliSense: inicializador de delegado no válido: la función no coincide con el tipo delegado c:\Users\Meta\Documents\Visual Studio 2013\Projects\Project1\Project1\MyForm.h 52 30 Project1
Valora esta pregunta


0