Problem with Parsing a String from the Serial Port
kvanepps
Posts: 10
To All:
I am new to using the Propeller so I think I am doing something wrong that would be a simple fix.
The idea is to receive a string of characters that will be delimited by "|" (a pipe) including the end of the string. All the information will be numbers and I want to store them as a single byte for each number. For example, I will send the following info to be parsed:The numbers will never be greater than 200 so I want to use a byte for each number.
30|200|4|12|15| will be sent
parsed out to the following byte variables
a[0]=30
a[1]=200
a[2]=4
a[3]=12
a[4]=15
Below is the code. Following the code is the result I am seeing. I modified the Serial Parallax Terminal object to allow me to access the StrtoBase method. I know this may be wrong but I wanted to get something working. Sorry for the sloppy code but I am very frustrated right now.....
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
byte a[40]
OBJ
pst : "Parallax Serial Terminal"
PUB TestMessages |input[40], decres, ttt[60]
''Send test messages to Parallax Serial Terminal.
pst.Start(115_200)
bytefill(@a, 0, 40)
repeat
pst.StrIn(input)
pst.str(string("Original string was: "))
pst.str(input)
Pst.newline
ParseCommand(input)
Pst.newline
pst.str(string("Sanity check!"))
Pst.newline
bytefill(@a, 0, 40)
PRI PrintSingleChar(mystring)
repeat strsize(mystring)
pst.str((mystring++))
pst.newline
PRI ParseCommand(mystring)| i, count, ourtemp[40]
i:=0
count:=0
pst.newline
pst.str(string("In ParseCommand!"))
pst.newline
pst.str(string("ourtemp string contains: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring contains: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
repeat strsize(mystring)
pst.newline
pst.str(string("Checking charachter in string: '"))
pst.char(byte[mystring])
pst.str(string("'"))
pst.newline
if byte[mystring] == "|"
Pst.Str(string("Found pipe"))
pst.newline
Pst.Str(string("
"))
pst.newline
a:=pst.StrToBase(ourtemp,10)
pst.str(string("a contains: "))
pst.newline
pst.bin(a, 8)
pst.newline
pst.dec(a)
pst.newline
pst.str(string("ourtemp string is now: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring is now: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
pst.str(string("
"))
pst.newline
i+=1
count:=0
' bytefill(@ourtemp, 0, 40)
else
bytemove(@ourtemp[count], @mystring[0], 1)
pst.str(string("Count is now: "))
pst.dec(count)
pst.newline
pst.str(string("ourtemp string is now: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring is now: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
count++
mystring++
My debug statements output the following if I send 1|2|3| :
Original string was: 1|2|3|
In ParseCommand!
ourtemp string contains: ''
mystring contains: '1|2|3|'
Checking charachter in string: '1'
Count is now: 0
ourtemp string is now: '1|2|3|'
mystring is now: '1|2|3|'
Checking charachter in string: '|'
Found pipe
a contains:
01111011
123
ourtemp string is now: '1|2|3|'
mystring is now: '|2|3|'
Checking charachter in string: '2'
Count is now: 0
ourtemp string is now: '2|3|'
mystring is now: '2|3|'
Checking charachter in string: '|'
Found pipe
a contains:
00010111
23
ourtemp string is now: '2|3|'
mystring is now: '|3|'
Checking charachter in string: '3'
Count is now: 0
ourtemp string is now: '3|'
mystring is now: '3|'
Checking charachter in string: '|'
Found pipe
a contains:
00000011
3
ourtemp string is now: '3|'
mystring is now: '|'
Sanity check!
Thank You
Ken
I am new to using the Propeller so I think I am doing something wrong that would be a simple fix.
The idea is to receive a string of characters that will be delimited by "|" (a pipe) including the end of the string. All the information will be numbers and I want to store them as a single byte for each number. For example, I will send the following info to be parsed:The numbers will never be greater than 200 so I want to use a byte for each number.
30|200|4|12|15| will be sent
parsed out to the following byte variables
a[0]=30
a[1]=200
a[2]=4
a[3]=12
a[4]=15
Below is the code. Following the code is the result I am seeing. I modified the Serial Parallax Terminal object to allow me to access the StrtoBase method. I know this may be wrong but I wanted to get something working. Sorry for the sloppy code but I am very frustrated right now.....
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
byte a[40]
OBJ
pst : "Parallax Serial Terminal"
PUB TestMessages |input[40], decres, ttt[60]
''Send test messages to Parallax Serial Terminal.
pst.Start(115_200)
bytefill(@a, 0, 40)
repeat
pst.StrIn(input)
pst.str(string("Original string was: "))
pst.str(input)
Pst.newline
ParseCommand(input)
Pst.newline
pst.str(string("Sanity check!"))
Pst.newline
bytefill(@a, 0, 40)
PRI PrintSingleChar(mystring)
repeat strsize(mystring)
pst.str((mystring++))
pst.newline
PRI ParseCommand(mystring)| i, count, ourtemp[40]
i:=0
count:=0
pst.newline
pst.str(string("In ParseCommand!"))
pst.newline
pst.str(string("ourtemp string contains: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring contains: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
repeat strsize(mystring)
pst.newline
pst.str(string("Checking charachter in string: '"))
pst.char(byte[mystring])
pst.str(string("'"))
pst.newline
if byte[mystring] == "|"
Pst.Str(string("Found pipe"))
pst.newline
Pst.Str(string("
"))
pst.newline
a:=pst.StrToBase(ourtemp,10)
pst.str(string("a contains: "))
pst.newline
pst.bin(a, 8)
pst.newline
pst.dec(a)
pst.newline
pst.str(string("ourtemp string is now: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring is now: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
pst.str(string("
"))
pst.newline
i+=1
count:=0
' bytefill(@ourtemp, 0, 40)
else
bytemove(@ourtemp[count], @mystring[0], 1)
pst.str(string("Count is now: "))
pst.dec(count)
pst.newline
pst.str(string("ourtemp string is now: '"))
pst.str(ourtemp)
pst.str(string("'"))
pst.newline
pst.str(string("mystring is now: '"))
pst.str(mystring)
pst.str(string("'"))
pst.newline
count++
mystring++
My debug statements output the following if I send 1|2|3| :
Original string was: 1|2|3|
In ParseCommand!
ourtemp string contains: ''
mystring contains: '1|2|3|'
Checking charachter in string: '1'
Count is now: 0
ourtemp string is now: '1|2|3|'
mystring is now: '1|2|3|'
Checking charachter in string: '|'
Found pipe
a contains:
01111011
123
ourtemp string is now: '1|2|3|'
mystring is now: '|2|3|'
Checking charachter in string: '2'
Count is now: 0
ourtemp string is now: '2|3|'
mystring is now: '2|3|'
Checking charachter in string: '|'
Found pipe
a contains:
00010111
23
ourtemp string is now: '2|3|'
mystring is now: '|3|'
Checking charachter in string: '3'
Count is now: 0
ourtemp string is now: '3|'
mystring is now: '3|'
Checking charachter in string: '|'
Found pipe
a contains:
00000011
3
ourtemp string is now: '3|'
mystring is now: '|'
Sanity check!
Thank You
Ken
Comments
-Phil