How many bytes in serial background receive buffer ?

hippy

Technical Support
Staff member
This code will show how to calculate how many bytes are in the high-speed serial background receive buffer of a PICAXE X2 which have not yet been extracted from the buffer.

This assumes you are using the 'ptr' variable to track and retrieve bytes from that buffer, such as in the following which simply echoes everything received to the Terminal ...

Code:
HSerSetup ...
ptr = 0
Do
  If hSerPtr <> ptr Then
    SerTxd( @ptrInc )
  End If
Loop
The 'hSerPtr' leads the 'ptr' variable so generally there are hSerPtr-ptr bytes in the receive buffer. However, the 'hSerPtr' may have wrapped past the end of the buffer before 'ptr' has, in which case there will be SIZE_OF_BUFFER-ptr+hSerPtr bytes in the buffer, where 'SIZE_OF_BUFFER' is 128 for a 20X2 and 1024 for a 28X2 or 40X2.

In PICAXE code this could be coded as a subroutine returning bytes in buffer in 'w0' as follows -

Code:
#IfDef 20X2
  Symbol SIZE_OF_BUFFER = 128
#Else
  Symbol SIZE_OF_BUFFER = 1024
#EndIf

BytesInBuffer:
  If hSerPtr >= ptr Then
    w0 = hSerPtr - ptr
  Else
    w0 = SIZE_OF_BUFFER - ptr + hSerPtr
  End If
  Return
The w0=SIZE_OF_BUFFER-ptr+hSerPtr can however be improved on as we know 'SIZE_OF_BUFFER' is a power of two ...

w0 = - ptr + hSerPtr And SIZE_OF_BUFFER_MINUS_1

Which can be rewritten as ...

w0 = hSerPtr - ptr And SIZE_OF_BUFFER_MINUS_1

And because the number of bytes in the buffer is never going to get above 'SIZE_OF_BUFFER_MINUS_1' that also works for when hSerPtr >= ptr, so the simple solution for how many bytes are in the receive buffer is ...

Code:
#IfDef 20X2
  Symbol SIZE_OF_BUFFER = 128
#Else
  Symbol SIZE_OF_BUFFER = 1024
#EndIf

Symbol SIZE_OF_BUFFER_MINUS_1 = SIZE_OF_BUFFER - 1

BytesInBuffer:
  w0 = hSerPtr - ptr And SIZE_OF_BUFFER_MINUS_1
  Return
And the calculation can be in-lined where needed rather than calling the subroutine if desired.

The following example program will echo bytes received, but only eight bytes at a time, and only when all eight bytes of each block have been received ...

Code:
#IfDef 20X2
  Symbol SIZE_OF_BUFFER = 128
#Else
  Symbol SIZE_OF_BUFFER = 1024
#EndIf

Symbol SIZE_OF_BUFFER_MINUS_1 = SIZE_OF_BUFFER - 1

HSerSetup ...
ptr = 0
Do
  w0 = hSerPtr - ptr And SIZE_OF_BUFFER_MINUS_1
  If w0 >= 8 Then
    For b0 = 1 To 8
      SerTxd( @ptrInc )
    Next
    SerTxd( CR, LF )
  End If
Loop
 

jsmanson

Member
Hippy, I am having some difficulty getting the background serial receive working on an older 40x2 (5V) part (4520..) When is use hsersetup B9600_8,%000, it works fine using the hserin command and processed explicitly like a regular serial in. But if I try %100, and look for data in the scratchpad, it doesn't see any data. Does this chip not support background receive? I used your code example at the top of this post and I don't get anything out the terminal window. Cheers! - John
 

nick12ab

Senior Member
Hippy, I am having some difficulty getting the background serial receive working on an older 40x2 (5V) part (4520..) When is use hsersetup B9600_8,%000, it works fine using the hserin command and processed explicitly like a regular serial in. But if I try %100, and look for data in the scratchpad, it doesn't see any data. Does this chip not support background receive? I used your code example at the top of this post and I don't get anything out the terminal window. Cheers! - John
Because it's %001 you have to use, not %100.
 
Top