Shop OBEX P1 Docs P2 Docs Learn Events
Communication Protocol - Help! — Parallax Forums

Communication Protocol - Help!

computer guycomputer guy Posts: 1,113
edited 2010-04-23 00:15 in Propeller 1
Hi Everyone,

I have a serial device connected to my propeller (Easy Bluetooth Module). I have started writing an object to interface with it and have it so that I can issue a command and read the response, however the Easy Bluetooth Module sends a lot of messages to the propeller without the propeller first issuing a command.
i.e. When another bluetooth device connects to the easy bluetooth module, it tells the propeller that there is an incoming connection.

My problem is, how do I get it receiving both messages and reacting acordingly?

Working
Prop------>EB (Set Name "test")
Prop<------EB (Name has been set to "test")




Unknown
Prop<------EB (Incoming connection from F9:62:01:A0:17:00)




I have attached my code for reference.


This is urgent, if anyone can solve this problem for me I will owe them. smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"powered by Propeller" domed stickers $1.50 - Find them here
Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE

Post Edited (computer guy) : 4/19/2010 11:15:20 AM GMT

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-19 11:38
    computer guy: You need to check for characters in the receive buffer before you issue
    BT.Send_Data(string("Test"))
    BT.Read_Real_RSSI

    You can do this by using fdx.rxcheck or making a new fdx.rxavail which does not take the character from the buffer. FDX will be receiving your string and you need to check for it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 11:48
    Cluso,

    How accurate will that be? What if the EB module sends a "incomming conection" message just after you issue a "set name" command?
    Also, I would like the rx command to be in a seperate cog and continuously reading the rx buffer.


    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • kwinnkwinn Posts: 8,697
    edited 2010-04-19 12:12
    @computer guy, ultimately you will have to deal with all the incoming messages, even if dealing with them means ignoring all but the few that are of interest. To do that your receive program will most likely need to know what commands were sent to the BT module.

    Without more information about what you are trying to accomplish it is difficult to make any specific suggestions.
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 12:26
    Hi Kwinn,

    I am wanting to have access to most features of the Easy Bluetooth module.
    At the moment my biggest fear is missing a "Connection droped" message from the EB and still sending data. I will essentially need a "re-connect" function.


    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-19 12:31
    There is only one input buffer. You code must handle each possibility. I expect that if data starts coming in while you are outputting a get message (whatever you called it) you should abort it and handle the message you are receiving first.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 13:00
    Ok, can someone please supply me with a repeat loop that will read in a packet starting with $02 and ending with $03 of unknown length using the full duplex serial object?

    After that, I am hoping the rest will come easily.


    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-19 13:12
    var
    str byte[noparse][[/noparse]32]
     
     
    PRI getstring | i, c
      i:=0
      repeat
        c:=fdx.rx
        if c:=$02
          i:=0
        elseif c:=$03
          str[noparse][[/noparse]i]:=0     'to terminate the string
          i++
        else
          str[noparse][[/noparse]i]:=c     'save the char
          i++
          if i>31
            i:=0        'overrun so what do you want to do?
      
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 13:19
    Thanks Cluso.
    I see one problem though, what if $03 or $02 is in the middle of a packet?



    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • kwinnkwinn Posts: 8,697
    edited 2010-04-19 14:05
    $02 and $03 are ascii control codes. $02 is STX ( Start Transmit ), $03 is ETX ( End Transmit ), so they should never appear in the middle of a packet.
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 14:12
    Ok, Thank you both. Will see how I go. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-04-19 14:31
    Funny you should mention that Cluso. I'm just in the middle of coding up network protocols for the Propeller. $04 is what I chose to end a packet - $01 is Start of Heading and $04 is End of Transmission. Is this better or worse than $02=Start of Text and $03 End of Text

    As for those bytes appearing in the middle of a packet, one would hope not. In my packets, they can as the data bytes are $00 to $FF but the byte after the start byte is the number of bytes in the packet, so if that doesn't point to $04 at the end of the packet then the packet is invalid and is discarded and the code goes back to waiting for a start byte. Is there such a protocol for Bluetooth packets?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller

    Post Edited (Dr_Acula) : 4/19/2010 2:36:09 PM GMT
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-19 14:42
    Is there any reason why this wouldn't work?

    VAR
    long stk[noparse][[/noparse]1024]
    
    Pub Start
    cognew(rx_loop, @stk)
    
    PRI rx_loop | c, i
    
      repeat
        c := serial.rxcheck
        DB.str(string("Looping..."))
        if c == $02
          i := 0
        elseif c == $03
          'read_packet(i)
        else
          read_data[i] := c     'save the char
          i++
          if i > 31
            fault := 1
    [/i]
    



    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE

    Post Edited (computer guy) : 4/19/2010 2:52:22 PM GMT
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-19 14:50
    @Drac: STX/ETX was used in blocks of transmission and the whole transmission was enclosed in SOH/EOT. When embedding of those control characters, or any other for that matter, it was usual to use DLE-EOT to mean a real $04 not an EOT. IIRC character counts did not include the CRCC or the STX/ETX and I am unsure about the counter character itself. AN ACK or NAK were singular transmissions, excluding SYN characters. This is racking the old brain - I did this in the early 80's! Why don't you use ENCDIC?? LOL

    However, you are not doing synchronous transmission so it does not really matter. Could you use the basic Xmodem protocol?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-20 01:27
    Hmmmm.....
    I still can't understand why cognew isn't working. :-(

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-20 02:43
    It started working.
    I have a quick question though.

    With full duplex serial, if I receive $1F how do I then transmit this as "$1F" (string) to the serial line?



    Thank you smile.gif

    edit: I have it almost working now!!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE

    Post Edited (computer guy) : 4/20/2010 3:39:42 AM GMT
  • kwinnkwinn Posts: 8,697
    edited 2010-04-20 13:16
    You can send it by using the "str (stringptr)" public function in FullDuplexSerial.
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-20 13:30
    Thanks kwinn. I ended up using the hex() public function in FullDuplexSerial, once I worked out that it was actually doing what I wanted it to.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-22 08:20
    $02 and $03 are appearing in the "length" section of the packet and are causing problems with the code.

    PRI rx_loop | c, i
    
      repeat
        c := serial.rx
        if c == $02
            i := 0
            read_data[noparse][[/noparse] i] := c
            i++
        elseif c == $03
          read_data[noparse][[/noparse] i] := c
          read_packet(i)
        else
          read_data[noparse][[/noparse] i] := c     'save the char
          i++
          if i > 31
            fault := true
    
    



    Any ideas?


    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE

    Post Edited (computer guy) : 4/22/2010 11:05:07 AM GMT
  • RvnPhnxRvnPhnx Posts: 36
    edited 2010-04-22 10:27
    computer guy said...
    $02 and $03 are appearing in the "length" section of the packet and are causing problems with the code.

    Where's a link to the protocol spec? I'm guessing that the length segment (like a CRC segment) is probably subject to a somewhat different set of rules than packet body text. That is not unusual.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --RvnPhnx
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-22 10:58
    The length segment can contain anything from $00 to $FF (it's the number of bytes contained within the data segment).

    I tried changing:
    if c == $02
    
    



    to

    if c == $02 and read_data[noparse][[/noparse] i] == $03
    
    



    but that just made things worse for unknown reasons.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-22 11:52
    Ok. This works to handle $02 in the middle of the packet, but I am stumped as to how to handle the $03.

    if c == $02 and (read_data[noparse][[/noparse] i] == $03 or i == 0)
    
    




    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-22 11:57
    It should not appear in the middle of the packet unless there are other rules such as a leadin character, or a character count or the frame is fixed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • kwinnkwinn Posts: 8,697
    edited 2010-04-22 12:27
    computer guy, this type of protocol usually breaks the packet into three sections. The header or first part of the packet would start with the start transmission character ($02) and contain information about the packet (length, type, origin, destination, etc, and possibly crc). The second part would be the body or data that is being transmitted. The third part would be the trailer containing the crc, etc. ending with the end transmission character ($03). In some cases the error checking (parity, crc) is included in the header so the trailer may consist of only the end transmission character.

    The usual program sequence would be:

    read a character, compare it to $02, and set the "start of packet" flag if it is $02 - once the flag is set do not check for $02
    read and save/process the header data (length, type, etc.)
    read and save as many characters as specified by length and calculate crc - $00 - $FF are valid characters in this block
    read and save/process the trailer
    read the next character and compare it to $03 - error if not $03
    check received crc (if any) and set error if not matching calculated crc
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-22 12:28
    Hi Cluso,

    Maybe you can make better sense of this and assist me with the packet receiving code.

    attachment.php?attachmentid=69669

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
    755 x 255 - 20K
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-22 12:59
    This is a problem you can easily solve with a state machine. You have several states which means your program will do different things in each state. Some conditions lead to a change in the state.

    A state machine is very simple. In your case you have a repeat loop which reads bytes from the interface. You start with state := 0, which means FIND START DELIMITER.
    It's a good idea to define some constants to make the code more readable:
    FIND_START_DLM = 0
    READ_HEADER = 1
    CHECK_CHECKSUM = 2
    READ_DATA = 3
    FIND_END_DLM = 4

    Now you'd use a case statement which has some code for each of those constants. For example
    repeat
    ' this is inside of a repeat loop which reads the interface
    read_byte:= ' whatever interface call you need to read a byte
    
    case state
     FIND_START_DLM:
       if read_byte == START_DLM
        state:=READ_HEADER
        byte_counter:=0
        checksum:=0
    

     READ_HEADER:
       buffer[noparse][[/noparse] byte_counter++ ]:= read_byte
       checksum:= ' here is your code for the checksum calculation
       if byte_counter==4
         state:=CHECK_CHECKSUM
    

     CHECK_CHECKSUM:
       if checksum<>read_byte
         state:=FIND_START_DLM
       else
         state:=READ_DATA
         data_length := 'here is the code that assembles the data length bytes to one variable value
         byte_counter:=0
    

     READ_DATA:
       buffer2[noparse][[/noparse] byte_counter++ ]:= read_byte
       if byte_counter==data_length
         state:=FIND_END_DLM
    

     FIND_END_DLM:
       if END_DLM == read_byte
         ' the whole packed seems to be ok, so do whatever you want to do with it
       state:=FIND_START_DLM
    



    Post Edited (MagIO2) : 4/22/2010 1:05:44 PM GMT
  • computer guycomputer guy Posts: 1,113
    edited 2010-04-22 13:09
    Thanks MagIO2,

    I think I can put this into practice with a few modifications and filling in the blanks.
    Will give me a much cleaner piece of code and better functionality.

    Thanks smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "powered by Propeller" domed stickers $1.50 - Find them here
    Check out my Design and Technology project for my Higher School Certificate www.ecosureblog.net

    The Sarah Myatt Fund - Help Sarah and Her Family Fight Quadriplegia HERE
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-04-23 00:15
    I see MagIO has helped. I have not looked at his code.

    The length character tells you how long the packet is so the $03 is not a problem because you know what you are looking for. You just have to work through a block receiving the characters into a buffer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
Sign In or Register to comment.