LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ViStatus _VI_FUNC AvtAldlk_Read (ViSession vi,ViUInt8 buf[],ViUInt32 bufSize,ViUInt32 *numBytesRead,ViReal64 *timeStamp)

I built a IVI driver used for R232 communication, the read function is like below:
ViStatus _VI_FUNC AvtAldlk_Read (ViSession vi,ViUInt8 buf[],ViUInt32 bufSize,ViUInt32 *numBytesRead,ViReal64 *timeStamp)
{
}
//ViUInt8 buf[] is to store the buffer data.
Then value of "buf" will be transfered to a Teststand arrary of number.During the communication, I found the value will be like:11 14 ffffff4. But the correct value should be like "11 14 f4", how "ffffff4" comes here?

Thanks!
Jacky
0 Kudos
Message 1 of 4
(4,051 Views)
Somewhere an 8 bit value is copied to a signed 32 bit value.  Through "sign extension" the high bit of values above 0x7F is extended into the upper bits of the variable.  If this value is then cast to an unsigned value 0x8F will look like 0xFFFFFF8F.  Without seeing more of the code, I could not guess where that would be occurring.

Message Edited by mvr on 04-12-2006 10:34 AM

0 Kudos
Message 2 of 4
(4,044 Views)
When I create a number of array, how can I restrict it to unsigned int8? If there's such a function, then I can resolve the problem above.
 
Thanks!
Jacky
0 Kudos
Message 3 of 4
(4,030 Views)

Hello,

 

I don’t think you can really “restrict” the datatype of an array, since it can’t change anyway.  I don’t know exactly how the conversion from the “U8” takes place, but with 8-bit characters the problem comes from come from some typecast/conversion from your unsigned to a signed char.  If you go from an unsigned char for example to a signed int, the sign is not extended:

 

#include <utility.h>

static unsigned char a = 255;

DebugPrintf ("%i", ((int) a));

 

If, however, you cast a unsigned char to a signed char and then cast that signed char to a signed int, it will be treated as a (-) number and converted:

 

#include <utility.h>

static unsigned char a = 255;

static char z;

z = (char) a;

DebugPrintf ("%i", ((int) z));

 

The solution is to find where that conversion takes place, and eliminate/change it.  You could also modify your program to use a different type of array from the start to eliminate the conversions.

 

Hope it helps-

Travis M
LabVIEW R&D
National Instruments
0 Kudos
Message 4 of 4
(4,014 Views)