Documentos de Académico
Documentos de Profesional
Documentos de Cultura
namespace ComunicacionVCUSB {
/// <summary>
/// Resumen de Form1
/////////////////////////////////////////////////////////////////////////////
// //
// Programa para conectar mediante USB el PC y el PIC18F4550 //
// (Enviamos datos por el USB) //
// Si enviamos 'a': Encendemos Led Rojo //
// Si enviamos 'b': Encendemos Led Verde //
// Si enviamos 'l': Encendemos Led Amarillo //
// Si enviamos 'c': Apagamos todos los Led's //
// //
// Creado por: David Lopez Anton & Sergio Gala Trallero //
// Primavera 2011 //
// Comunicacion VC++ USB PFC-1 //
////////////////////////////////////////////////////////////////////////////
- 42 -
Red inalmbrica para instrumentacin de procesos
/// </summary>
public:
Form1(void)
{
InitializeComponent(); // Inicializamos componentes
UpdateCOMPortList(); // Actualizamos los puertos USB
//
//TODO: agregar cdigo de constructor aqu
//
}
protected:
/// <summary>
/// Limpiar los recursos que se estn utilizando.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
protected:
private:
/// <summary>
/// Variable del diseador requerida.
/// </summary>
/// <summary>
/// Mtodo necesario para admitir el Diseador. No se puede modificar
/// el contenido del mtodo con el editor de cdigo.
/// </summary>
- 43 -
David Lpez Antn & Sergio Gala Trallero
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->EnviarDatos = (gcnew System::Windows::Forms::Button());
this->btnConexion = (gcnew System::Windows::Forms::Button());
this->txtDatosRecibidos = (gcnew System::Windows::Forms::TextBox());
this->txtEnviarTexto = (gcnew System::Windows::Forms::TextBox());
this->PuertosCOM = (gcnew System::Windows::Forms::ComboBox());
this->btnDesconexion = (gcnew System::Windows::Forms::Button());
this->serialPort2 = (gcnew System::IO::Ports::SerialPort(this->components));
this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// EnviarDatos
//
this->EnviarDatos->Location = System::Drawing::Point(6, 44);
this->EnviarDatos->Name = L"EnviarDatos";
this->EnviarDatos->Size = System::Drawing::Size(85, 19);
this->EnviarDatos->TabIndex = 13;
this->EnviarDatos->Text = L"Enviar Datos";
this->EnviarDatos->UseVisualStyleBackColor = true;
this->EnviarDatos->Click += gcnew System::EventHandler(this,
&Form1::EnviarDatos_Click);
//
// btnConexion
//
this->btnConexion->Location = System::Drawing::Point(3, 12);
this->btnConexion->Name = L"btnConexion";
this->btnConexion->Size = System::Drawing::Size(89, 19);
this->btnConexion->TabIndex = 12;
this->btnConexion->Text = L"Conexin";
this->btnConexion->UseVisualStyleBackColor = true;
this->btnConexion->Click += gcnew System::EventHandler(this,
&Form1::btnConexion_Click);
//
// txtDatosRecibidos
//
this->txtDatosRecibidos->Location = System::Drawing::Point(3, 79);
this->txtDatosRecibidos->Multiline = true;
this->txtDatosRecibidos->Name = L"txtDatosRecibidos";
this->txtDatosRecibidos->Size = System::Drawing::Size(333, 153);
this->txtDatosRecibidos->TabIndex = 11;
this->txtDatosRecibidos->Text = L"Esperando conexin...";
//
// txtEnviarTexto
//
this->txtEnviarTexto->Location = System::Drawing::Point(101, 44);
this->txtEnviarTexto->Name = L"txtEnviarTexto";
this->txtEnviarTexto->Size = System::Drawing::Size(138, 20);
this->txtEnviarTexto->TabIndex = 10;
//
// PuertosCOM
//
this->PuertosCOM->FormattingEnabled = true;
this->PuertosCOM->Location = System::Drawing::Point(99, 12);
this->PuertosCOM->Name = L"PuertosCOM";
this->PuertosCOM->Size = System::Drawing::Size(141, 21);
this->PuertosCOM->TabIndex = 9;
//
// btnDesconexion
//
this->btnDesconexion->Location = System::Drawing::Point(246, 12);
this->btnDesconexion->Name = L"btnDesconexion";
this->btnDesconexion->Size = System::Drawing::Size(90, 20);
- 44 -
Red inalmbrica para instrumentacin de procesos
this->btnDesconexion->TabIndex = 8;
this->btnDesconexion->Text = L"Desconexin";
this->btnDesconexion->UseVisualStyleBackColor = true;
this->btnDesconexion->Click += gcnew System::EventHandler(this,
&Form1::btnDesconexion_Click);
//
// serialPort2
//
this->serialPort2->DataReceived += gcnew
System::IO::Ports::SerialDataReceivedEventHandler(this,
&Form1::serialPort2_DatosRecibidos);
//
// timer2
//
this->timer2->Enabled = true;
this->timer2->Interval = 1;
this->timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(342, 238);
this->Controls->Add(this->EnviarDatos);
this->Controls->Add(this->btnConexion);
this->Controls->Add(this->txtDatosRecibidos);
this->Controls->Add(this->txtEnviarTexto);
this->Controls->Add(this->PuertosCOM);
this->Controls->Add(this->btnDesconexion);
this->Name = L"Form1";
this->Text = L"Comunicacin VC++ USB";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//////////////////////////////////////////////////////////
// //
// RUTINA DE DETECCION DE PUERTOS USB //
// //
//////////////////////////////////////////////////////////
- 45 -
David Lpez Antn & Sergio Gala Trallero
else
{
foundDifference = true;
}
/////////////////////////////////////////////////////////////////////////////
// //
// RUTINA TIMER (DETECCION NUEVA CONEXION USB EN FUNCIONAMIENTO //
// //
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// //
// RUTINA DE BOTON CONEXION //
// //
//////////////////////////////////////////////////////////
- 46 -
Red inalmbrica para instrumentacin de procesos
//////////////////////////////////////////////////////////
// //
// RUTINA DE BOTON DESCONEXION //
// //
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// //
// RUTINA DE BOTON ENVIAR DATOS //
// //
//////////////////////////////////////////////////////////
- 47 -
David Lpez Antn & Sergio Gala Trallero
//////////////////////////////////////////////////////////
// //
// RUTINA RECEPCION DATOS PUERTO SERIE (USB) //
// //
//////////////////////////////////////////////////////////
/****************************************************************************
Function: private void SetText(string text)
Summary: This function prints the input text to the txtDataReceived
textbox.
Precondition: None
////////////////////////////////////////////////////////////////
// //
// RUTINA ESCRITURA DATOS RECIBIDOS EN TEXTBOX //
// //
////////////////////////////////////////////////////////////////
- 48 -
Red inalmbrica para instrumentacin de procesos
SetTextCallback^ d = gcnew
SetTextCallback(this,&ComunicacionVCUSB::Form1::SetText);
};
}
En las siguientes figuras podemos observar como a travs del programa Visual
C++ 2010, podemos enviar caracteres a travs del puerto USB (mientras
seguimos recibiendo informacin) para cambiar el estado de los LEDs.
- 49 -