LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Hexadecimal Pattern to RS232 port in CVI

Hello,

 

I have a task of performing a loopback test between 2 serial ports. The test plan says I need to send a 1024 bytes AA55 pattern from one RS232 port and read it back from the 2nd port.

 

I would like to know what's the best way to use the CVI RS-232 library functions to do my job here.

I've read something here before suggesting that I can convert an AA55 to bytes as below and send it through ComWrtByte as below:

 

char send_byte[100];
int send_byte_converted_to_int;
Scan(send_byte,"%s>%x",&send_byte_converted_to_int);
ComWrtByte (comport, send_byte_converted_to_int);

 

If I do a ComRdByte at the other port, how shall I convert the byte integer back to AA55?

 

Your help is appreciated. Thanks

 

Will

 

0 Kudos
Message 1 of 2
(2,217 Views)

Considering that 0xAA is an all-even-bits-set pattern and 0x55 is the symmetric all-odd-bits-set pattern I suppose you must transmit those two bytes repeatedly. You can do this:

char msg[8];
unsigned char out[8];

strcpy (msg, "AA55");
sscanf (msg, "%2x%2x", (int *)&out[0], (int *)&out[1]);
for (i = 0; i < 512; i++)
    ComWrt (comPort, out, 2);

 

On the receiver part, you can simply read every 2 bytes and cast the resulting string into a short formatting it to hex in output:

unsigned short  ux;
char string[8];

ComRd (comPort, string, 2); ux = ToBigEndian16 (*(unsigned short *)string);
DebugPrintf ("Output = 0x%x\n", ux);

 

ToBigEndian16 function is part of the Programmer's toolbox and serves to rebuild the correct byte sequence when transiting from a short in memory.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(2,187 Views)