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.
Código: Seleccionar todo
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.
}
}
}
Código: Seleccionar todo
#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);
}
};
}
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