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 ...
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 -
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 ...
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 ...
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
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
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
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