Ahh - the interpreter isn't multi-threaded so it relies on having short functions that finish relatively quickly. When you get data from serial it goes into a Queue, and that queue is handled in the 'idle' loop - so it's expected that what you're doing there won't work.
The best bet is to use on('data' as you say. If you post up details of what you've got then I can try and see why it's not working?
As a start, you could try:
var dataLine = "";
Serial6.on('data',function(d) {
dataLine += d;
var idx = dataLine.indexOf("\n");
while (idx>=0) {
var command = dataLine.substr(0,idx);
soStuffWith(command);
dataLine = dataLine.substr(idx+1);
idx = dataLine.indexOf("\n");
}
});
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ahh - the interpreter isn't multi-threaded so it relies on having short functions that finish relatively quickly. When you get data from serial it goes into a Queue, and that queue is handled in the 'idle' loop - so it's expected that what you're doing there won't work.
The best bet is to use
on('data'
as you say. If you post up details of what you've got then I can try and see why it's not working?As a start, you could try: