Simple_Serial.spin Bug

It took hours to locate, so I'd best save others the trouble ...
If the 'b:=0' isn't added, you'll get corrupt characters under some circumstances, but not all. I presume it depends on how the stack has been used before rx is called and that local variables are not initialised to zero. I'd also recommend adding a 'waitpne(inverted & |< sin, |< sin, 0)' before the waitpeq just to make sure the line has returned to idle before looing for the start bit.
PUB rx | t, b
'' Receive a byte; blocks program until byte received
if started and rxOkay
b := 0 ' <=== ADD THIS
dira[noparse][[/noparse]sin]~ ' make rx pin an input
waitpeq(inverted & |< sin, |< sin, 0) ' wait for start bit
t := cnt + bitTime >> 1 ' sync + 1/2 bit
repeat 8
waitcnt(t += bitTime) ' wait for middle of bit
b := ina[noparse][[/noparse]sin] << 7 | b >> 1 ' sample bit
waitcnt(t + bitTime) ' allow for stop bit
return (b ^ inverted) & $FF ' adjust for mode and strip off high bits
If the 'b:=0' isn't added, you'll get corrupt characters under some circumstances, but not all. I presume it depends on how the stack has been used before rx is called and that local variables are not initialised to zero. I'd also recommend adding a 'waitpne(inverted & |< sin, |< sin, 0)' before the waitpeq just to make sure the line has returned to idle before looing for the start bit.
Comments
"All local variables ....are left uninitialized upon each call to the method."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?