Hserin parsing string...again?
Hello all,
This has probably been answered in a million different threads...all of which I have read, which leads me to my question.
What I'de like to do is receive a string into the usart similar to this:
ABC123
What I would like to do is store the ABC in a word var to send through a
select case statement and...
store the 123 in a byte var to use later in code.
The commands coming into the usart will always be this format, for ex:
ABC123
XYZ255
PQR002
LETTER-LETTER-LETTER-NUMBER-NUMBER-NUMBER
NUMBER-NUMBER-NUMBER not to exceed 256.
Any help parsing these from the RCREG or a 6 byte ring buffer?
Thanks to all,
Chris
Re: Hserin parsing string...again?
Hi All,
Can anyone offer an opinion on if this will work? I don't seem to be having much luck.
I'm trying to capture streaming data and there are a few sequences that I can key on to determine if I have a valid combination. So far the simplest is to look for the word "INVALID".
Here is one attempt:
Code:
' * * * * * * Interrupt handler *******
serialin: ' Buffer the character received
buffer[7] = buffer[5] ' get the newest character into the buffer
buffer[6] = buffer[5] ' and shift them into a sliding form
buffer[5] = buffer[4]
buffer[4] = buffer[3]
buffer[3] = buffer[2]
buffer[2] = buffer[1]
buffer[1] = buffer[0]
HSerin [buffer[0]] ' Read USART and store character in first position
IF buffer[0]="I" and buffer[1]="N" and buffer[2]="V" and buffer[3]="A" then
goto Freeze
endif
IF RCIF Then serialin ' Check for another character while we're here
@ INT_RETURN ; Return to program
I'm under the belief that by " "ing an alpha character that it will recognize it as the ASCII character and do the compare. Can anyone explain what I'm missing?
A related question is " how do I catch a STX ($02) and a ETX ($03) with a number (4) and a non control character (X)in the middle? The data stream is STX, 4, X, $FF and I'm struggling how to compare mixed forms. Can I just use the ASCII numbers and compare them?
Sorry for such a simple question, but it's kicking my butt.
Thanks
Bo
Re: Hserin parsing string...again?
If this is a cut and paste, you have a mistake in the first line (buffer[7] = buffer[5] should be buffer[7] = buffer[6])
Re: Hserin parsing string...again?
I have a question.... Why not just move a pointer instead of copying all of the data each time a new character is received?
As far as comparing the data bytes, just compare the Hex or Decimal ascii equivelents? After all thats all your "buffer[0]="I" statement is doing, comparing the ASCII equivelent of the received byte.
Re: Hserin parsing string...again?
Hi Guys,
Thanks for the replies.
The (buffer[7] = buffer[5] error was because it was 4 in the morning and I had worked on it way too long. The characters after the 5th weren't used in the compare, they were only for display so that I could get some idea on if it were successful. Thanks for seeing that.
Dave, I have looked at quite a few examples of compares and they are not yet clicking with me. The data is coming in @ 9600, so its not too fast, but I still manage to miss it.
I've looked at this, this, and this. They all have good examples and I should be able to build something, but as of yet, no banana.
I think my best answer is to just try again and dissect these and try to code them for my use. I will have to understand this better.
Thanks
Bo
Re: Hserin parsing string...again?
Remember that it's all just numbers, if you want to check if a byte contains 'I' either of these will work
Code:
IF myByte = 73 THEN....
If myByte = "I" THEN....
IF myByte = $49 THEN....
IF myByte = %00110001 THEN...
They all do the same thing - they compare the content of myByte with the decimal value 73, which is the ASCII code for the letter 'I'.
Instead of using AND you might try something like:
Code:
IF Buffer[0] = "I" THEN
IF Buffer[1] = "N" THEN
IF Buffer[2] = "V" THEN
IF Buffer[3] = "A" THEN
GOTO Freeze
ENDIF
ENDIF
ENDIF
ENDIF
There's nothing wrong with the AND approach but I think using multiple IF statements will produce smaller code.
/Henrik.
Re: Hserin parsing string...again?
Thanks Henrik,
I just needed a sanity check. Too many hours and poor results.
I'll have a look at it when I get back to the shop. Maybe something obvious after some sleep and a fresh look.
Bo
Re: Hserin parsing string...again?
Dave, (and, of course, anyone else that care to respond)
I'm a bit confused about checking the data with a pointer. Do you mind explaining more?
I understand setting up a ring buffer similar to DT's serial LCD program. What I'm not sure is if I need to keep it cleaned out or something similar. If I just keep stepping the pointer around the loop, what happens when you fill the buffer? Does it just overwrite the previous data and keep circling the ring? After looking at that program for a long time, I finally believe that is what happens.
Still trying to work towards for that "Neo" moment when the Matrix just starts to come into focus....
Bo
Re: Hserin parsing string...again?
Dave, (and, of course, anyone else that care to respond)
I'm a bit confused about checking the data with a pointer. Do you mind explaining more?
I understand setting up a ring buffer similar to DT's serial LCD program. What I'm not sure is if I need to keep it cleaned out or something similar. If I just keep stepping the pointer around the loop, what happens when you fill the buffer? Does it just overwrite the previous data and keep circling the ring? After looking at that program for a long time, I finally believe that is what happens.
Still trying to work towards for that "Neo" moment when the Matrix just starts to come into focus....
Bo