Recepcion de Datos Por Puerto Serial Visual c++ _ Programando Con Huguito

7
Recepciónde datos en C++ a través del puerto serial recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece... 1 de 7 14/07/2013 6:43

Transcript of Recepcion de Datos Por Puerto Serial Visual c++ _ Programando Con Huguito

Recepciónde datos en C++ a través del puerto serial

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

1 de 7 14/07/2013 6:43

publicrefclass Form1 : publicSystem::Windows::Forms::Form

{

public:

intnumimpresiones;

Form1(void)

{

InitializeComponent();

//

//TODO:agregar código de constructor aquí

//

numimpresiones=0;

if(!PuertoSerial->IsOpen)

{

try

{

//se abre el puerto serial

PuertoSerial->Open();

}

catch (Exception ^ex)

{

//manda un mensaje si no hay puerto detectado

MessageBox::Show(“No hay puertos abiertos”);

}

}

Ahora programamos elevento DataReceived del SerialPort:

Antes de ello declaramos un delegado:

delegatevoidDelegado(String ^ s);

Ahora sí programamos el evento DataReceived

//programacióndeleventoDataReceived

private: System::VoidPuertoSerial_DataReceived_1(System::Object^ sender,

System::IO::Ports::SerialDataReceivedEventArgs^ e) {

//sedeclara una variable s de tipo string

String ^ s;

if(PuertoSerial->BytesToRead>0){// Si hay datos a leer…

s=PuertoSerial->ReadExisting(); // Leemosdatos.-

Delegado ^ Escribir=gcnew Delegado(this,&Form1::vCargaDatos);// Invocamos a función que escribe en

TextBox

this->Invoke(Escribir,s);

}

}

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

2 de 7 14/07/2013 6:43

//metodopara imprimir en el TextBoxDatosRecepcion

voidvCargaDatos(String ^Data){

if(numimpresiones<30)

{

//agregaal textboxDatosRecepcion el valor de data

DatosRecepcion->Text+=Data;

//incrementode la variable numimpresiones

numimpresiones=numimpresiones+1;

}

else

{

//limpiatextboxDatosRecepcion

DatosRecepcion->Text=L“”;

//agregaal textboxDatosRecepcion el valor de data

DatosRecepcion->Text+=Data;

//inicializanuevamente la variable numimpresiones en cero

numimpresiones=0;

}

}

#pragmaonce

namespacemyserialport {

usingnamespace System;

usingnamespace System::ComponentModel;

usingnamespace System::Collections;

usingnamespace System::Windows::Forms;

usingnamespace System::Data;

usingnamespace System::Drawing;

/// <summary>

/// Resumen de Form1

/// </summary>

publicrefclass Form1 : publicSystem::Windows::Forms::Form

{

public:

//variableglobal para contar las veces

//quese imprime en el textboxDatosRecepcion

intnumimpresiones;

Form1(void)

{

InitializeComponent();

//

//TODO:agregar código de constructor aquí

//

//seinicializa la variable numimpresiones

numimpresiones=0;

if(!PuertoSerial->IsOpen)

{

try

{

//abre el SerialPort

PuertoSerial->Open();

}

catch (Exception ^ex)

{

//manda un mensaje si no hay puerto detectado

MessageBox::Show(“No se encontró el puerto”);

}

}

}

protected:

/// <summary>

///Limpiar los recursos que se estén utilizando.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::IO::Ports::SerialPort^ PuertoSerial;

private: System::Windows::Forms::TextBox^ DatosRecepcion;

protected:

private: System::Windows::Forms::Label^ lbdatosrecibidos;

private: System::ComponentModel::IContainer^ components;

private:

/// <summary>

///Variable del diseñador requerida.

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

3 de 7 14/07/2013 6:43

/// </summary>

#pragmaregion Windows Form Designer generated code

/// <summary>

///Método necesario para admitir el Diseñador. No se puede modificar

/// elcontenido del método con el editor de código.

/// </summary>

voidInitializeComponent(void)

{

this->components = (gcnewSystem::ComponentModel::Container());

this->PuertoSerial = (gcnewSystem::IO::Ports::SerialPort(this->components));

this->DatosRecepcion = (gcnewSystem::Windows::Forms::TextBox());

this->lbdatosrecibidos = (gcnewSystem::Windows::Forms::Label());

this->SuspendLayout();

//

// PuertoSerial

//

this->PuertoSerial->PortName = L“COM10″;

this->PuertoSerial->StopBits =System::IO::Ports::StopBits::Two;

this->PuertoSerial->DataReceived +=

gcnewSystem::IO::Ports::SerialDataReceivedEventHandler(this,&Form1::PuertoSerial_DataReceived_1);

//

// DatosRecepcion

//

this->DatosRecepcion->Location =System::Drawing::Point(12, 34);

this->DatosRecepcion->Multiline = true;

this->DatosRecepcion->Name = L“DatosRecepcion”;

this->DatosRecepcion->Size = System::Drawing::Size(362,255);

this->DatosRecepcion->TabIndex = 0;

//

//lbdatosrecibidos

//

this->lbdatosrecibidos->AutoSize= true;

this->lbdatosrecibidos->BackColor =System::Drawing::Color::Silver;

this->lbdatosrecibidos->Font = (gcnew System::Drawing::Font(L“MicrosoftSans

Serif”, 9.75F, System::Drawing::FontStyle::Bold,System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(0)));

this->lbdatosrecibidos->Location =System::Drawing::Point(12, 9);

this->lbdatosrecibidos->Name = L“lbdatosrecibidos”;

this->lbdatosrecibidos->Size =System::Drawing::Size(122, 16);

this->lbdatosrecibidos->TabIndex = 1;

this->lbdatosrecibidos->Text= L“Datos recibidos:”;

//

// Form1

//

this->AutoScaleDimensions =System::Drawing::SizeF(6, 13);

this->AutoScaleMode =System::Windows::Forms::AutoScaleMode::Font;

this->BackColor =System::Drawing::Color::LightGray;

this->ClientSize = System::Drawing::Size(386,298);

this->Controls->Add(this->lbdatosrecibidos);

this->Controls->Add(this->DatosRecepcion);

this->Name = L“Form1″;

this->Text = L“Leerpuertoserie”;

this->Load += gcnewSystem::EventHandler(this,&Form1::Form1_Load);

this->ResumeLayout(false);

this->PerformLayout();

}

#pragmaendregion

/*************************************************************************************************************************************/

delegatevoidDelegado(String ^ s);

private: System::VoidPuertoSerial_DataReceived_1(System::Object^ sender,

System::IO::Ports::SerialDataReceivedEventArgs^ e) {

String ^ s;

if(PuertoSerial->BytesToRead>0){ // Si haydatos a leer…

s=PuertoSerial->ReadExisting(); // Leemosdatos.-

Delegado ^ Escribir=gcnew Delegado(this,&Form1::vCargaDatos);// Invocamos a función que escribe en

TextBox

this->Invoke(Escribir,s);

}

}

//metodopara imprimir en el TextBoxDatosRecepcion

voidvCargaDatos(String^ Data){

if(numimpresiones<30)

{

//agregaal textboxDatosRecepcion el valor de data

DatosRecepcion->Text+=Data;

//incrementode la variable numimpresiones

numimpresiones=numimpresiones+1;

}

else

{

//limpiatextboxDatosRecepcion

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

4 de 7 14/07/2013 6:43

DatosRecepcion->Text=L“”;

//agregaal textboxDatosRecepcion el valor de data

DatosRecepcion->Text+=Data;

//inicializanuevamente la variable numimpresiones en cero

numimpresiones=0;

}

}

};

}

Circuito montado en Proteus:

Código del programa para el pic en PIC C

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

5 de 7 14/07/2013 6:43

Cuando no encuentre puerto serie mostrará un MessageBox:

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

6 de 7 14/07/2013 6:43

recepcion de datos por puerto serial visual c++ | Programando con Huguito http://uitoarellanojosehugoarellanoperez.wordpress.com/category/rece...

7 de 7 14/07/2013 6:43