Breaking up a long into eight bytes for serial transmission
kt88seamp
Posts: 112
I have a number 640000, which is a qword if I am not mistaking. It is %10011100010000000000 in bianary. I want to break it up into eight bytes (in an array)·so I can transmit it over a serial connection. At the other end I wish to reassemble it into the qword. How is this done?
Comments
You might re-assemble like this:
In the example here, a used a cog to watch for bytes coming through a serial port. This was only for receiving and I didn't need to send data ack the other way, so this is pretty simple.
I created a state machine that looks for the starting marker then each byte after that had a special meaning. The packet system I created used a simple checksum to make sure the packets were good. If not, it will just wait for the next packet. My system just needed to get the current reading·for several values. If a packet was bad, it wasn't the end of the world. However, it would·accept bad data, just skip it and wait for the next good packet.
Not sure if this is what you're looking for, but it could be a good start to a much better system.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jim Fouch
FOUCH SOFTWARE
Post Edited (Jim Fouch) : 11/26/2009 3:31:34 PM GMT
My computer transmits a long from a C# program to the propeller by breaking up a long into a byte array. The propeller recieves the four bytes and stores it into its own byte array with this code.
I still am trying to figure out how to reassemble the four bytes (in the array) into a long variable.
You could of course do it byte by byte by yourself, then you know in which order you receive it.
Otherwise simply find out in which order you receive it by sending $11223344 instead. Then your little display routine will show you the order.
Either it's MSB first or LSB first. When MSB first you can change the reading-loop by
buffer <<= 8
buffer |= SER.rx & $ff
If it's LSB first you'd do
buffer >>= 8
buffer |= (SER.rx << 24) & $ff00_0000
Remember to set buffer to 0 before starting the loop.
Post Edited (kt88seamp) : 11/26/2009 9:31:25 PM GMT
You don't show what GetBytes is doing ...
But when you wrote that by yourself as well I don't understand why you have to ask. Just do the opposite.
$ff_00_00_00 is the most significant byte of the long
$00_ff_00_00
$00_00_ff_00
$00_00_00_ff is the least significant byte.
So, when you receive the byte simply put it back to the place where it belongs by shift operation.
Post Edited (kt88seamp) : 11/26/2009 9:45:50 PM GMT
Thanks. [noparse]:)[/noparse]