sdot.yo.ieee
Joined: 23 Sep 2010 Posts: 1 Location: MN
|
How Do I get back a sent value using Printf |
Posted: Sat Sep 25, 2010 12:42 am |
|
|
Hi,
I have 2 pic 16 connected with an xbee module for wireless connection purposes.
The transmitter (1st PIC) side:
Code: |
long int x;
x=1201;
printf(x);
|
On the receiving (2nd PIC) pic I have :
What is the value of w?? I get confuse, is w in decimal or ascii?
And I think w =x. Obviously. _________________ souleymane yo |
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
Re: How Do I get back a sent value using Printf |
Posted: Sat Sep 25, 2010 3:19 am |
|
|
sdot.yo.ieee wrote: | Hi,
I have 2 pic 16 connected with an xbee module for wireless connection purposes.
The transmitter (1st PIC) side:
Code: |
long int x;
x=1201;
printf(x);
|
On the receiving (2nd PIC) pic I have :
What is the value of w?? I get confuse, is w in decimal or ascii?
And I think w =x. Obviously. |
Start with x. 1201. Internally _binary_.
Now you print it. How this is sent, depends on the 'format' qualifier you use in the printf. So, if you use:
printf(""%lu",x);
This says print the _long_ variable 'x' in ASCII as an unsigned integer.
So, you will get the text characters '1', '2', '0', & '1' sent one after the other.
At your receive end, you get one character, so you would just get the ASCII '1', and the other characters will get 'lost', unless your code then goes on and retrieves these.
There are a series of problems at this point. The length of the sent text, will change as the number alters. How do you know when you have retrieved it all?. It is also going to take five times as long to send '20001', as it takes to send '1'.
So, you then have the other possibilities:
1) Send the 'raw' binary data. Makes finding the 'length' even harder, but sends the data as fast as possible. Just two bytes needed for the 16bit value.
2) Send a fixed length format.
The second is commonly used. If you send the value in hex, and send four hex characters, you can send every possible 16bit value. Also, since hex is normal text, you can still use perhaps the line feed character as a 'marker' to say 'this is the whole number', and reduce the number of bytes sent.
To send in hex, use:
printf("%x", x);
Add '/r' to the format string to generate a carriage return to mark the end of the text, or use "%04x", to always send four text characters, so you can 'count' to know where you are.
Then on the retrieval end, you have different choices.
'gethex', is a function in input.c, which gets a pair of hex digits and puts these back together to make a byte. With the fixed length transmission, you could call this twice, and re-assemble the 16bit value from these.
Alternatively, you can use gets, of get_string, with a text buffer big enough to hold the sent text, _plus room for a terminator (five characters for a four character hex value, or six for a five character text numeric value). Then use 'atol' to convert the latter back to a number (just add the letter 'x' to the front of the number to handle hex, if using hex transmission).
Best Wishes |
|