Shop OBEX P1 Docs P2 Docs Learn Events
What is the best way to watchdog serial input? — Parallax Forums

What is the best way to watchdog serial input?

sccoupesccoupe Posts: 118
edited 2013-12-23 10:11 in Propeller 1
I'm trying to figure the best clean way to watchdog serial input between characters. Take the following code for example...
    x := FDS.RxCheck
    if x == $23
      x := FDS.rx
      y := FDS.rx
      z := FDS.rx


If a $23 doesnt come in then the code just moves on. If a $23 does come in, then I want to grab the next 3 bytes. If only two come in, then its stuck waiting for the last byte that never shows up. Whats the best way to get out of that and just move on?

Thanks

Comments

  • T ChapT Chap Posts: 4,223
    edited 2013-12-23 06:39
    I like to park a serial read code at the top of the code with a ser.rx so that it waits for the first byte of a transmission. After the first byte, have a repeat that uses ser.rxtime and you specify how long the wait period is for that read, make it short as possible typically so that if there is nothing received, it does not stick. Repeat the length that you expect your string + any termination might be. After the read, simply parse each element of the array for your packet header, and then you can easily access the following elements to find what you need.

         SerialReadBuffer[0] := ser.rx     'waits here for first byte  (1F) from master
         Repeat 6                    'look for more, else return
           SerialReadBuffer[ii] := ser.rxtime(2) 
    

    You also add the option to only read further if you see the packet header.
         IF  ser.rx  == $1F
         Repeat 3                   'look for more, else return
           SerialReadBuffer[ii] := ser.rxtime(2) 
         ParseSerialReadBuffer
    

    When watching for some external device to send data, you can't always be certain that the complete packet will be received. The other device could malfunction, you can have a bad connection, etc. Having the code only stick at the top of the code makes sure that no matter what you receive, you cannot stick somewhere that will screw with your code if something doesn't go as planned.
  • sccoupesccoupe Posts: 118
    edited 2013-12-23 07:12
    Ahh yes! I forgot about the rx.time.

    Thanks!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-12-23 10:11
    One of my motivations to modifying Tim Moore's four port serial object was a hope I could use the rx buffers to hold an entire message without having to continually move the characters from one buffer to another.

    I modified the driver to watch of an end of message character and once the character was received, a message counter was incremented. Only once a full message was received would it get parsed.

    I thought this approach had some advantages to watching the incoming message one character at a time.

    If your communication protocol includes an end of message character you might want to get the technique some consideration.
Sign In or Register to comment.