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

Transmitting bytes via USART

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



Joined: 06 Aug 2007
Posts: 5

View user's profile Send private message MSN Messenger

Transmitting bytes via USART
PostPosted: Wed Aug 15, 2007 3:54 am     Reply with quote

Hi everyone,

I am having some problems with the communication via RS232 / RS485. My hardware is all working and i can transmit ASCII charaters using the fgets() and fprintf() in either direction.

However I would like to reduce the amount of data being sent on the bus and rather than transmitting a number like 254 as 3 ASCII characters, i.e. '2' '5' '4' (so 3 bytes) I would like to transmit it in one byte, i.e. 0xFE (in hex) or 11111110 (in binary).

Is there any way of doing this? I have the variables I would like to transmitt saved in an array of the type int8 and want to transmit each element of that array as 1 byte.


Cheers
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Wed Aug 15, 2007 4:08 am     Reply with quote

Hello,

I think that if you are shure that the 3 elements are not representing a number greater than 255 then it is as simple as that :
Code:

  int8 toSend ;
  ...
  toSend =  ( array[0] * 100 ) +  ( array[1] * 10 ) + array[2]  ;
  fputc (  toSend ) ;
  ...
 


dro.
_________________
in médio virtus


Last edited by inservi on Wed Aug 15, 2007 7:29 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Wed Aug 15, 2007 7:14 am     Reply with quote

Code:

//Start with a variable
int8 val;

//set it to a number
val=254;

//Now to send it as text
printf("%d",val);
//Sends '2', '5', '4'

//Now to send the single character with this ASCII value
putc(val);
//Sends _one_ character, with the value '254' in decimal.
printf("%c",val);
//does the same

//Now to send as hex
printf("%2X",val);
//sends 'F', 'E'

Now, remember the caveats. You need to be using 8bit transmission to handle a value this large. You also may need to beware, if the program at the other end, may treat control codes (13, 10 etc) specially.

Best Wishes
philipp2084



Joined: 06 Aug 2007
Posts: 5

View user's profile Send private message MSN Messenger

PostPosted: Sat Aug 25, 2007 11:36 am     Reply with quote

Very Happy
Cheers guys, that got the job done
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