The missing element is that you need two levels of looping, but you’re trying to do it with only one.
The algorithm should read:
-
Collect characters into an array as long as they are decimal digits – this may comprise multiple digits, so it must be an inner loop, which is most easily expressed by
while
.while { ascii = ~port.read.asAscii; ascii.isDecDigit } { charArray = charArray.add(ascii); };
-
After this,
ascii
will be a non-digit. Use this character to decide which value is set.value = charArray.collect(_.digit).convertDigits }; switch(ascii) { $a } { ~val_01 = value } { $; } { ~val_02 = value }
And all of that runs in an outer loop.
This way, you need only one charArray. Also, charArray is properly local.
(
~charArray_01 = [ ];
~charArray_02 = [ ];
~getValues = Routine.new({
var ascii, charArray, value;
loop {
while {
ascii = ~port.read.asAscii;
ascii.notNil and: { ascii.isDecDigit }
} {
charArray = charArray.add(ascii);
};
value = charArray.collect(_.digit).convertDigits;
charArray = nil; // EDIT: Need to clear the list!
switch(ascii)
{ $a } { ~val_01 = value }
// (or maybe 'b' would make more sense here?)
{ $; } { ~val_02 = value };
};
}).play;
)
hjh