Sending Remote AT via C program

Hello,

I’m trying to send this frame to my PC serial port where my Base Xbee is connected. On a remote Xbee a led is connected on D0. I’d like to use a C program.

The frame is :
// Packet: 7E 00 10 17 4D 00 00 00 00 00 00 00 01 00 01 02 44 30 05 1E
// 7E Correct packet header byte
// 00 10 payload length (decimal 16)
// 17 Packet type: remote AT command with 64-bit destination address
// 4D frame id
// 00 00 00 00 00 00 00 01 64-bit destination address
// 00 01 16-bit destination address
// 02 apply changes immediately
// 44 30 AT command “D0”
// 05 parameter value (“digital output high”)
// 1E checksum - correct

Sending this frame via X-CTU in Hex mode, works fine (I’m able to switch OFF/ON a led on my remote Xbee via my base Xbee, connected to my PC).

I’m trying to move away from X-CTU, and write a small C program to send this frame from my PC, via the serial port, to my base xBee. (Like X-CTU does).

Here is the code :

int main() {
int nId, i;
DWORD nBytes;
unsigned char AT[10];unsigned char Frame[40] = “7E0010174D00000000000000010001024430051E”;

nId=4; // COM4

if( !OpenCOM( nId ) ) return( -1 );

strcpy( AT, "+++" );
WriteCOM( AT, strlen( AT ), &nBytes );

sleep( 1 * 1000 );

strcpy( AT, "ATAP\x0d" );
WriteCOM( AT, strlen( AT ), &nBytes );
sleep( 1 );

WriteCOM( Frame, 40, &nBytes );

CloseCOM();
return 0;

}

OpenCOM : call CreateFile function to open the COM.
WriteCOM : write string, using WriteFile function.

Running this code doesnt’ swith on the led. Nothing happend. Any clue ?

Mersing.

Hey - looks like you’re using my packet-check program!!!

At last! A user! :slight_smile:
My life is now complete :slight_smile:

Enough of that. To turn to your program, I think I can see two problems.

The first is the line:

sleep(1 * 1000);

The sleep function interprets its parameter as the number of seconds to sleep, not milliseconds. So that line makes your program pause for 16 minutes and 40 seconds, and I imagine you control-C it before that amount of time has elapsed.

The second problem is the line

WriteCOM( Frame, 40, &nBytes );

This sends 40 ASCII characters to the XBee. But what the XBee needs is 20 bytes, each of which is described by two of those characters. One possible fix would be to do this:

unsigned char Frame[] = { 0x7e, 0x00, 0x10, 0x17 /etc/ };

and change the other line to

WriteCOM( Frame, 20, &nBytes );

Does that help?

Hello John,

Yeap, I’m using extensively your packet-check program, to first undertand the frame stucture (in link with the Digi Doc), and for checking my frames. Good stuff.

Now back to the issues :

  1. Sleep comment

From #include Sleep( n ); the number is in milliseconds

http://www.cplusplus.com/forum/beginner/3574/
I did wait for 1000s (eg 16 minutes and 40 seconds), neihter control-C to interrupt the execution. The program normally end after 1 s.

Some output :
Before Sleep : Mon Mar 16 10:55:32 2009
Sleep for 1000 * 5
After Sleep : Mon Mar 16 10:55:37 2009

Before Sleep : Mon Mar 16 11:02:11 2009
Sleep for 1000 * 3
After Sleep : Mon Mar 16 11:02:14 2009

  1. Sending the right Frame length
    unsigned char Frame[] = { 0x7e, 0x00, 0x10, 0x17 /etc/ };
    WriteCOM( Frame, 20, &nBytes );
    I tried (again as I initially setup my array with Hex ), and it works :slight_smile:
    One thing to be carefull : not using standard string functions (such as strcpy, etc), as 0x00 is the array will be interpreted as an end of string.
    I guess this was the reason why this did work at the begining.

Mersing.

Hello,

can you send me the link of your serial lib please?

i have to do the same thing but with AT command;

Thank you for help

Hello,

http://files.codes-sources.com/fichier_fullscreen.aspx?id=47035&f=main.c