Background serial demo hiccup

SD2100

New Member
I've been playing around with background serial receive and while trying to sort a few things out I tried the demo code below, all works well until the scratchpad overflows then a problem appears, some of the serial data is lost when the pointer goes to zero, the problem appears to be in the FOR/NEXT loop when the end value (last_byte_received) becomes less than the start value (next_byte_to_echo) and a dozen or so bytes disappear.

Code:
; This example program shows how to use the serial hardware
; with automatic background serial receives.
; Serial hardware pins (C6 and C7) connected via MAX232 to
; computers serial port. Output 1 connected to an LED.

; In normal use the LED just flashes. When a background serial receive
; occurs the bytes are echoed back out for testing purposes.

symbol last_byte_received = b1
symbol next_byte_to_echo = b2
symbol counter = b3

; set picaxe type
#picaxe 28x1

; open terminal after download
#terminal 2400
; Don't forget to move serial cable to the hardware pins!

   ; setup serial hardware 
   ; at 9600 with background receive
   hsersetup b2400_4,%01
	
   'loop waiting for a byte
main:
	
   ' do something - e.g. flash LED
   toggle 1		
   pause 500
	
   ' test hserinflag which is set when a 
   ' background receive occurs
   if hserinflag = 1 then byte_echo
   goto main

   ' echo back out the received bytes
byte_echo:
   hserinflag = 0	'reset the flag
		
   ' the last scratchpad address written to 
   ' will be the serialpointer - 1
   last_byte_received = hserptr - 1
	
   ' echo out any bytes we have not yet used 
   for counter = next_byte_to_echo to last_byte_received
      ptr = counter  ' set scatchpad pointer to the relevant address
                     ' and then output the data @ that address
      hserout 0,(#ptr,"=",@ptr,cr,lf)
   next counter

   ' set the last byte echoed as the current serial pointer value 
   next_byte_to_echo = last_byte_received + 1
	
   'loop
   goto main
I've made some changes which seems to have fixed the problem, data can be sent and no data will be lost on the buffer overflow.

Code:
symbol lastbyte = b0

; set picaxe type
#picaxe 28x1

; open terminal after download
#terminal 2400
; Don't forget to move serial cable to the hardware pins!

hsersetup b2400_4,%01	'Setup for background receive at 2400		

Main:
   ' do something - e.g. flash LED
   toggle 1
   pause 500
	
   ' test hserinflag which is set when a 
   ' background receive occurs
   if hserinflag = 1 then Byte_Echo
   goto main

' echo back out the received bytes
Byte_Echo:
   hserinflag = 0		 'Reset receive flag
	
   ' the last scratchpad address written to 
   ' will be the serialpointer
   lastbyte = hserptr
   ' echo out any bytes we have not yet used 
   do
      hserout 0,(#ptr,"=",@ptr,cr,lf) 'Echo byte to PC

      ' set scatchpad pointer to the relevant address
      ' and then output the data @ that address
      inc ptr
      if ptr > 127 then	'Check for buffer overflow
         ptr = 0		'set pointer to start of buffer
      end if
   loop until ptr = lastbyte 'Keep sending to PC until last byte
   goto main	'Go back to main loop and check if more data has arrived
Also what would be the best way to clear the buffer so all data in it is erased and the ptr and hserptr are set to zero.

Thanks
 
Top