<br><br><div class="gmail_quote">On Sun, Jan 25, 2009 at 7:27 AM, Ravi <span dir="ltr"><<a href="mailto:ra.ravi.rav@gmail.com">ra.ravi.rav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>
> Take a look at the struct and ctypes modules.<br>
<br>
</div>struct is really not the choice. it returns an expanded string of the<br>
data and this means larger latency over bluetooth.</blockquote><div><br>Noo... struct really IS the choice; that is the explicit purpose of the struct library. I went and included an example too which you're not noticing :)  <br>
</div></div><br>Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) <br>[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> import struct<br>>>> data = struct.pack("BB4s", 1, 4, "this")<br>>>> data<br>'\x01\x04this'<br>>>> <br><br>That's precisely six raw bytes which is exactly what you specified you needed: exactly one unsigned byte for packet type, one unsigned byte for length, and four bytes for the data after. In real life you'd probably use something besides "4s" to pack what you marked as "other" and "variable", of course, but still. Replace it with whatever your message requires.<br>
<br>Really, this is how you do line protocols ;) Well there's other ways, but...<br><br>--S<br>