Mister_E,

Thank you for the lovely code! It works nicely and I've been examining it while referencing the datasheet and I have learned a good deal. I then tried to manipulate it to work with my application and I've hit a wall. Initially I'm trying to look at a string of characters sent via the hyperterminal, particularly the first and last bit.

Now, perhaps I'm going into overkill with caution, but I only want to store the string into a variable array if the string starts with a ASCII "!". I then want to continue taking the rest of the string until an ASCII "L" is seen.

The idea being if for some reason or another, I receive a string of data and I start storing the data string at the middle instead of the beginning and miss some data. Here's what I have:

' Program to echo incoming serial data
' Baudrate : 2400 Bauds
' Method : Polling USART interrupts flags

' Pic Definition
' ==============
' Using PIC 18F2320 @ 4MHZ and bootloader
'
DEFINE LOADER_USED 1
DEFINE OSC 4

' Hardware configuration
' ======================
'
'
TRISC = %10000000 ' PORTC.7 is the RX input
' PORTC.6 is the TX output

' Serial communication definition
' ===============================
' Using internal USART and MAX232 to interface to PC
'
DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
define HSER_TXSTA 24h ' enable transmit,
' BRGH=1
'
define HSER_SPBRG 103 ' set baudrate to 2400
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

' Alias definition
' ================
'
'
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
a var byte
i var byte
' Variable definition
' ===================
'
'
SerialData var byte[40]

' Hardware initialisation
' =======================
'
'
pause 10 ' safe start-up delay

Main:
' Clear - tried using this to clear, but then I get nothing back

if RCIF then ' incomming data?
hserin [SerialData[0]] 'Store the first character received in SerialData,
'at byte 0
endif

if SerialData[0] = "!" then RCV 'if the first byte is an ASCII ! then
'go on to receive rest of data string


goto Main


RCV:

for i = 1 to 40

hserin [SerialData[i]] ' take it
if SerialData[i] = "L" then TRS 'if an ASCII L is
'present stop
'and output
'string
next

goto Main 'If ASCII L is not detected return to main loop


TRS:
for a = 1 to i 'Okay, if I set a = 0 to i, I get a ! in
'front of everything when it outputs
'i.e. if I send !#00CL I get !!#00CL
'back. Why, I don't know.
hserout [SerialData[a]] ' send it
next 'endif
hserout [10,13] 'line feed and carriage return
goto Main





Alright, if I send !#X00CL I get back !#X00CL, which is great! However, and here's the where the confusion starts, if I send 123456ABCDEF#, I get nothing back, again, good news right? But, if I follow that string with !#X00CL, I get back 123456ABCDEF#!#X00CL. I am hitting a wall here.

It seems as though the program stores up to 41 (0 to 40) bytes from a serial string (this because in my code I set SerialData to store up to 41 bytes, from 0 to 40). Once it sees 41 bytes, it resets itself and starts anew until it sees an ASCII L. For example, if I send 123456ABCDEF# three times then follow with !#X00CL I get back #1X00CL.

Is there a way to tell the program to write and erase to only SerialData[0] until a specific character is seen? ASCII ! in this case. Or am I missing something else altogether? Thanks!