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

RS232 printf

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
[email protected]



Joined: 16 May 2006
Posts: 39

View user's profile Send private message

RS232 printf
PostPosted: Wed May 17, 2006 6:50 am     Reply with quote

I have problem to send a long integer or float number to a PC through RS232

printf("Output=%ld",123456);

The data received by the Hyperterminal is

Output=-5046

It's fine to send unsigned integer ans string.

Thanks
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

Re: RS232 printf
PostPosted: Wed May 17, 2006 7:13 am     Reply with quote

[email protected] wrote:
I have problem to send a long integer or float number to a PC through RS232

printf("Output=%ld",123456);

The data received by the Hyperterminal is

Output=-5046

It's fine to send unsigned integer ans string.

Thanks


123456L perhaps?
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Wed May 17, 2006 8:02 am     Reply with quote

The problem here, is that the 'L' specifier, has two meanings, according to the 'type' of the value being received. So if you give a 16bit value to it, it uses 16bit arithmetic, while if you give it a 32bit value, it switches 'up' again, and uses 32bit arthmetic. Unfortunately, using a constant (even though too large to fit into an int16), sometimes only results in the int16 behaviour, and you therefore overflow.
Now I am assuming that the code given is only a demo, otherwise there is no point at all in using such a constant inside a printf. What you need to do, is to ensure that the value received by the printf routine, is stored in a signed int32 variable, or cast into such a type.
You should find that:

printf("Output=%ld",(signed int32)(123456));

If you are using a variable to 'feed'the routine, you need to verify that these are signed int32 values, and also whn perfoming earlier arithmetic, be very careful about the type casting. For instance:

Code:

int16 val1, val2;
signed int32 result;

val1=100;
val2=60000;
result=val1*val2;


Will give the _wrong_ answer, since in the actual 'sum', the parts are all int16. You will get (60000*100)%65536....

When doing all such sums, which are not explicitly using the larger data types, you need to ensure that you 'cast' the types up to the form that you want to do the arithmetic in (the '(signed int32)' declaration is a 'cast'.

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