Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature currently requires accessing the site using the built-in Safari browser.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Clase para importar DLL
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace AppUSBv1
{
unsafe public class PicUSBAPI
{
#region Definición de los Strings: EndPoint y VID_PID
string vid_pid_norm = "vid_04d8&pid_000B";
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;
/*static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new AppUSBv1());
}*/
public void OpenPipes()
{
DWORD selection = 0;
myOutPipe = _MPUSBOpen(selection, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(selection, vid_pid_norm, in_pipe, 1, 0);
}
public void ClosePipes()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
private void SendPacket(byte* SendData, DWORD SendLength)
{
uint SendDelay = 1000;
DWORD SentDataLength;
OpenPipes();
_MPUSBWrite(myOutPipe, (void*)SendData, SendLength, &SentDataLength, SendDelay);
ClosePipes();
}
private void ReceivePacket(byte* ReceiveData, DWORD *ReceiveLength)
{
uint ReceiveDelay=1000;
DWORD ExpectedReceiveLength = *ReceiveLength;
OpenPipes();
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectedReceiveLength, ReceiveLength, ReceiveDelay);
ClosePipes();
}
/* public void SumaPIC(uint sumando1, uint sumando2)
{
byte* send_buf = stackalloc byte[3];
send_buf[0] = 0x00; // Código de Entrada a Modo_Suma
send_buf[1] = (byte)sumando1;
send_buf[2] = (byte)sumando2;
SendPacket(send_buf, 3);
}
*/
/* public uint ResultadoPIC()
{
uint result = 0;
byte* receive_buf = stackalloc byte[1];
DWORD RecvLength = 1;
ReceivePacket(receive_buf, &RecvLength);
result = receive_buf[0];
return result;
}
*/
/* public void LedPIC(uint led)
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = 0x01; // Código de Entrada a Modo_Led
send_buf[1] = (byte)led;
SendPacket(send_buf, 2);
}
*/
public void Enviar(byte led1)
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = led1; //Notifica al pic que ha de modificar puerto B
//send_buf[1] = salida; //Dato a sacar por puerto B
SendPacket(send_buf, 2); //Envío 2 bytes de información
}
}
}
Hola a todos.
Bueno siguiendo con el tema de mpusbapi.dll, veremos como utilizar las funciones usb_put_packet() y usb_get_packet(). Las cuales permiten enviar datos por el usb o recibir datos.
Bien una descripción breve de las mismas es la siguiente:
Código:usb_put_packet(endpoint,*ptr,len,toggle) // Función completa Vamos a ver lo que está dentro del parentesis: 1 - endpoint: Buffer de entrada o de salida (son unidireccionales) // Ya lo explique post atrás. 2 - *prt : apunta a la dirección del dato a enviar (es una variable declarada por nosotros). 3 - len: es el tamaño del paquete en bytes. (hasta 64 max) 4 - toggle: En la parte de transmisión de datos USB, los paquetes de datos se encuentran en grupos de paquetes de datos, y dentro de estos, existen unos llamados DATA0, DATA1. hay un proceso llamado sincronización del data toggle. a grandes rasgos esto no es mas que un método de validación de paquetes, y lo que hace es enviar alternadamente a DATA0 y DATA1 en una secuencia seguido de su ACK respectivo. todo con el objetivo de mantener la sincronización transmisor <-> receptor. ver fig 1 y 2 Sumario de la función: Esta función manda un paquete de datos hacia el host, el tamaño de los paquetes de datos hay que definirlos en el programa. por ejemplo: #define USB_EP1_TX_SIZE 1 // Tamaño del buffer de salida. #define USB_EP1_RX_SIZE 3 // Tamaño del buffer de entrada.
Buenos dias soy nuevo en el foro le queria hacer una consulta tengo una aplicacion q me manda datos de mi pic a mi pc por usb en forma correcta, mi duda es si quiero enviar este dato en un intervalo de tiempo el buffer cuando envia se limpia solo hoy hay q limpiar el buffer antes de q se llene? Como se limpia el buffer
usb_flush_out(int8 endpoint, USB_DTS_BIT tgl)
pruebalo tu que tienes mas experiencia trabajando con los de gama alta. SUERTE Y gracias por todos tus trabajo e aprendido bastante.
La conexión en MAC es mas facil realizarla con HID porque si no se hace así la programación se complica, y si que se complica, existe en la pagina http://www.signal11.us/oss/hidapi/hi...tml/index.html una librería que facilita la conexión.