Page 1 of 1

problem with usb_write method

Posted: Tue Jun 22, 2010 10:03 am
by happilylazy
I reproduced the example of synchronized program that we get when downloading Win64 USB drivers. But when calling the usb_write method I get error "usb_write for enable handshake trouble 6"
[code]
nStatus = usbwrite(usbHandle, &usbdata[0]);
if (nStatus == FALSE) {
// ERROR
printf ("usb_write for enable handshake trouble %d\n", GetLastError());
return 1;
}
[/code]
[b]
Why is that?[/b]

Full code
[code]
#include <stdio>
#include <stdlib>
#include <time>

#include <windows>
#include <conio>
#include <string>

// DLL function signature
typedef HANDLE (*OPEN_DRIVER_ASYNC)(void);
typedef BOOL (*USB_WRITE)(HANDLE hdevice, PCHAR data);
typedef BOOL (*READ_MULTIPLE_SWEEPS)(HANDLE hdevice,PCHAR data,DWORD nBytesToRead);

OPEN_DRIVER_ASYNC opendriver;
USB_WRITE usbwrite;
READ_MULTIPLE_SWEEPS readmultiplesweeps;

int loadFunctions (HINSTANCE hinstLib) {

// Get function pointer
opendriver = (OPEN_DRIVER_ASYNC)GetProcAddress(hinstLib, "OPEN_DRIVER_ASYNC");
if (opendriver == NULL) {
printf("ERROR: unable to find DLL function OPEN_DRIVER_ASYNC\n");
FreeLibrary(hinstLib);
return -1;
}

// Get function pointer
usbwrite = (USB_WRITE)GetProcAddress(hinstLib, "USB_WRITE");
if (usbwrite == NULL) {
printf("ERROR: unable to find DLL function USB_WRITE\n");
FreeLibrary(hinstLib);
return -1;
}

// Get function pointer
readmultiplesweeps = (READ_MULTIPLE_SWEEPS)GetProcAddress(hinstLib, "READ_MULTIPLE_SWEEPS");
if (readmultiplesweeps == NULL) {
printf("ERROR: unable to find DLL function READ_MULTIPLE_SWEEPS\n");
FreeLibrary(hinstLib);
return -1;
}

return 0;
}

int main (void) {
HANDLE usbHandle;
CHAR usbdata[64];
BOOL nStatus;

HINSTANCE hinstLib = LoadLibrary(TEXT("Labview_dll.dll"));
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
return 1;
}

if(loadFunctions(hinstLib) < 0)
return 1;

// Call function.
usbHandle = opendriver();

int nsecs = 600;
INT_PTR ringBufferSize = 65536;
int plusBytes = 0;

if (plusBytes/4*4 != plusBytes)
{
printf("\n*** +bytes not a multiple of 4! ***\n\n");
return 1;
}

/********************************
* allocate the large ring buffer
* *****************************/

PUCHAR samBuffer;

INT_PTR bytes2Use = ((INT_PTR)ringBufferSize)*512 + (INT_PTR)plusBytes;

printf("ring buffer size = %d\n", bytes2Use);

if ((samBuffer=(PUCHAR)VirtualAlloc(NULL, (SIZE_T)bytes2Use, MEM_COMMIT, PAGE_READWRITE)) == 0)
{
printf("VirtualAlloc returns 0, GetLastError = %d\n", GetLastError());
return 0;
}

/**********************************************
* connect the ring buffer and the usb driver
* *******************************************/

readmultiplesweeps(usbHandle, (PCHAR)samBuffer, bytes2Use);

for (int i=0; i<64; i++)
usbdata[i]=0;
usbdata[0] = (CHAR)(-1);

Sleep(1000); // msec sleep

nStatus = usbwrite(usbHandle, &usbdata[0]);
if (nStatus == FALSE) {
// ERROR
printf ("usb_write for enable handshake trouble %d\n", GetLastError());
return 1;
}

// Unload DLL file
FreeLibrary(hinstLib);

// Display result
printf("The End.\n");

return 0;

}
[/code]

Posted: Tue Jun 22, 2010 12:35 pm
by pmac
Hi,

The error codes are defined here:

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Error 6 is "invalid handle".

In your code you don't check the returned value from the "opendriver" call.
usbHandle may be NULL at this point.

To verify the success of the driver install,
does the Labview_DLL_SyncTest.exe that you downloaded work?

Paul

Posted: Tue Jun 22, 2010 3:41 pm
by happilylazy
The [b]Labview_DLL_SyncTest.exe[/b] works withouth the problem as long as I don't stop it, so I guess I'm good on driver point.
I will try to fix my code a little bit regarding dll inclusion and check this in detail.