Serial communcation question
turbosupra
Posts: 1,088
I'd like to detect a carriage return, then send the cached bytes (prior to the carriage return) to a delimiter routine.
So let's say I typed into the Parallax Serial Terminal forum=65 and then a carriage return.
After carriageReturnCheck equaled 13, it would then send "forum=65" to my delimiter routine ... but I don't know how to do the cached bytes storage, stringIn := (stringIn + debug.rx) does not work.
I can post the additional code routines if it would make answer the question easier.
Thanks!
So let's say I typed into the Parallax Serial Terminal forum=65 and then a carriage return.
After carriageReturnCheck equaled 13, it would then send "forum=65" to my delimiter routine ... but I don't know how to do the cached bytes storage, stringIn := (stringIn + debug.rx) does not work.
I can post the additional code routines if it would make answer the question easier.
Thanks!
stringIn := (stringIn + debug.rx) carriageReturnCheck := debug.rx if (carriageReturnCheck == 13) debug.str(string("Carriage return detected")) debug.tx(13) debug.str(@stringIn) debug.tx(13) debug.StrIn(@stringIn) DelimiterFinder(@stringIn)
Comments
As a good sample of string manipulation, see Kye's string routines in OBEX.
You will also have to replace the cr with $0 in the stringIn or you will overrun the buffer until a null is found ( a null=$0 is the string terminator)
Instead, use an index designator to store each byte in a seperate element of the array: stringIn[0] := debug.rx stores the byte in the first element. Remember that arrays start at 0.
Use a variable for the index to advance through the array as each byte is received: stringIn[i++] := debug.rx
The ++ operator increments the index after the byte has been stored, so you need to loop until the byte at the previous index is a carriage return.
You of course need to make sure that the array is large enough or have some way of limiting the index or otherwise controlling the length of the string.
I cannot seem to get this to work, I had to add the rxcheck statement because my cog was hung in the repeat loop ... what am I missing?
EDIT: You are also incrementing i when reading the character, and then referring to stringIn later on. You should either increment i after doing everything else, or use stringIn[i-1] to compensate for having incremented i.
with the result of
[code]
bCharacter detected
oCharacter is - o
bCharacter detected
=Character is - =
1Character detected
0Character is - 0
0Character detected
Character is -
ˆP,
but the debug.str(stringIn) still prints nonsense to the terminal.
I just read your message after my latest code and I believe I've gotten that part correct. Any idea why I can't write the final value ( debug.str(stringIn) ) as confirmation I've ingested it and stored it in ram?
I did not work in my code at first, but then I noticed you had stringIn defined as a 32bit byte array and I had it as a long.
Can you explain why you knew to do that so I can understand why that the correct choice over a 32bit long?
Strings in SPIN are zero terminated byte arrays. There is nothing else to it. You can use a long array if you want but still have to fill it with bytes e.g.