You are reading a single comment by @countxerox and its replies. Click here to read the full conversation.
  • Here's my code using pipe:

    /* Module for interfacing with FrSky SBUS radio control receivers
    
    Serial2.setup(100000, {rx:A3,parity:E,stopbits:2});
    mysbus = require('SBUS').connect(Serial2, function(data){
      console.log(data);
    });
    
    */
    
    function decode(frame, callback) {
      arr = E.toUint8Array(frame);
      callback({
        ch1 :  ((arr[0]     | arr[1]<<8)                & 0x07FF),
        ch2 :  ((arr[1]>>3  | arr[2]<<5)                & 0x07FF),
        ch3 :  ((arr[2]>>6  | arr[3]<<2  | arr[4]<<10)  & 0x07FF),
        ch4 :  ((arr[4]>>1  | arr[5]<<7)                & 0x07FF),
        ch5 :  ((arr[5]>>4  | arr[6]<<4)                & 0x07FF),
        ch6 :  ((arr[6]>>7  | arr[7]<<1  | arr[8]<<9)   & 0x07FF),
        ch7 :  ((arr[8]>>2  | arr[9]<<6)                & 0x07FF),
        ch8 :  ((arr[9]>>5  | arr[10]<<3)               & 0x07FF),
        ch9 :  ((arr[11]    | arr[12]<<8)               & 0x07FF),
        ch10 : ((arr[12]>>3 | arr[13]<<5)               & 0x07FF),
        ch11 : ((arr[13]>>6 | arr[14]<<2 | arr[15]<<10) & 0x07FF),
        ch12 : ((arr[15]>>1 | arr[16]<<7)               & 0x07FF),
        ch13 : ((arr[16]>>4 | arr[17]<<4)               & 0x07FF),
        ch14 : ((arr[17]>>7 | arr[18]<<1 | arr[19]<<9)  & 0x07FF),
        ch15 : ((arr[19]>>2 | arr[20]<<6)               & 0x07FF),
        ch16 : ((arr[20]>>5 | arr[21]<<3)               & 0x07FF),
        ch18 : (arr[22] & 0x0001) ? 2047:0,
        ch19 : ((arr[22] >> 1) & 0x0001) ? 2047:0,
        framelost : ((arr[22] >> 2) & 0x0001) ? true:false,
        failsafe  : ((arr[22] >> 3) & 0x0001) ? true:false,
      });
    }
    
    exports.connect = function (serial, callback) {
      dest = {
        state: 'sync',
        last: '',
        write : function(data) {
          switch(this.state){
            case 'sync':
              buff = data;
              if (this.last === "\x00" && buff.substr(0,1) == "\x0F"){
                this.state = 'read';
              }
              break;
            case 'read':
              buff += data;
              if (buff.length > 24) {
                if (buff.substr(24,1) === "\x00") {
                  this.state = 'sync';
                  decode(buff.substr(1,23), callback); 
                }else{
                  buff = buff.substr(buff.length - buff.indexOf("\x0F",1));
                }
              }
              break;
          }
          this.last = data.substr(-1,1);
        }
      };
      serial.pipe(dest,{chunkSize : 25});
    };
    
About

Avatar for countxerox @countxerox started