[futurebasic] FB string parsing

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2012 : Group Archive : Group : All Groups

From: Spencer Ted <tedspencer50@...>
Date: Mon, 2 Apr 2012 18:10:44 -0400
There's a wee USB micro connected, and it sends a string reporting 3 voltages via a com port at 57600. The data is making it into the Mac. The string goes like this:

p (the lowercase character
1234 (a number from 0 to 4095)
r (LC character)
2345 (number again)
s (LC character)
0123 (number)

The string, then, could look like "p1234r2345s0123p1235r2346s0125p....", or "p1r2s3p4r5s6...". There are no CRs or LFs. 5 complete strings arrive a second for debug purposes, but will need to be much faster when it works. A frame which is to be extracted and parsed would start with the "p" and end with the last character before the next "p".

Consider this, being called every 2 ticks:

Begin globals
dim as str255 gC1
end globals

local fn showvoltage
dim as str255 c
dim as short nCharsReady,pPos

nCharsReady = lof (-1)
read #-1,c;nCharsReady
gc1 = gc1 + c

long if nCharsReady > 100	// only if stuff built up
gc1 = right$(gC1,16)		// chuck most of it, but leave a p behind
pPos = instr(1,gC1,"p")
gC1 = mid$(gC1,pPos)	// get the p and everything that follows
end if

pPos = instr(2,gc1,"p")	// is there a next "p"?

long if pPos	// yup
Edit field 1,gc1
c = left$(gC1,pPos-1)
gC1 = right$(gC1,len(gC1)-pPos)	//take out what we've read
edit field 3,c
edit field 4,str$(nCharsReady)
pPos = instr(1,c,"r")
volt = val(left$(c,pPos-1))
appearance button 2,,volt	// updte the voltage slider
end if

end fn

The edits are there for debug purposes.

Problem: the leading "p" never turns up in the edits, although the balance of the string does. Whatever my blind spot is, it also is preventing me from finding the "r" and the "s".

Duh??? I'm well past feeling shame, so be brutal, please...

Ted