LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert C unsigned char to string

Solved!
Go to solution

I'm looking toconvert this C code into LabVIEW.  I'm a bit confused on how to load the SendData into my .so or .dll (for windows).  The CPCI75C3_SER_TransmitBuffer function provided takes in a string. How do I convert this array of unsigned char to a string?

 

 

 

 

   unsigned char SendData[8];

...

      for (i=0; i < 8; i++)
         SendData[i] = (unsigned char)i;           /* incremental data */

      /* Add a delay here for receiver to be ready */
      nai_msDelay(500);

      nNumWordsSend = i;
      printf ("\nSending %d words ...", nNumWordsSend);
      status = CPCI75C3_SER_TransmitBuffer(Card, nMod, nChan, SendData, &nNumWordsSend); /* send data */

0 Kudos
Message 1 of 8
(7,036 Views)

What is the function prototype for the CPCI75C3_SER_TransmitBuffer function?

 

You can build a standard LabVIEW string and configure the Call Library Function Node to pass it as a C string, or you can use the "String to Byte Array" function to convert the LabVIEW string into an array of U8 (unsigned char) and pass that array to the function.  Either should work.  Alternatively, if in your LabVIEW code you already generate an array of U8, then there is no need to do anything - just pass the array directly.

 

If you're still confused, please upload your VI showing how you have configured the Call Library Function Node.

0 Kudos
Message 2 of 8
(7,026 Views)

Here is the function name and variables.  Attached it my VI to handle this transmitbuffer.

 

_75C3FUNC int CPCI75C3_SER_TransmitBuffer(
  int Card,
  int Module,
  int Channel,
  unsigned char* Buffer,
  int* Length
  )

 

My guess is that I can use the byte array to string function.  Will that fit my need?  Is my understanding a unsigned char a unsigned byte (u8)?

0 Kudos
Message 3 of 8
(7,010 Views)
Solution
Accepted by topic author thomassster

Looks to me like the Call Library Function Node is properly configured.

 

EDIT: you updated your post after I replied.  An unsigned char is the same as an unsigned byte (U8) but the way you have it configured now is fine, no need to change it to a byte array unless I've misunderstood what you're asking.

Message 4 of 8
(7,007 Views)

thanks, looks to me I will create my array values and convert them to u8 and then convert them to strings.

0 Kudos
Message 5 of 8
(6,984 Views)

Actually, from the C code in the first post it seems the data are supposed to be truely binary bytes, since the first character is sent as NULL byte. So using a string is not an ideal way. It should work for sending data to the C function, with a minor issue, as LabVIEW will actually append a NULL byte to the end of the string before sending it to the function. But since the data is binary and therefore the function will need to go with the extra length parameter anyhow to know how many bytes are to be transmitted, that extra appended byte is only a cosmetic issue and also causes a minor performance hit, as LabVIEW needs to append the NULL byte first.

 

It would be totally unsuitable for binary buffer parameters that return data from the function. The LabVIEW Call Library Node will scan C string pointers on return and cut them off at the first NULL character. So if your binary buffer contains a NULL byte, LabVIEW will never see anything behind the first NULL byte.

 

The right solution for binary byte buffers to be passed to an external C function is to configure the parameter as array of unsigned 8 bit integers.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 8
(6,975 Views)

Thanks Rolf for pointing out the null byte ending a C string, I had forgotten about that.


@thomassster wrote:

thanks, looks to me I will create my array values and convert them to u8 and then convert them to strings.


If you have an array of u8, pass that.  There is no reason to convert it to a string, as Rolf explained.

0 Kudos
Message 7 of 8
(6,947 Views)

Thanks for this info.

This also helped me solving the problems I was having.

 

________________________________________________________________________
Problems will keep comming... Lets hope the answers do that to.
Never give up without a fight...
0 Kudos
Message 8 of 8
(5,705 Views)