From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need a little help with read/write String from DLL

Solved!
Go to solution

I have a DLL that communicates over RS232 with some instruments.

By passing 2 numbers (parameter number and a process number) the DLL will communicate and fetch the values.

 

I have succesfully set up the DLL to get words, characters, longs and floats (single precision).

 

Now, this is the documentation that came with it for strings:

readstring.png

 

 

ReadString asks the DLL to get the data from the instrument, if succesfull I further on call GetString to get the string from memory.

 

I have some questions:

-The starred values (*) are pointers; as I understand it points to a spot in memory where the value is stored, is this correct?

-How do I find out whether I need to pass the value to the DLL, or read it? For example with GetString Im not sure whether

I should pass the string length to the DLL or if it is read

-How to get the returned character into a string; Im assuming I have to get a byte array from somewhere.

 

Now I can also write strings:

writestring.png

 

Also a f question:

-Should I add NULL at the end of the string, or does labview do this by default?

My procedure is now: reading the string input, reduce it to specified string size (this is  constant depending on the instrument parameter), convert to byte array and replace last element in array with 0 (Null).

However, the input from the DLL expects a character, what should I pass into the DLL?

 

 

 

0 Kudos
Message 1 of 2
(2,437 Views)
Solution
Accepted by topic author _Faust

 


_Faust wrote: 

I have some questions:

-The starred values (*) are pointers; as I understand it points to a spot in memory where the value is stored, is this correct?

 


 

They can be pointers or buffers. Which one of the two is ambiguous in C syntax. However in your case I would assume piMsgId and pcLen to be a pointer to a single value and pszDest a string buffer.

 


 

-How do I find out whether I need to pass the value to the DLL, or read it? For example with GetString Im not sure whether

I should pass the string length to the DLL or if it is read

 


 

The description is not really clear however most C APIs nowadays require the length of the provided allocated buffer on entry to make sure they don't overrun the buffer length. They may or may not modify that value before returning to indicate how much data they actually filled in. The pcLen parameter being passed by reference most likely indicates that this function does it in this way.

 


 

-How to get the returned character into a string; Im assuming I have to get a byte array from somewhere.

 


 

That is not necessary. You can simply configure the parameter to be a string and to be passed as C string pointer. In LabVIEW 8.5 and up you can also configure to resize that string to the number of characters as indicated by a different Call Library Node (CLN) parameter, in this case your pcLen. Before 8.5 you had to use an Initialize Array function to explicitedly create a Bbyte array of the necessary length, convert it with Byte Array to String and pass it to the CLN Parameter. LabVIEW will automatically only return the part of the string up to the NULL character if a CLN parameter is configured as C string pointer

 


Now I can also write strings:

 

Also a f question:

-Should I add NULL at the end of the string, or does labview do this by default?

 


 

No! If you configure the parameter to be a string, passed as C string pointer, LabVIEW will do that automatically for you. 

 


My procedure is now: reading the string input, reduce it to specified string size (this is  constant depending on the instrument parameter), convert to byte array and replace last element in array with 0 (Null).

 

However, the input from the DLL expects a character, what should I pass into the DLL?


I'm not sure what you mean with this. pszBuf is not a character but a character buffer. Configure it as a string passed as C String pointer and all is fine.

 

Rolf Kalbermatter
My Blog
Message 2 of 2
(2,425 Views)