View previous topic :: View next topic |
Author |
Message |
sk Guest
|
receiving 32 bit integer through RS232 |
Posted: Tue Feb 08, 2005 6:25 pm |
|
|
Hi everyone,
I would like to know, is there any way to receive 32bit integers through RS232. I used printf to send the 32 bit integer in hexadecimal format. When I tried to use scanf to receive the 32bit integer, the compiler shows error"UNDEFINED IDENTIFIER SCANF". I could not find the scanf statement in the reference manual also. Is there anyway to receive it.
Thanks for any help
sk |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Tue Feb 08, 2005 8:48 pm |
|
|
In my humble opinion, you'd do better to break the integer up into 4 bytes and send it that way. It'll send faster, and be much easier to reconstuct the quantity in the processor. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Feb 08, 2005 9:45 pm |
|
|
For the most part, RS232 is an 8-bit kinda thing unless you start bit banging by hand.
4 bytes is the way to go.
-Ben |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Tue Feb 08, 2005 10:06 pm |
|
|
When you send the int32 with printf, it is sent as 8 ascii characters. These should be received at the other end in a buffer. These characters could then be copied to a string where the first 2 characters are '0', 'x', followed by the 8 ascii characters of the int32, and terminated with a 0 (null or zero). Next, use atoi32() to convert the string to an int32. |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Wed Feb 09, 2005 10:22 am |
|
|
as you've already figured out, you can't send a 32bit value directly -- the value has to be parsed into 8 bit chunks suitable for transmission over RS232. let me give you an analogy... suppose you needed to send an extremely large crate via Fed Ex -- but the 747's that Fed Ex uses can only take a crate of such and such a size, which is smaller than your huge crate. so you need to break your big crate into several pieces that do fit on the 747, transport them via multiple Fed Ex planes, and then reassemble them on the other side once all the planes are in.
algorithmically in pseudo-code, the sender end could look like
Code: |
int32 someval
int8 i, temp
someval=MEASUREMENT_32BIT
printf("send 32bit value=%d\n\r",someval)
for i = 0 to 3, i++ {
temp=int(someval>>(8*i)) // send LSB first
fprintf(stream_RS232,temp)
}
on the receiving end, you invert the process and rebuild the int32
int32 someval=0
int8 i, temp
for i = 3 to 0, i-- {
getchar(temp)
someval=(someval<<(8*i) & temp)
}
printf("recd 32bit value=%d\n\r",someval)
|
jds-pic |
|
|
|