It might be a little better, but there's a much better way.
Receive 1 byte at a time in the handler
If you get an interrupt from the USART, you are guaranteed that there is at least 1 byte in the USART's buffer.
So when you get to the handler, just get the byte that you know is there. No Timeouts, no sitting around waiting for the next byte to finally come in. Serial data is Really slow compared to the speed of the processor. It doesn't make sense to waist millions of instructions sitting around waiting for serial data.
After getting the byte, exit the Handler. If there is another byte still in the buffer, then DT_INT's will automatically loop back around to grab the next one, without ever leaving the interrupt processor.
Then all you need to do is watch for the synchronizing character, and count how many bytes have been received. If you get a sync byte, reset the count. If you count the correct number of bytes received, set a flag to tell the main loop that the receive succeeded.
Something like this off the top of my head...
Code:
TempByte VAR BYTE
RXdone VAR BIT
'Receive Interrupt
Receive:
HSERIN [TempByte] ; Get the byte
IF (TempByte = MyID) THEN ; is it the sync byte?
Cnt = 0 ; Yes, reset the counter
RXdone = 0 ; indicate, RX not finished
@ INT_RETURN ; Return, and discard the sync byte
ENDIF
BufRX(Cnt) = TempByte ; Not a sync byte. save it.
Cnt = Cnt + 1 ; increment buffer pointer
IF (Cnt = 7) THEN ; If all bytes are received?
RXdone = 1 ; Tell Main loop to process it.
; If the main loop cannot process
; FOR Cnt = 0 to 6 ; the data before the next sync byte?
; RX_Data(Cnt) = BufRX(Cnt) ; Copy it to another "Working" buffer
; NEXT Cnt ; Reception will continue while the
ENDIF ; last packet is processed
@ INT_RETURN
;---------------------------------------
Main:
; ...
IF RXdone then Gosub ProcessData
; ....
GOTO Main
;---------------------------------------
ProcessData:
RXdone = 0
; ....
; ....
RETURN
This will use the least amount of time required to receive serial data. And the main loop has much more time to do whatever it needs to. And allows many more interrupts to happen too, if needed.
Just make sure that the Sync byte can NEVER be found in the data bytes. This goes for the way you had it too.
If you are sending data in the form of Bytes, it's likely that a data byte will have the same value as the sync byte. If that's possible, you'll need to develop a multi-byte synchronization, or transmit the data in plain text (HEX is the easiest).
HTH,
<br>
Bookmarks