Discussion:
How do you use the MPUSBAPI.dll?
(too old to reply)
mchp1
2008-08-12 13:10:08 UTC
Permalink
For some reason the attached DLL driver (Mpusbapi\Dll\Borland_C\Mpusbapi.dll) seems to crash whenever it is run within LabVIEW.  I have run this dll under visual c++ and it works just fine.  In particular the functions MPUSBRead and MPUSBWrite are generating errors within LabVIEW. The other functions of this DLL such as MPUSBGetDeviceCount and MPUSBGetDllVersion are working correctly.   I am using the "Call Library Function" to access this DLL.  Is there anything special that I have to do to use the functions MPUSBRead and MPUSBWrite?  Any suggestions?  Thanks.  Below is the header for this dll. #ifndef _MPUSBAPI_H_#define _MPUSBAPI_H_#define    MPUSB_FAIL                  0#define MPUSB_SUCCESS               1#define MP_WRITE                    0#define MP_READ                     1// MAX_NUM_MPUSB_DEV is an abstract limitation.// It is very unlikely that a computer system will have more// then 127 USB devices attached to it. (single or multiple USB hosts)#define MAX_NUM_MPUSB_DEV           127extern "C" __declspec(dllexport)DWORD MPUSBGetDLLVersion(void);extern "C" __declspec(dllexport)DWORD MPUSBGetDeviceCount(PCHAR pVID_PID);extern "C" __declspec(dllexport)HANDLE MPUSBOpen(DWORD instance,    // Input                 PCHAR pVID_PID,    // Input                 PCHAR pEP,         // Input                 DWORD dwDir,       // Input                 DWORD dwReserved); // Input <Future Use>extern "C" __declspec(dllexport)DWORD MPUSBRead(HANDLE handle,              // Input                PVOID pData,                // Output                DWORD dwLen,                // Input                PDWORD pLength,             // Output                DWORD dwMilliseconds);      // Inputextern "C" __declspec(dllexport)DWORD MPUSBWrite(HANDLE handle,             // Input                 PVOID pData,               // Input                 DWORD dwLen,               // Input                 PDWORD pLength,        &nbsp
mchp1
2008-08-12 14:40:19 UTC
Permalink
This DLL uses some non standard types.  Any suggestions as to how I can work around this?   Could this be the cause of the errors that I am seeing?
smercurio_fc
2008-08-12 14:40:23 UTC
Permalink
What does you LabVIEW code look like? I'm guessing you're not properly allocating the memory for those pointers.
mchp1
2008-08-12 15:10:08 UTC
Permalink
I've attached the project


Labview PIC32 Driver.zip:
http://forums.ni.com/attachments/ni/170/348627/1/Labview PIC32 Driver.zip
smercurio_fc
2008-08-12 15:10:09 UTC
Permalink
When you use the read function are you pre-allocating the buffer in your calling function, or are you trying to use the read function directly? The latter will probably cause a crash. You need to pre-allocate the buffer using Initialize Array.
mchp1
2008-08-12 15:10:10 UTC
Permalink
I'll try that, but I think it may be a different problem since my write function also is returning a '0' indicating that the write function generated an error during execution.  Thank you.
smercurio_fc
2008-08-12 15:10:11 UTC
Permalink
For the write are you properly setting the value for dwLen, which is the length of the input array in your calling VI? I have no idea what pLength is for since I don't have the documentation for that DLL.
mchp1
2008-08-12 15:10:13 UTC
Permalink
I've attached the functional C code that implements this DLL.  I am making the changes that you suggested. 


Ex2.cpp:
http://forums.ni.com/attachments/ni/170/348641/1/Ex2.cpp
mchp1
2008-08-12 16:10:11 UTC
Permalink
Attached is the DLL's .cpp code.  From the .cpp code:  "//  dwLen   - Specifies the number of bytes to write to the pipe."


_mpusbapi.cpp:
http://forums.ni.com/attachments/ni/170/348692/1/_mpusbapi.cpp
smercurio_fc
2008-08-12 16:40:21 UTC
Permalink
This post might be inappropriate. Click to display it.
mchp1
2008-08-12 17:10:05 UTC
Permalink
I thought I set that...   Thank you.
mchp1
2008-08-12 21:10:06 UTC
Permalink
Ok, I think that the problem may be arising because I am not initializing the buffer that I am feeding the write or the buffer that I am reading from the read function .  How can I do this?  The Write function is expecting an array of type BYTE[64] and the Read function should return an array of type BYTE[64]. extern "C" __declspec(dllexport)DWORD MPUSBRead(HANDLE handle,             
// Input                PVOID pData,                //
Output                DWORD dwLen,                //
Input                PDWORD pLength,             //
Output                DWORD dwMilliseconds);      // Inputextern "C"
__declspec(dllexport)DWORD MPUSBWrite(HANDLE handle,             //
Input                 PVOID pData,               //
Input                 DWORD dwLen,               //
Input                 PDWORD pLength,            //
Output&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD dwMilliseconds);&nbsp;&nbsp;&nbsp;&nbsp; // Input<img src="file:///C:/DOCUME%7E1/GBRACA%7E1/LOCALS%7E1/Temp/moz-screenshot.jpg" alt="">


sh.jpg:
Loading Image...
smercurio_fc
2008-08-12 21:40:19 UTC
Permalink
mchp1 wrote:Ok, I think that the problem may be arising because I am not initializing the buffer that I am feeding the write or the buffer that I am reading from the read function .&nbsp; How can I do this?&nbsp; The Write function is expecting an array of type BYTE[64] and the Read function should return an array of type BYTE[64].Did you read my replies? That's precisely what I said in reply #5. Also, it makes no sense that it requires a fixed byte array of 64 elements. If it did, then the read would not require a separate input of how many elements are in the array. Same goes for the write. In your example C program you were pre-allocating an array of 64 bytes, but tellng the functions that the array length was 4 for the receive and 2 for the send. Made no sense to me, but whatever.To have the read function read a maximum of 64 bytes then you would do something like this:<img src="Loading Image..."> This allocates an array of 64 bytes and tells the function that you are providing an array of 64 bytes (not 4). The function will return how many bytes were read with its output parameters. Note that the output array will still be 64 bytes. You can use Split Array if you wish to chop off the extra zeros.Message Edited by smercurio_fc on 08-12-2008 04:24 PM


MPUSBRead_BD.png:
http://forums.ni.com/attachments/ni/170/348818/1/MPUSBRead_BD.png
mchp1
2008-08-13 14:10:06 UTC
Permalink
Thank you.&nbsp; I found the problem.&nbsp; I was calling the dll through the &quot;Call Library Function&quot; as if it were using the stdcall convention instead of the c convention.
smercurio_fc
2008-08-13 14:10:08 UTC
Permalink
My comments regarding the pre-allocation requirement are still valid since you have to do that.
Loading...