using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace ONDAS_PLETISMOGRAFICAS
{
unsafe public class PICUSBAPI
{
#region DEFINICION DE STRINGS ENDPOINT , VID_PID
string vid_pid_norm = "vid_04d8&pid_0052";
string out_pipe = "\\MCHP_EP1";
string in_pipe = "\\MCHP_EP1";
#endregion
#region FUNCIONES IMPORTADAS DE LA DLL: mpusbapi.dll
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDeviceCount(string pVID_PID);
[DllImport("mpusbapi.dll")]
private static extern void* _MPUSBOpen(DWORD instance, string pVID_PID, string pEP, DWORD dwDir, DWORD dwReserved);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBRead(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBWrite(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBReadInt(void* handle, DWORD* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern bool _MPUSBClose(void* handle);
#endregion
void* myOutPipe;
void* myInPipe;
#region ABRIR CERRAR PUERTO
// METODO ABRIR PIPE//
public void AbrirPipe()
{
DWORD seleccion = 0;
myOutPipe = _MPUSBOpen(seleccion, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(seleccion, vid_pid_norm, in_pipe, 1, 0);
}
public void CerrarPipe()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
//METODOS ENVIO RECEPCION PAQUETES
private void EnvioPaquetes(byte* SendPacket, DWORD SendLength)
{
uint sendelay = 4000000000;
DWORD SendDataLength;
_MPUSBWrite(myOutPipe, (void*)SendPacket, SendLength, &SendDataLength, sendelay);
}
private void ReciboPaquete(byte* ReceiveData, DWORD* ReceiveLentgh)
{
uint ReceiveDelay = 4000000000;
DWORD ExpectReceiveLength = *ReceiveLentgh;
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectReceiveLength, ReceiveLentgh, ReceiveDelay);
}
//METODO PRENDER APAGAR LEDS //
public void LedPic(uint Led)
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = 0x00;
send_buf[1] = (byte)Led;
EnvioPaquetes(send_buf, 2);
}
#endregion
#region ENVIO-RECIBO SEÑALES INDIVIDUALES
//METODO RECIBE ENVIA DATOS ADC PIC 1
//SEÑAL ROJO_AC_4
public void PideDatos_ADC_ROJO_4_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x02;
Envio_Buff[1] = 0; //No implementado
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_ROJO_4_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_rojo_4_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_rojo_4_pic1[0] = recibir_buff[0];
recibe_adc_rojo_4_pic1[1] = recibir_buff[1];
recibe_adc_rojo_4_pic1[2] = recibir_buff[2];
recibe_adc_rojo_4_pic1[3] = recibir_buff[3];
recibe_adc_rojo_4_pic1[4] = recibir_buff[4];
recibe_adc_rojo_4_pic1[5] = recibir_buff[5];
recibe_adc_rojo_4_pic1[6] = recibir_buff[6];
recibe_adc_rojo_4_pic1[7] = recibir_buff[7];
return recibe_adc_rojo_4_pic1;
}
//METODO RECIBE ENVIA DATOS ADC PIC 1
//SEÑAL ROJO_AC_8
public void PideDatos_ADC_ROJO_8_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x03;
Envio_Buff[1] = 0; //No implementado
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_ROJO_8_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_rojo_8_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_rojo_8_pic1[0] = recibir_buff[0];
recibe_adc_rojo_8_pic1[1] = recibir_buff[1];
recibe_adc_rojo_8_pic1[2] = recibir_buff[2];
recibe_adc_rojo_8_pic1[3] = recibir_buff[3];
recibe_adc_rojo_8_pic1[4] = recibir_buff[4];
recibe_adc_rojo_8_pic1[5] = recibir_buff[5];
recibe_adc_rojo_8_pic1[6] = recibir_buff[6];
recibe_adc_rojo_8_pic1[7] = recibir_buff[7];
return recibe_adc_rojo_8_pic1;
}
//METODO ENVIA-RECIBE DATOS ADC PIC1
// SEÑAL: INFR_AC_4
public void PideDatos_ADC_INFR_AC_4_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x04;
Envio_Buff[1] = 0;
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_INFR_AC_4_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_infr_ac_4_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_infr_ac_4_pic1[0] = recibir_buff[0];
recibe_adc_infr_ac_4_pic1[1] = recibir_buff[1];
recibe_adc_infr_ac_4_pic1[2] = recibir_buff[2];
recibe_adc_infr_ac_4_pic1[3] = recibir_buff[3];
recibe_adc_infr_ac_4_pic1[4] = recibir_buff[4];
recibe_adc_infr_ac_4_pic1[5] = recibir_buff[5];
recibe_adc_infr_ac_4_pic1[6] = recibir_buff[6];
recibe_adc_infr_ac_4_pic1[7] = recibir_buff[7];
return recibe_adc_infr_ac_4_pic1;
}
//METODO ENVIA RECIBE DATOS ADC PIC1
// SEÑAL: INFR_AC_8
public void PideDatos_ADC_Infr_AC_8_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x05;
Envio_Buff[1] = 0;
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_Infr_AC_8_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_infr_ac_8_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_infr_ac_8_pic1[0] = recibir_buff[0];
recibe_adc_infr_ac_8_pic1[1] = recibir_buff[1];
recibe_adc_infr_ac_8_pic1[2] = recibir_buff[2];
recibe_adc_infr_ac_8_pic1[3] = recibir_buff[3];
recibe_adc_infr_ac_8_pic1[4] = recibir_buff[4];
recibe_adc_infr_ac_8_pic1[5] = recibir_buff[5];
recibe_adc_infr_ac_8_pic1[6] = recibir_buff[6];
recibe_adc_infr_ac_8_pic1[7] = recibir_buff[7];
return recibe_adc_infr_ac_8_pic1;
}
//METODO ENVIA-RECIBE DATOS
// SEÑAL ROJO_DC
public void PideDatos_ADC_Rojo_DC_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x06;
Envio_Buff[1] = 0;
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_Rojo_DC_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_rojo_dc_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_rojo_dc_pic1[0] = recibir_buff[0];
recibe_adc_rojo_dc_pic1[1] = recibir_buff[1];
recibe_adc_rojo_dc_pic1[2] = recibir_buff[2];
recibe_adc_rojo_dc_pic1[3] = recibir_buff[3];
recibe_adc_rojo_dc_pic1[4] = recibir_buff[4];
recibe_adc_rojo_dc_pic1[5] = recibir_buff[5];
recibe_adc_rojo_dc_pic1[6] = recibir_buff[6];
recibe_adc_rojo_dc_pic1[7] = recibir_buff[7];
return recibe_adc_rojo_dc_pic1;
}
//METODO ENVIA-RECIBE DATOS
// SEÑAL INFR_DC
public void PideDatos_ADC_Infr_DC_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x07;
Envio_Buff[1] = 0;
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_Infr_DC_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_infr_dc_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_infr_dc_pic1[0] = recibir_buff[0];
recibe_adc_infr_dc_pic1[1] = recibir_buff[1];
recibe_adc_infr_dc_pic1[2] = recibir_buff[2];
recibe_adc_infr_dc_pic1[3] = recibir_buff[3];
recibe_adc_infr_dc_pic1[4] = recibir_buff[4];
recibe_adc_infr_dc_pic1[5] = recibir_buff[5];
recibe_adc_infr_dc_pic1[6] = recibir_buff[6];
recibe_adc_infr_dc_pic1[7] = recibir_buff[7];
return recibe_adc_infr_dc_pic1;
}
//METODO ENVIA-RECIBE DATOS
// SEÑAL LUZ_DC
public void PideDatos_ADC_Luz_DC_PIC1()
{
byte* Envio_Buff = stackalloc byte[2];
Envio_Buff[0] = 0x08;
Envio_Buff[1] = 0;
EnvioPaquetes(Envio_Buff, 2);
}
public float[] RecibeDatos_ADC_Luz_DC_PIC1(uint velo)
{
uint veloH;
veloH = velo / 2;
DWORD RecvLength = 8;
float[] recibe_adc_luz_dc_pic1 = new float[8];
byte* recibir_buff = stackalloc byte[8];
ReciboPaquete(recibir_buff, &RecvLength);
recibe_adc_luz_dc_pic1[0] = recibir_buff[0];
recibe_adc_luz_dc_pic1[1] = recibir_buff[1];
recibe_adc_luz_dc_pic1[2] = recibir_buff[2];
recibe_adc_luz_dc_pic1[3] = recibir_buff[3];
recibe_adc_luz_dc_pic1[4] = recibir_buff[4];
recibe_adc_luz_dc_pic1[5] = recibir_buff[5];
recibe_adc_luz_dc_pic1[6] = recibir_buff[6];
recibe_adc_luz_dc_pic1[7] = recibir_buff[7];
return recibe_adc_luz_dc_pic1;
}
#endregion
}
}
- Es hora de volver al código principal, para usar la clase que hemos creado haremos los siguiente:usbapi.AbrirPipe();
usbapi.LedPic(0x00);http://img70.imageshack.us/img70/2180/cpaso17.jpg private void btnCerrar_Click(object sender, EventArgs e)
{
usbapi.CerrarPipe();
}
private void btnRojo_Click(object sender, EventArgs e)
{
usbapi.LedPic(0x02);
}
private void btnVerde_Click(object sender, EventArgs e)
{
usbapi.LedPic(0x01);
}- OK, empecemos a recibir y plotear los datos, primero declaramos en la parte public Form1(), lo siguiente: //PEDIMOS AL PIC 1 INICIALIZAR EL MODULO ADC ROJO AC_4TO
usbapi.PideDatos_ADC_ROJO_4_PIC1();
//----- CREAMOS UN ARREGLO PARA ALMACENAR DATOS
float[] Resultado = new float[8];
//----- ALMACENAMOS LOS DATOS PROVENIENTES DEL ADC DEL PIC 1
Resultado = usbapi.RecibeDatos_ADC_ROJO_4_PIC1(4);
//----- CREAMOS UN BUFFER AUXILIAR PARA GRAFICAR DATOS
float[] Buff_Aux = new float[4];
Buff_Aux[0] = ((Resultado[0] + Resultado[1] * 256) * 5 / 4096);
Buff_Aux[1] = ((Resultado[2] + Resultado[3] * 256) * 5 / 4096);
Buff_Aux[2] = ((Resultado[4] + Resultado[5] * 256) * 5 / 4096);
Buff_Aux[3] = ((Resultado[6] + Resultado[7] * 256) * 5 / 4096);
plotter1.Channels[0].CurrentValue = Buff_Aux[0] / 25;
plotter1.UpdateDisplay();
plotter1.Channels[0].CurrentValue = Buff_Aux[1] / 25;
plotter1.UpdateDisplay();
plotter1.Channels[0].CurrentValue = Buff_Aux[2] / 25;
plotter1.UpdateDisplay();
plotter1.Channels[0].CurrentValue = Buff_Aux[3] / 25;
plotter1.UpdateDisplay();- Describiendo un poco el cod anterior :Gracias a todos por sus comentarios, pero esto no lo hubiese realizado sin la ayuda de la diversidad de articulos asi como de tutoriales en todo el foro.
Att.
Washington.
Gracias a todos por sus comentarios, pero esto no lo hubiese realizado sin la ayuda de la diversidad de articulos asi como de tutoriales en todo el foro.
Att.
Washington.