flush out serial data when too long (#375)

This commit is contained in:
Peli de Halleux 2017-03-24 09:51:07 -07:00 committed by GitHub
parent 684646d3de
commit 1f2027d0f9
2 changed files with 12 additions and 14 deletions

View File

@ -93,7 +93,7 @@ namespace pxsim {
break;
case "serial":
let data = (<SimulatorSerialMessage>msg).data || "";
this.serialState.recieveData(data);
this.serialState.receiveData(data);
break;
case "radiopacket":
let packet = <SimulatorRadioPacketMessage>msg;

View File

@ -1,8 +1,9 @@
namespace pxsim {
const SERIAL_BUFFER_LENGTH = 16;
export class SerialState {
serialIn: string[] = [];
public recieveData(data: string) {
public receiveData(data: string) {
this.serialIn.push();
}
@ -13,18 +14,15 @@ namespace pxsim {
serialOutBuffer: string = "";
writeSerial(s: string) {
for (let i = 0; i < s.length; ++i) {
let c = s[i];
this.serialOutBuffer += c;
if (c == "\n") {
Runtime.postMessage(<SimulatorSerialMessage>{
type: "serial",
data: this.serialOutBuffer,
id: runtime.id
})
this.serialOutBuffer = ""
break;
}
this.serialOutBuffer += s;
if (/\n/.test(this.serialOutBuffer) || this.serialOutBuffer.length > SERIAL_BUFFER_LENGTH) {
Runtime.postMessage(<SimulatorSerialMessage>{
type: 'serial',
data: this.serialOutBuffer,
id: runtime.id,
sim: true
})
this.serialOutBuffer = '';
}
}
}