Using hubSerial.available() - rather than hubSerial.data('on') - to chunk the input stream seems to make some difference (fewer errors) at 4800 baud. But no difference at 2400 (mostly good) or 9600 (bad transmission). See code below (both .available and .on('data') fragments are included:
var d0 = console.log;
var debug = console.log;
var hubSerial = new Serial();
var usbSerial = new Serial();
function onInit() {
hubSerial.setup(4800, { // problematic when baud rate=9600
rx: NodeMCU.D5,
tx: NodeMCU.D6
});
usbSerial.setup(9600, {
rx: NodeMCU.D9,
tx: NodeMCU.D10
});
usbSerial.on('data', function (data) {
try {
hubSerial.print(data);
} catch (e) {
d0("failed to send on hub's data");
}
});
//*
hubSerial.on('framing', function (data) {
d0("framing error");
});
setInterval(() => {
let n = hubSerial.available();
if (n > 0) {
//d0(n);
let d = hubSerial.read(0);
usbSerial.print(d);
}
}, 200);
//*/
/*
hubSerial.on('data', function (data) {
try {
// d0(data.length);
usbSerial.print(data);
} catch (e) {
d0("failed to send on usb's data");
}
});
//*/
}
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.
Using hubSerial.available() - rather than hubSerial.data('on') - to chunk the input stream seems to make some difference (fewer errors) at 4800 baud. But no difference at 2400 (mostly good) or 9600 (bad transmission). See code below (both .available and .on('data') fragments are included: