LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

.NET Invoke of method with Byte* parameter?

Hi!

 

I'm trying to call a function from LabVIEW in a .NET assembly ("HID class.dll" from Microchip) using the .NET Invoke Node. The signature of the function is:

 

boolean MCHPHIDClass::USBHIDWriteReport(Byte* buffer, UInt32 nBytes)

 

The buffer I'm passing is a 64 element byte array. But I can't figure out how to cast it to the System.Byte* type.

Naively using the To .NET Object helper does not work since the type System.Byte[] is not accepted as a System.Byte*:

pointertobytearray1.PNG

Does anyone of you know of a way to produce the required pointer to byte array within LabVIEW? (I.e. corresponding to &buffer[0] in C#.)

 

I've attached the non-working code above and the DLL file. Any ideas are welcome!

 

Yours,

Mikael Sterner

0 Kudos
Message 1 of 8
(4,467 Views)

After trying all conversion/marshal functions I could find I gave up and wrote a tiny C# wrapper DLL of my own:

 

namespace hidclass_wrapper
{
unsafe public class HIDClassWrapper : HIDClass.MCHPHIDClass
{
public HIDClassWrapper() { }

public static bool USBHIDReadReport(byte[] buffer)
{
fixed (byte* b = &buffer[0]) { return USBHIDReadReport(b); }
}

public static bool USBHIDWriteReport(byte[] buffer, uint nBytes)
{
fixed (byte* b = &buffer[0]) { return USBHIDWriteReport(b, nBytes); }
}
}
}

Then LabVIEW happily accepts the byte array without even needing a conversion object:

pointertobytearray2.PNG

It would still be interesting to know if there is a way to do this natively in LabVIEW?

 

Yours,

Mikael Sterner

 

 

0 Kudos
Message 2 of 8
(4,454 Views)

Hello!

I make hid_wrapper dll. Writing to device seems to be good. But when read error 1172 occur.

Wrapper dll writes data to the memory used by array. May be something with array adressing in labview and in .Net?

LabView 8.6.

0 Kudos
Message 3 of 8
(3,945 Views)

Your code doesn't make much sense. You have not one, but two infinite loops. Infinity fighting infinity. It's like a mirror. Although, the outer loop will run only once because once the inner loop starts, the outer loop waits for the inner loop to end. Which never will. I am assuming the array constant that has two elements is an array of I32 values? Otherwise, the 64 doesn't make much sense. In the inner loop are you passing in an empty array, or an array that has already been allocated?

0 Kudos
Message 4 of 8
(3,936 Views)

Hi, i am newbie to labview, this is my first project. I need to communicate with hid device: writing commands and reading data. Data needs filtration, saving to file and displaying in a plot. The data in array constant must be 8 bit length - so my program has an error here because i leave the default value I32. The data from USBHIDReadreport must be allocated in an array and then saved to file or dispalyed in a plot. Also I now understand that there is no need in the outer loop, because the labview program is cyclic itself.

thanks.

0 Kudos
Message 5 of 8
(3,925 Views)

i change the dll from microchips' hid class.dll to atmels' atusbhid.dll. This dll is a com based dll, so it recognizes with labview CallLibraryFunctionNode. The dll writes data to device with function

BOOLEAN writeData (UCHAR* buffer);

The question is how to setup CallLibraryFunctionNode output parametrs? UCHAR in text base language is STRING in Labview? If so, the problem is that in CallLibraryFunctionNode there is no  pointer to ARRAY OF STRING type. 

The dll reads data with function BOOLEAN ReadData (UCHAR* buffer); And the problem is the same.

Thanks all.

0 Kudos
Message 6 of 8
(3,914 Views)

Yes!!

uchar* buffer - it is simply a string, not an array!!!!! So i try to send to dll a string and it seems to work well -))

0 Kudos
Message 7 of 8
(3,908 Views)

Take a look at my program. How it will be better to organize recieving and sending data? I think that the main troubles are:

1) TWO strings for receiving data- one as input parameter and one as output. Why in LV there is now such an array (or string) that can be writeable and readable simultaneously, as variables in C, Delphi et.c.

2) Without "case" structure for reading data my string rewrites each time the function executes, but maybe there is a better way to update string indicator only when a VALID data arrives.

3) And what is the easiest way to storage data not in visual component (that is displaying in control panel). I want to store data somewhere in memory and then filter it, write to a file, do some other calculations and data conditioning. I dont want my data always be visible to a user. I want to be visible only the ready data (filtered, conditioned and e t.c.)

0 Kudos
Message 8 of 8
(3,907 Views)