Hello, I have been wrestling with this for days and thought I'd see if anyone could tell me what I'm doing wrong. I've Googled, gone through some excellent intro PIC books and read the forum here. The closest info I've found is here under the "serial string parsing" thread by billybobbins.

To give you a picture of the end product:
I am putting together a program that does four things: 1) Except a serial data stream of up to 40 bytes long, 2) execute commands based on data from the data stream, 3) re-transmit the data stream (nothing added or changed), and 4) count pulses from an encoder using an interrupt.

My problem is with 1). For the life of me, I cannot get read a stream of serial data as I'd like. I'm trying to create a loop that looks for serial data coming in, reads and stores it into a variable array if any is present and goes onto to perform another function briefly before going back into the original loop.

I'm in a Catch 22. If I don't use a timeout in the serin command, the program does nothing until data comes in, but when it comes in I have all of the bytes regardless of the size. If I do use a timeout either I get junk or a varying portion of the original data.

My test data is sent via hyperterminal at 2400 baud 8N1. This is sent as a data file (notepad txt).

Test data stream is: A00gfhgd00CR+
where the + is used to identify the end of the stream. Eventually the baud will be 9600 and a line feed will be used to denote the end of a stream, just can't simulate a line feed in notepad.

Here is the code with the results:

All code has the following:

include "modedefs.bas"

Trisb = %00000000
Trisc = %01000000
Portb = %00000000
Portc = %00000000

a var byte
b var byte
c var byte
d var byte
RX var byte[40]
TX var byte[40]
i var byte
dSize var byte
f var byte
PDA var byte
InCheck var byte

Yes, I have a few extra variables I'm not using in the current program.



A) Using a for loop and a timeout

Main:

serin portc.7, T2400, 100, LED, RX[0]

If RX[0] != 0 Then ReadPDA

goto LED

ReadPDA:

For i = 1 to 40

serin portc.7, T2400, RX[i]

If RX[i] = "+" Then DataOut
Next



goto Error 'DataEnd


LED:

portb.0 = 1
pause 10
portb.0 = 0
pause 10
PDA = 0
goto Main

DataOut:

for d = 0 to i
serout portc.6, T2400, [RX[d]]
next

serout portc.6, T2400, [10, 13]

goto Main

Error:

serout portc.6, T2400, ["Error"]

goto Main

And the results for five streams sent are:
0gfhgd00CR+
A00gfhgd00CR+
˜gfhgd00CR+
A00gfhgd00CR+
A00gfhgd00CR+

Okay, not bad, but the first run didn't catch the A0 and the third not only lost the A00 but thought it saw a y with two dots over it instead.

B) Using a while loop and a timeout


Main:

serin portc.7, T2400, 1, LED, RX[0]
'pause 1
If RX[0] != 0 Then ReadPDA
'If RX[0] = 0 Then LED
goto LED ' ReadPDA 'Error

LED:

portb.0 = 1
pause 10
portb.0 = 0
pause 10
PDA = 0
goto Main

ReadPDA:

counter = 1

while counter != 40 and RX[counter] != "+"
serin portc.7,T2400, RX[counter]
'if RX[counter] = 0 Then LED
'pause 1
counter = counter+1
Wend

If counter = 40 then Error

goto DataOut



DataOut:

for i = 0 to counter
serout portc.6, T2400, [RX[i]]
next

serout portc.6, T2400, [10, 13]

goto Main


Error:

serout portc.6, T2400, ["Error", 10, 13]

goto Main


And the results for five streams sent are:
00gfhgd00CR++
: fhgd00CR++
‚þ0gfhgd00+
ó0gfhgd00+
Óöfhgd00CR+

Again, if I remove the timeout, the data is perfect but the program doesn't move. I've seen some code with hserin and serin2, but I haven't played with those commands and haven't seen an advantage in using them in this application over serin, at least yet.

Any help or advice is greatly appreciated! Thanks!