Shop OBEX P1 Docs P2 Docs Learn Events
Building A Packet — Parallax Forums

Building A Packet

computer guycomputer guy Posts: 1,113
edited 2012-08-10 09:35 in Propeller 1
I am waiting for a CC-Debugger to program a 8051 MCU, to get a Bluetooth Module working.

In the meantime as I am impatient, I have proceeded to try and write a driver object for the module based on the documentation and wanted to see if I am doing it right. (As I am new to byte manipulation).
'Packet Structure

'  +------------+---------+--------+--------+--------+----------+------------+
'  |      0     |   0000  |   000  |00000000|00000000| 00000000 |00000000 ...|
'  +------------+---------+--------+--------+--------+----------+------------+
'  |Message Type|Tech Type|Len High|Len Low |Class ID|Command ID|  Payload   |
'  +------------+---------+--------+--------+--------+----------+------------+
'  |                            Header                          |  Payload   |
'  +------------------------------------------------------------+------------+
 ' BUILD HEADER
  txHeader[0] := messageType
  txHeader[0] |= techType
  txHeader[0] |= len << 8
  txHeader[1] := len
  txHeader[2] := class
  txHeader[3] := cmd

The following code is supposed to build the header section of the packet.

Where message type is one of either:
messageTypeCommand := %00000000
messageTypeEvent   := %10000000

techType is:
techTypeBT4S := %00000000

and len is the length of the payload.

It's just the first byte im not sure about. The ORing of the values together and shifting of the length.

Does it look right?

Comments

  • ericballericball Posts: 774
    edited 2012-08-10 08:33
    txHeader[0] |= len >> 8
    txHeader[1] := len & 255
    
    You could also have
    txHeader[0] := messageType << 7
    txHeader[1] := techType << 3
    
    in which case messageType is 0 or 1 and techType is 0 to 15.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-10 09:35
    If "txHeader" is an array of bytes, I think you could simplify:
    txHeader[1] := len & 255
    

    to:
    txHeader[1] := len
    

    Since only the least significant byte would be used from "len" anyway.

    (Not that it matters much.)

    Edit: I see this is the way Computer Guy originally had it.
Sign In or Register to comment.