using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace USB_ADC_TESIS
{
public partial class Form1 : Form
{
PicUSBAPI usbapi = new PicUSBAPI();
public Form1()
{
InitializeComponent();
}
unsafe public class PicUSBAPI
{
#region Definición de los Strings: EndPoint y VID_PID
string vid_pid_norm = "vid_04d8&pid_0042";
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;
//Constantes
const int MaxRec = 2;
const int MaxEnv = 2;
//**** FUNCION ABRIR PIPE ****
public void AbrirPipes()
{
DWORD seleccion = 0;
myOutPipe = _MPUSBOpen(seleccion, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(seleccion, vid_pid_norm, in_pipe, 1, 0);
}
//**** FUNCION CERRAR PIPE ****
public void CerrarPipes()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
/**** FUNCIONES ENVIO RECEPCION DE PAQUETES ******/
private void EnvioPaquete(byte* SendPacket, DWORD SendLength)
{
uint Sendelay = 1000;
DWORD SendDataLength;
_MPUSBWrite(myOutPipe, (void*)SendPacket, SendLength, &SendDataLength, Sendelay);
}
private void ReciboPaquete(byte* ReceiveData, DWORD* ReceiveLength)
{
uint ReceiveDelay = 1000;
DWORD ExpectReceiveLentgh = *ReceiveLength;
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectReceiveLentgh, ReceiveLength, ReceiveDelay);
}
/**** PRENDER APAGAR LEDS ******/
public void LedPIC(uint led)
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = 0x01;
send_buf[1] = (byte)led;
EnvioPaquete(send_buf, 2);
}
///////////////////////////////////////////////
/**** FUNCIONES PERSONALIZADAS ****/
//public void RecibirPaquetePic(byte* BufferRec)
public float RecibirPaquetePic()
{
float Conversion = 0;
byte* BufferRec = stackalloc byte[3];
DWORD RecvLentgh = 2;
ReciboPaquete(BufferRec, &RecvLentgh);
// if (BufferRec[0]==0)
// return Conversion = BufferRec[0];
//else
return Conversion = BufferRec[1];
}
// **** FUNCION QUE PIDA DATOS AL PIC ****
public void PideDatosPic()
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = 0x02; //pedimos al pic iniciar ADC
send_buf[1] = 0; // no iplementado
EnvioPaquete(send_buf, 2);
}
}
private void btnAbrir_Click(object sender, EventArgs e)
{
try
{
// CERRAMOS ENLACES ABIERTOS PARA INICIAR NUEVO ENLACE
usbapi.CerrarPipes();
// ABRIENDO NUEVO ENLACE
usbapi.AbrirPipes();
usbapi.LedPIC(0x00);
}
catch
{
MessageBox.Show("NO SE PUDO ABRIR ENLACE");
}
}
private void btnCerrar_Click(object sender, EventArgs e)
{
usbapi.CerrarPipes();
}
private void btnRojo_Click(object sender, EventArgs e)
{
usbapi.LedPIC(0x01);
}
private void btnVerde_Click(object sender, EventArgs e)
{
usbapi.LedPIC(0x02);
}
private void btnADC_Click(object sender, EventArgs e)
{
usbapi.PideDatosPic();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
usbapi.RecibirPaquetePic();
textBox1.Text = usbapi.RecibirPaquetePic().ToString();
this.listBox1.Items.Add(usbapi.RecibirPaquetePic().ToString());
}
private void btnPausa_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
}