View previous topic :: View next topic |
Author |
Message |
philipp2084
Joined: 06 Aug 2007 Posts: 5
|
Transmitting bytes via USART |
Posted: Wed Aug 15, 2007 3:54 am |
|
|
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
|
|
Posted: Wed Aug 15, 2007 4:08 am |
|
|
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
|
|
Posted: Wed Aug 15, 2007 7:14 am |
|
|
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
|
|
Posted: Sat Aug 25, 2007 11:36 am |
|
|
Cheers guys, that got the job done |
|
|
|