You are reading a single comment by @countxerox and its replies. Click here to read the full conversation.
  • Yep, with parity:'e' all the framing errors are gone.

    Serial1.setup(100000, {rx:B7,parity:'e',stopbits:2});
    Serial1.on('framing', function() {
      console.log("frame");
      });
    Serial1.on('parity', function() {
      console.log("parity");
      });
    Serial1.on('data', function(data) {
      console.log(E.toUint8Array(data));
    });
    Output...
    new Uint8Array([15])
    new Uint8Array([172, 96, 5, 43])
    new Uint8Array([88, 193, 10, 86, 176])
    new Uint8Array([130, 21, 172, 96, 5])
    new Uint8Array([43, 88, 193, 10, 86, 176])
    new Uint8Array([130, 21, 0, 0])
    

    0x0F is the startbyte, the following 22 bytes are the payload, then the next byte is for flags which show if the receiver has dropped frames or if the signal has been lost and then the last byte is the endbyte 0x00. The framerate is about 7ms but it wibbles at bit so I can't think of anyway to sync using timing.

    The startbyte and endbyte could potentially appear in the payload so I'm watching for 0x00 followed by 0x0F to mark the start of a frame, then when the 25 bytes have been received I check the last byte is 0x00 for validation.

    Hers's my code using Serial.on('data'...

    /* Module for interfacing with FrSky SBUS radio control receivers
    
    Serial1.setup(100000, {rx:B7,parity:'e',stopbits:2});
    mysbus = require('SBUSON').connect(Serial1, 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);
        }
      };
    Serial1.on('data', function(data) {
      dest.write(data);
    });
    };
    

    I seem to have bytes going missing sometimes when the callback's called

About

Avatar for countxerox @countxerox started