10-18-2017 09:08 AM
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
10-18-2017 06:12 PM - edited 10-18-2017 06:15 PM
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.