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

Send 32bit float via RS232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

Send 32bit float via RS232
PostPosted: Thu May 19, 2005 8:55 am     Reply with quote

How can i send a 4 byte float via RS232 in CCS - C ?

edit

How can I send the char \NUL (decimal 0) via RS232?

printf(%u,0) sends decimal 48, I want decimal 0 te be sent..
Ttelmah
Guest







PostPosted: Thu May 19, 2005 9:50 am     Reply with quote

printf("%c",0);

or

putc(0);

Probably the easiest way to send numbers (assuming you want reasonably easy interpretation), is to use text, and send a limited size 'representation', with perhaps a comma delimiter.
You can of course just send the four bytes that represent the number, but if so, assuming you are talking to a PC, there is qute a bit of 'conversion' involved, because the CCS representation does not match the IEEE representation used on machines like this. The following are a pair of routines, which do the conversion for a block of four bytes representing a float, when handed the address of the block.
Code:

void toCCS(int *Value) {
   //program to convert a 4 bytes 'word' from IEEE to CCS format
   int1 sign;
   sign = shift_left(&Value[1],1,0);
   sign = shift_left(&Value[0],1,sign);
   shift_right(&Value[1],1,sign);
}

void toIEEE(int *Value) {
   //complement to the above
   int1 sign;
   sign = shift_left(&Value[1], 1, 0);
   sign = shift_right(&Value[0], 1, sign);
   shift_right(&Value[1], 1, sign);
}


If you have variable like:

union convbuff {
int8 b[4];
float fp;
} value;

You can then use:

value.fp=1000.0;

toIEEE(&value.b[0]);

Then simply send the four bytes over the serial link to a PC, and place them into a similar union (using a single precision float), at the othr end, and the value will be correct.
Similarly going the other way, you can send the bytes from the union, and put them into the individual b[0]....b[3] locations, then use:

toCCS((&value.b[0]);

to have value.fp become the same number as the PC is sending.

Best Wishes
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Fri May 20, 2005 12:41 am     Reply with quote

How can I send a float?
float f;
putc(f); // sends only 1 byte
Guest








PostPosted: Fri May 20, 2005 6:13 am     Reply with quote

You can't.
Since RS232 only transfers one byte at a time, you either have to send it in ASCII as text, or send the four bytes one after the other. This is exactly the same as in any other 'C'.
So:
Code:

union {
   float f;
   int8 b[4];
} value;
int8 n;

value.f=1000.0;
for(n=0;n<4;n++)
   putc(value.b[n];

//sends the four bytes

printf("%f",value.f);

//sends the text


Obviously this needs a suitable 'wrapper' to setup th RS232 etc..

Best Wishes
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Mon May 23, 2005 2:17 am     Reply with quote

I get a compiler error; ' a numeric expression must appear here' (points to union)

Code:
union {
            float f;
            int8 b[4];
         } Value;
         value.f=Vbat;

         printf("BATT%i",floatgrootte);
         for(int8 n=0 ; n<4 ; n++)
            putc(value.b[n]);
Ttelmah
Guest







PostPosted: Mon May 23, 2005 4:08 am     Reply with quote

The union has to be declared at the start of the function (or as a global). Then the code itself sits further down in the function. There are two seperate 'parts' to the code here, one showing how to declare the union, and the other how to use it, they do not go together as one 'lump'...

Best Wishes
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Mon May 23, 2005 4:15 am     Reply with quote

Yes,

that was it, all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?

How can I convert the 4 sent bytes back to a float?
Ttelmah
Guest







PostPosted: Mon May 23, 2005 7:28 am     Reply with quote

Christophe wrote:
Yes,

that was it, all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?

How can I convert the 4 sent bytes back to a float?

The requirement to declare the variables, is standard C.
You can declare the variable for 'internal' use, by creating a command 'block' (with '{}' brackets round it), and then declaring variables inside this. This is accepted by standard C, but I would not trust this on the CCS C, since the way that local variables are generated, is by appending the variable name to the function name, and a bracketted block like this, does not have a distinct name...
You just rebuild the variable the same way. Declare a similar union at the other end, and then write the bytes into this, and look at the float. However see the caveat I posted about this (and the functions called toccs, and toIEEE), in a thread a few days ago, since the floating point format in CCS, is not the IEEE format used in the PC.

Best Wishes
bfemmel



Joined: 18 Jul 2004
Posts: 40
Location: San Carlos, CA.

View user's profile Send private message Visit poster's website

Decalring C Variables
PostPosted: Mon May 23, 2005 9:45 am     Reply with quote

Quote:
all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?

Different compilers have different rules. While all require you to declare the variable before you use it, they differ in how restrictive they are in where you do that. Some compilers will not accept a decalration after any C program statements inside a function while some will let you declare them in any place as long as it is before you use them. From both the standpoint of good practices and to standardize on one way which will work all the time, you should get in the habit of always declaring variables at the front of a function.

- Bruce
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