LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Assembling a packet from a mixture of chars arrays and an int using Fmt

Hi,

I need to assemble a packet containing 2 qualifier bytes, 2 length bytes big endian, and variable length command bytes.

I can do this in one line and this is what I have come up with:

 

int Send((unsigned char *command,int port,int length) 

unsigned char qualifiers[] = {0xAA,0x55,0}; 

unsigned char packet[0x10005];

//length is length of command array and stored as little endian on my machine.

Fmt(packet,"%s%c[b2o1]%c%s[t-w*]",qualifiers,length,length,length,command);

 

To get the 2 length bytes in place with high order byte first i'm using the byte order modifier. Since i'm printing to a string I need to use %c because I don't want the int length converted to ascii. But i need length to appear twice as an argument to fill in the 2 length bytes in my packet.

Is there anyway to get the length in the packet correctly with 1 less length argument?

 

Thank you

0 Kudos
Message 1 of 3
(3,029 Views)

Hi lupy 87,

 

May I ask why you want to remove a length argument? Maybe I'm misunderstanding the question.

 

Regards,

B. Poteet

National Instruments

0 Kudos
Message 2 of 3
(2,982 Views)

Hi B.Poteet,

 

No reason other than to shorten the line. It just seems a bit of a waste to repeat the same argument 3 times. It would be good if each format specifier could reference an argument of your choice rather then just going by the order in which they appear.

 

I don't think that is possible so I was looking at the int conversion. I came up with this but its flawed and format string is a lot longer instead!:

 

Fmt(packet,"%100i[b2uz]<%1i%i[b2o10]%*i[b2]",qualifier,length,length/2+1,command);

0 Kudos
Message 3 of 3
(2,977 Views)