LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to call FT4222 SPI Master SingleReadWrite functions

I need help uderstanding how to use ReadWrite functions from LibFT4222.dll. Lets say we have function :

 

FT4222 SPI Master Single Read Write

 

In the documentation the function is defined as:

 

AndrazS_0-1641386773013.png

 

Where function parameters are:

AndrazS_1-1641386813086.png

I have succesfully wrapped function according to documentation and i have defined it as:

AndrazS_3-1641386958665.png

 

The misunderstanding is in readBuffer and writeBuffer. We can se how I defined them here:

 

AndrazS_2-1641386907699.png

 

It is, Array Data Pointer. According to documentation, calling function is for me very challenging becouse buffers are indexed when function is called. Se this:

AndrazS_4-1641387110319.png

I have also found some implementations of calling this function using Delphi programming language and it has the same confusing calls of functions. Se this:

AndrazS_5-1641387251370.png

 

 

My question is: How can this function be programmed in labview? What does pointer indexing mean, is there any labview example anywhere?

0 Kudos
Message 1 of 7
(2,507 Views)

That doesn't look like you defined the VI but instead took it directly from the library that the Import Library Wizard created!

And again I have to say despite the name it is not a magicien. It can not know how to deal with those two buffers nor what size they need to be nor that the adjacent parameter is meant to tell the function how big the buffer is!

 

So what should you have done instead?

 

Create a byte array on the front panel as write input. Wire this to the writeBuffer. Connect an Array Length to this array wire and wire its output to the sizeToTransfer input.

 

Create an Initialize Array on the diagram, connect the array length output to the size input of the Initialize Array, a Byte constant to the type input. Wire the resulting array to the readBuffer input.

 

Wire the output of the sizeTransfered to the length input of an Array Subset function and the output of the readBuffer parameter to the array input of this function, to make sure the array is resized to the actually received data. Create a byte array indicator on the front panel and connect it to the output of the Array Subset function.

 

Do proper error handling: This function has an error return value that will tell you if something went wrong!

 

And that handle is almost certainly not an UINT32 but a pointer sized variable. So configure it accordingly as an Pointer Sized Integer and make the controls on the front panel an UINT64.

 

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 7
(2,482 Views)

Thanks for info. According to your response is this vi ok now?

 

AndrazS_0-1641409729888.png

 

Still cannot understand similarity of delphi and C implementation that I posted before...

0 Kudos
Message 3 of 7
(2,474 Views)

I also have one additional question. Should output of Array Size be connected to "sizeToTransfer" of to "sizeTransferred" ??

 

Documentation say:

sizeToTransfer The size of read and write buffer. They must be the same.
sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes read
and written to the device

 

It is a little confusing since buffers can be different from call to call...

0 Kudos
Message 4 of 7
(2,456 Views)

You got it wrong.

 

sizeToTransfer tells the function both how many bytes are in the writeBuffer as well as how large the readBuffer is that the function may write data into. And yes, this function REQUIRES that the buffer for the readBuffer is allocated before you call it, as with most C functions that expect buffers in which they can write into.

 

The sizeTransferred tells you AFTER the call, how many bytes effectively were written into the readBuffer. Usually this is the same as what the sizeToTransfer is as SPI does both read and write in parallel. But if there was an error somewhere, it could be less or even 0.

 

If you can't understand the Delphi or C code, it won't be useful in any way to understand what you need to do in LabVIEW. In fact it is my strong opinion that if you can't create the code in C to call a specific function, you should not try to do it in LabVIEW instead. Much of the fundamental programming is the same, but DLLs come usually with C header files and you have to go from there to understand how a function call must be modeled. In addition you have to read the according documentation to understand what the different parameters effectively do, such as you can see with above two parameters. And then you need of course to understand that a LabVIEW programmer is not dealing with an array and a seperate array length parameter as that is an inherent attribute of a LabVIEW array. This is why your original approach was bad! You do not want the user of your LabVIEW VIs to worry about such details but simply let him give the array to your function and you translate it to the C function interface of your function.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 7
(2,428 Views)

Thank you for the instructions and I think I got it now. Now my code in LV looks like this:

AndrazS_0-1641554813233.png

 

Is this better now?

 

0 Kudos
Message 6 of 7
(2,408 Views)

That looks about right if you only plan to ever work in LabVIEW 32-bit. Otherwise you should make the ftHandle control on the front panel to be an U64 and configure the FTHandle parameter in the Call Library Node to be a pointer sized (unsigned) integer. That way it will work for both 32-bit and 64-bit LabVIEW (provided you use the according DLL of course).

 

And yes there WILL be a time when LabVIEW will not support 32-bit anymore. On the Mac and Linux it is only 64-bit for several versions already!

Rolf Kalbermatter
My Blog
0 Kudos
Message 7 of 7
(2,406 Views)