Shop OBEX P1 Docs P2 Docs Learn Events
I need help making this Communication buffer work — Parallax Forums

I need help making this Communication buffer work

BitsBits Posts: 414
edited 2012-11-21 13:32 in Propeller 1
I have a cog that runs "fullduplexserial" and packs a buffer made up of 255 bytes. I have a problem unpacking the buffer and wanted a sugjestion doing so.

Here is my code that runs 2 cogs. Its function is to monitor the USB port looking for a byte that is within the start and stop range ($20 to $5D). Once it sees this byte is true it begins packing the buffer.


Here is a picture of packing the buffers.
[code]

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐
│S││X││X││X││X││S││S││X││X││X││S││S││X││X││X││X││X││X││X││S││ │
└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘
S = START AND STOP BIT
X = NUMBER OR SYMBOL
3 commands are in the buffer, each could different lengths and values



Now I need to figure our a way to run another cog and look at this buffer to see if valid commands are contained within it.
I am at a loss of how to unpack the buffer. I thought about byte shifting but that needs a lot of overhead.

Suggestions?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-21 10:35
    This would work the same as any other buffer in / out scheme. You need one pointer to tell you where to put data into the buffer ... with provisions to wrap the pointer value at the end of the buffer. You need another pointer to tell you where the next character is coming from. You also need a way to tell when the buffer is empty. Typically, when the buffer is full you overwrite the last value in the buffer (or throw away the new value ... both work). Your input routine is fine the way it is except that you need another pointer (say "readPointer") also initialized to zero. Your read routine would look like:
    pub Get_A_Byte : bite
       if readPointer == buffercount   ' any characters in the buffer?
          return -1   ' if not, return -1 to indicate buffer is empty
       bite := buffer[readPointer++]  ' get character in buffer
       if readPointer == 254
          readPointer := 0
    
    The problem with this is that there's no way to tell that the input buffer is already full. You need to add the following to your buffer writing routine:
    if ((bufferCount + 1) // 254) == readPointer
       ' the buffer is full, and you're trying to add another byte to it.  Do something
    
    Alternatively, you can also keep a byte count of the number of bytes stored in the buffer. Increment it when you put something in and decrement it when you take something out. If it's zero, the buffer is empty. If it's equal to 254 (or whatever your buffer size is), the buffer is full.
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-11-21 11:05
    Is it important to have a buffer size of 255 or could it also be 256?
    256 just gives you the rollover for free because a byte variable would automatically go back to 0 when adding 1 to 255.
  • BitsBits Posts: 414
    edited 2012-11-21 13:32
    Thanks a bunch guys. I got it working better than I imagined. I used a little of Mike G's advice as well as MagIO2 to get it running.
Sign In or Register to comment.