//slip1.js
//26 Aug 2016
//Written on a PICO
//For testing connect Tx to RX for loopback, pins B6 and B7 Serial1
//SLIP protocol
// https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol
var END=0xC0;
var ESC=0xdb;
var ESC_END=0xdc;
var ESC_ESC=0xdd;
// END+Packet+END
//if the END byte occurs in the data to be sent, the two byte sequence
// ESC, ESC_END is sent instead,
//if the ESC byte occurs in the data, the two byte sequence ESC, ESC_ESC // is sent.
var baudrate=115200;
var Port=Serial1;
//Port.setup(baudrate);
function Packet(){
this.A="";
}
var P=new Packet();
P.on('data',function(data){
console.log("P.on= "+data);
});
//var sss="";
var State=0;
Port.on('data', function (data){
var i;
console.log("Port.on= "+data);
for(i=0;i<data.length;i++){
switch(State){
case 0://looking for END to start packet
//console.log("0");
if(data.charCodeAt(i)===END){
State=10;
i++;
}
return;
case 10://get the packet
//console.log("10");
if(data.charCodeAt(i)===END){
State=0;
P.emit('data',P.A);
i++;
return;
}
State=20;
return;
case 20://look for ESC
//console.log("20");
if(data.charCodeAt(i)===ESC){State=30;i++;return;}
P.A+=data.charAt(i);
State=10;
return;
case 30://ESC was seen
//console.log("30");
if(data.charCodeAt(i)===ESC_END){
P.A+=String.fromCharCode(END);
State=10;
return;
}
if(data.charCodeAt(i)===ESC_ESC){
P.A+=String.fromCharCode(ESC);
State=10;
return;
}
return;
default:
console.log("default error");
break;
}//end switch
}//next i
});
function sendEND(){
Port.write(String.fromCharCode(END));
}
function sendESC(){
Port.write(String.fromCharCode(ESC));
}
function sendESC_END(){
Port.write(String.fromCharCode(ESC_END));
}
function sendESC_ESC(){
Port.write(String.fromCharCode(ESC_ESC));
}
function sendPacket(A){
var i;
sendEND();
for(i=0;i<A.length;i++){
switch(A.charCodeAt(i)){
case END:
sendESC();
sendESC_END();
break;
case ESC:
sendESC();
sendESC_ESC();
break;
default:
Port.write(A.charAt(i));
break;
}//end switch
}//next i
sendEND();
}
function test(){
var text=
"Hello"+String.fromCharCode(ESC)+"World"+String.fromCharCode(END)+
"Eagles";
Port.setup(baudrate);
console.log("Text= "+text);
sendPacket(text);
}//end test
test();
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.
Slip1.js 26 Aug 2016
A program to send and receive data packets in SLIP format.
Written for a PICO
https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol
The output:
1 Attachment