TODOPIC

Lenguajes de programación para PC => C, C#, C++ => Mensaje iniciado por: Mr.Hell en 11 de Julio de 2011, 23:37:34

Título: Abrir pipes con MPUSBAPI.DLL
Publicado por: Mr.Hell en 11 de Julio de 2011, 23:37:34
Antes que nada, hola a todos. Quería hacer una consulta, dado que he estado intentando lograr una interfaz USB entre un PIC18F4550 y un programa en Visual C++ 2005, pero no logro abrir las pipes, ni la de entrada, ni la de salida. La aplicación logra cargar exitosamente la DLL, obtener su version y obtener la cantidad de dispositivos USB con un VID y PID específico, pero a la hora de abrir las pipes, siempre devuelve INVALID_HANDLE_VALUE (-1). Estuve mirando en distintos temas, en distintas páginas web, pero no pude resolver mi problema.
Código: [Seleccionar]
typedef DWORD (__cdecl *MPUSBGetDeviceCount) (string pVID_PID);
typedef void* (__cdecl *MPUSBOpen) (DWORD instance, string pVID_PID, string pEP, DWORD dwDir, DWORD dwReserved);
typedef DWORD (__cdecl *MPUSBRW) (void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
//typedef DWORD (__cdecl *MPUSBWrite) (void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds); //Se utiliza MPUSBRW
typedef DWORD (__cdecl *MPUSBReadInt) (void* handle, DWORD* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
typedef bool (__cdecl *MPUSBClose) (void* handle);

void ConvertirVersion(DWORD VerGET, BYTE* VerPARSE);
void SendPacket(BYTE* SendData, DWORD SendLength);
void ReceivePacket(byte* ReceiveData, DWORD* ReceiveLength);
void AbrirPipes(void);
void CerrarPipes(void);

//Declaraciones globales
BYTE VersionDLL[4]; //Variable para la version de la DLL.
DWORD err; //Contador de errores.
HINSTANCE hDLL; //HANDLE a la DLL.
void* hUSB_DeviceOut; //HANDLE al dispositivo USB (Salida)
void* hUSB_DeviceInp; //HANDLE al dispositivo USB (Entrada)
FARPROC USB_GetDLLVersion; //Funcion
MPUSBGetDeviceCount USB_GetDeviceCount; //Funcion
MPUSBOpen USB_Open; //Funcion
MPUSBRW USB_Read; //Funcion
MPUSBRW USB_Write; //Funcion
MPUSBReadInt USB_ReadInt; //Funcion
MPUSBClose USB_Close; //Funcion
string VID_PID  = "vid_0925&pid_1231"; //VID y PID del dispositivo
string out_pipe = "\\MCHP_EP1"; //EndPoint de salida a usar
string inp_pipe = "\\MCHP_EP1"; //EndPoint de entrada a usar

int main(int argc, char* argv[])
{
hDLL = LoadLibrary(L"mpusbapi");

if(hDLL != NULL)
cout << "Dll cargada con exito" << endl;
else
{
        err = GetLastError();
cout << "Error al cargar la dll\nCodigo: " << err << endl;
system("pause");
return (-1);
}

USB_GetDLLVersion  = (FARPROC) GetProcAddress(HMODULE (hDLL), "_MPUSBGetDLLVersion");
USB_GetDeviceCount = (MPUSBGetDeviceCount) GetProcAddress(HMODULE (hDLL), "_MPUSBGetDeviceCount");
USB_Open = (MPUSBOpen) GetProcAddress(HMODULE (hDLL), "_MPUSBOpen");
USB_Read = (MPUSBRW) GetProcAddress(HMODULE (hDLL), "_MPUSBRead");
USB_Write = (MPUSBRW) GetProcAddress(HMODULE (hDLL), "_MPUSBWrite");
USB_ReadInt = (MPUSBReadInt) GetProcAddress(HMODULE (hDLL), "_MPUSBReadInt");
USB_Close   = (MPUSBClose) GetProcAddress(HMODULE (hDLL), "_MPUSBClose");
#ifdef DEBUG-DLL
cout << "1- " << USB_GetDLLVersion << endl;
cout << "2- " << USB_GetDeviceCount << endl;
cout << "3- " << USB_Open << endl;
cout << "4- " << USB_Read << endl;
cout << "5- " << USB_Write << endl;
cout << "6- " << USB_ReadInt << endl;
cout << "7- " << USB_Close << endl;
#endif

ConvertirVersion(USB_GetDLLVersion(), VersionDLL);
printf("MPUSBAPI.DLL version: %u,%u,%u,%u\r\n",VersionDLL[0],VersionDLL[1],VersionDLL[2],VersionDLL[3]);
system("pause");

#ifdef VER-ERR
cout << "Verificando ultimo error..." << endl;
err = GetLastError();
if(err != NULL)
cout << "Error. Codigo: " << err << endl;
else
cout << "No hay errores.\n" << endl;
system("pause");
#endif

cout << "Intentando verificar cantidad de dispositivos\nVID: 0925\nPID: 1231" << endl;
int tmp = USB_GetDeviceCount(VID_PID);
cout << "Cantidad de dispositivos: " << tmp << "\n" << endl;

system("pause");
#ifdef VER-ERR
cout << "Verificando ultimo error..." << endl;
err = GetLastError();
if(err != NULL)
cout << "Error. Codigo: " << err << endl;
else
cout << "No hay errores.\n" << endl;
#endif
cout << "Intentando abrir el dispositivo..." << endl;
AbrirPipes();


//*********FINALIZAR************
system("pause");
cout << "Liberando DLL..." << endl;
::FreeLibrary(hDLL);
err = GetLastError();
if(err != 0)
cout << "Error al liberar la DLL. Codigo de error: " << err << endl;
else
cout << "Exito.\n" << endl;

system("pause");
return 0;

}

void ConvertirVersion(DWORD VerGET, BYTE* VerPARSE){

memset(VerPARSE, 0, 4);
VerPARSE[1] = static_cast<BYTE>(VerGET >> 24);
VerPARSE[0] = static_cast<BYTE>(VerGET >> 16);
VerPARSE[3] = static_cast<BYTE>(VerGET >> 8);
VerPARSE[2] = static_cast<BYTE>(VerGET);
}

void AbrirPipes(void){

DWORD selection = 0;
    hUSB_DeviceOut = USB_Open(0, VID_PID, out_pipe, 0, 0);
    hUSB_DeviceInp = USB_Open(0, VID_PID, inp_pipe, 1, 0);
if(hUSB_DeviceOut == INVALID_HANDLE_VALUE || hUSB_DeviceOut == INVALID_HANDLE_VALUE)
cout << "Error al iniciar la conexion\n" << endl;
}

void CerrarPipes(void){

USB_Close(hUSB_DeviceOut);
USB_Close(hUSB_DeviceInp);

}

Por si se preguntan, el código lo escribí yo, así que es probable que contenga errores, dado que recién estoy empezando en C++.
El circuito del PIC no es mio, ni su firmware, pero sé que es totalmente funcional dado que con su propio software (del que también tengo el código que está en C#; de ahí tome algunos de los datos para el código en C++), funciona perfectamente, y usé la misma DLL.

Bueno, gracias por tomarse la molestia de leer esto, y espero puedan resolver mis dudas.
Atte. se despide, Mr. Hell.