CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How Do I get back a sent value using Printf

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sdot.yo.ieee



Joined: 23 Sep 2010
Posts: 1
Location: MN

View user's profile Send private message MSN Messenger

How Do I get back a sent value using Printf
PostPosted: Sat Sep 25, 2010 12:42 am     Reply with quote

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 :
Code:

w=getc();

What is the value of w?? I get confuse, is w in decimal or ascii?
And I think w =x. Obviously.
_________________
souleymane yo
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Sat Sep 25, 2010 1:23 am     Reply with quote

Well it depends on how you set up your RS232. If you want to communicate with 16 bit data you have to add LONG_VAR to your declarations in the "#use_rs232" line.

otherwise I think the default setting is 8 bit data only.
_________________
Vinnie Ryan
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

Re: How Do I get back a sent value using Printf
PostPosted: Sat Sep 25, 2010 3:19 am     Reply with quote

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 :
Code:

w=getc();

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group