Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range Error #12

Open
mkarrei opened this issue May 26, 2014 · 20 comments
Open

Range Error #12

mkarrei opened this issue May 26, 2014 · 20 comments

Comments

@mkarrei
Copy link

mkarrei commented May 26, 2014

Hi,

I'm using xbee-api on my project, but I have a problem when the nodes (I'm using xbee-api for Arduino) send a information to controller (using node js). Sometimes (after a long time) I have a error:

Caught exception: RangeError: Trying to write outside buffer length - Line: undefined
RangeError: Trying to write outside buffer length
at RangeError ()
at checkInt (buffer.js:788:11)
at Buffer.writeUInt8 (buffer.js:794:5)
at XBeeAPI.parseRaw (/user/node_modules/xbee-api/lib/xbee-api.js:148:14)
at Object.parser (/user/node_modules/xbee-api/lib/xbee-api.js:119:10)
at Object.SerialPort.options.dataCallback (/user/node_modules/serialport/serialport.js:143:15)
at SerialPortFactory.SerialPort._emitData (/user/node_modules/serialport/serialport.js:312:20)
at afterRead (/user/node_modules/serialport/serialport.js:290:18)
at /user/node_modules/serialport/serialport.js:304:9
at Object.wrapper as oncomplete

I'm printing: console.log(S.b); console.log(S.offset); on file xbee-api.js, line 147, and after a long time values > 255 are received (495, 498, 499, 502, 495, 505, ...). Then I need to restart the application.

Thank's

@jankolkmeier
Copy link
Owner

Looks like it might be a bug. To be sure: Are you sending/receiving "a lot" of messages? Are you properly using the api_mode setting to match your XBee?

Could you share a minimal version of your program that reproduces the error? Or preferrably a dump of the incomming data that produce the error?

@duffmeister
Copy link

I had the same issue but it was resolved by making sure that the baud rate on the xbee and the initialization for the serial library matched (9600 in my case).

@mkarrei
Copy link
Author

mkarrei commented Sep 19, 2014

Hi,

Sorry for long time. Here is the code I use in my raspberry pi.

var xbee_api = require('xbee-api');
var SerialPort = require('serialport').SerialPort;

xbeeAPI = new xbee_api.XBeeAPI({
api_mode: 2,
module: "802.15.4",
raw_frames: false
});

serialPortBee = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600,
parser: xbeeAPI.rawParser()
});

serialPortBee.on("open", function () {
console.log('xbeeStarted');
});

xbeeAPI.on("frame_object", function(frame)
{
var data = JSON.stringify(frame);
data = JSON.parse(data);
...
});

I'm printing on line 123 of file xbee_api.js

...
XBeeAPI.prototype.parseRaw = function(buffer) {
console.log(buffer);
console.log(''+buffer);
var S = this.parseState;
for(var i = 0; i < buffer.length; i++) {

}
...

The log file (forever) is at http://pastebin.com/wCrnJ0Wr

@xianlin
Copy link

xianlin commented Apr 14, 2015

Hi @jankolkmeier , I have the similar problem as the below:

buffer.js:788
    throw RangeError('Trying to write outside buffer length');
          ^
RangeError: Trying to write outside buffer length
    at RangeError (<anonymous>)
    at checkInt (buffer.js:788:11)
    at Buffer.writeUInt8 (buffer.js:794:5)
    at XBeeAPI.parseRaw (/home/user/node_modules/xbee-api/lib/xbee-api.js:155:14)
    at XBeeAPI.<anonymous> (/home/user/node_modules/xbee-api/lib/xbee-api.js:126:10)
    at Object.SerialPort.options.dataCallback (/home/user/node_modules/serialport/serialport.js:179:15)
    at SerialPortFactory.SerialPort._emitData (/home/user/node_modules/serialport/serialport.js:362:20)
    at afterRead (/home/user/node_modules/serialport/serialport.js:338:20)
    at /home/user/node_modules/serialport/serialport.js:354:9
    at Object.wrapper [as oncomplete] (fs.js:459:17)

the condition to reproduce this problem is my code:

var util = require('util')
var SerialPort = require("serialport").SerialPort
var xbee_api = require('xbee-api');

var C = xbee_api.constants;

var xbeeAPI = new xbee_api.XBeeAPI({
  api_mode: 1
});

var serialPort = new SerialPort("/dev/ttyUSB0", {
  baudrate: 57600,
  parser: xbeeAPI.rawParser()
});

// All frames parsed by the XBee will be emitted here
xbeeAPI.on("frame_object", function(frame) {
    console.log(">>", util.inspect(frame));
});

XBee Sending Setting -- This is the remote Xbee sending ADC sample IO data via 16bit address
API Mode 1, source address-0x31, dest address-0xFF, sample and send rate (IR) - 500ms

XBee Receiving Setting -- This is the XBee talking to the xbee-api Application.
API Mode 1, BD: 57600, source address-0xFF

above setup tested under X-CTU and the XBee can receive packets successfully from the remote XBee with IO sample data.

When I run the node-serialport test program to see the buffer:

var com = require("serialport");

var serialPort = new com.SerialPort("/dev/ttyUSB0", {
    baudrate: 57600,
    parser: com.parsers.raw
  });

serialPort.on('open',function() {
  console.log('Port open');
});

serialPort.on('data', function(data) {
    console.log(data);
});

I got the below output which shows a lot of zeroes ahead of the useful buffer data.

Port open
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 83 00 31 32 00 01 06 04 00 04 03 ff 03 ff 06>
<Buffer 7e 00 0e 83 00 31 33 00>
<Buffer 01 06 04 00 04 03 ff 03 ff 05>
<Buffer 7e>
<Buffer 00 0e 83 00 31 32 00 01 06 04>
<Buffer 00 04 03 ff 03 ff>
<Buffer 06>
<Buffer 7e 00 0e 83 00 31>
<Buffer 37 00 01 06 04 00 04 03 ff 03 ff 01>
<Buffer 7e 00 0e 83 00 31 36 00 01 06 04 00>
<Buffer 04 03 ff 03 ff 02>
<Buffer 7e 00>
<Buffer 0e 83 00 31 34>
<Buffer 00 01 06 04 00 04 03>
<Buffer ff 03 ff 04>

Now I assume those leading zeros causes buffer length range errors and I don't know why so many leading zeros generated.
BTW, those leading zeros goes away after I exit my app and restart it.

Another strange thing is that if I enable "Encryption" on both XBee and set a 32-bit Hex String as the key and run the same app, it shows new errors:

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: Out of Original Buffer's Boundary
    at BufferReader.MAKE_NEXT_READER.BufferReader.(anonymous function) [as nextUInt16BE] (/home/user/node_modules/xbee-api/node_modules/buffer-reader/index.js:77:9)
    at Object.frame_parser.Recieved16BitPacketIO (/home/user/node_modules/xbee-api/lib/frame-parser.js:241:40)
    at Object.frame_parser.(anonymous function) [as 131] (/home/user/node_modules/xbee-api/lib/frame-parser.js:298:16)
    at XBeeAPI.parseFrame (/home/user/node_modules/xbee-api/lib/xbee-api.js:110:27)
    at XBeeAPI.parseRaw (/home/user/node_modules/xbee-api/lib/xbee-api.js:189:40)
    at XBeeAPI.<anonymous> (/home/user/node_modules/xbee-api/lib/xbee-api.js:126:10)
    at Object.SerialPort.options.dataCallback (/home/user/node_modules/serialport/serialport.js:179:15)
    at SerialPortFactory.SerialPort._emitData (/home/user/node_modules/serialport/serialport.js:362:20)
    at afterRead (/home/user/node_modules/serialport/serialport.js:338:20)
    at /home/user/node_modules/serialport/serialport.js:354:9

The serial port test program shows output:

Port open
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 31 2f 00 ff 04 b5 47 01 06 04 00 04 03 ff 03 ff 0a>
<Buffer 7e 00 12 83 00 31>
<Buffer 2f 00 ff 04 b5 47 01 06 04 00 04 03>
<Buffer ff 03 ff 0a>
<Buffer 7e 00 12 83>
<Buffer 00 31 2f 00 ff 04>
<Buffer b5 47 01 06 04 00>
<Buffer 04 03 ff 03 ff 0a>
<Buffer 7e 00 12>
<Buffer 83 00 31 2f 00 ff>
<Buffer 04 b5 47 01 06 04>
<Buffer 00 04 03 ff 03>
<Buffer ff 0a>
<Buffer 7e 00 12>
<Buffer 83 00 31 2f 00>
<Buffer ff 04 b5 47 01 06 04>
<Buffer 00 04 03 ff 03>
<Buffer ff 0a>

The difference is that this time the leading zeros will not go away and they occur every time when you run the app or the test program.

Anyone tried it with Encryption on with XBee Series 1 and get successful data output?

@jankolkmeier
Copy link
Owner

I never used encryption, so I cannot report on that, but in principle, it shouldn't matter.

It might be that the issue is related to a bug/curiosity in serialport. I do not think that writing any zero bytes just like that is part of the xbee specs.

Far fetched, but maybe it is an issue with the serial 2 usb converter you are using? Are you using an official shield with FTDI? Maybe a third party shield? @duffmeister @infomk did you happen to use something unusual?

Regardless, a fix would be something inside the parseRaw function that prevents writing to the working buffer unless we are inside a package, after we received a start byte. Could you check out if 889e588
fixes the issue?

@xianlin
Copy link

xianlin commented Apr 15, 2015

@jankolkmeier , Thank you and the Range error goes away by using the above fix
But the Assertions Error still persists:

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: Out of Original Buffer's Boundary
    at BufferReader.MAKE_NEXT_READER.BufferReader.(anonymous function) [as nextUInt16BE] (/home/user/node_modules/xbee-api/node_modules/buffer-reader/index.js:79:9)
    at Object.frame_parser.Recieved16BitPacketIO (/home/user/node_modules/xbee-api/lib/frame-parser.js:241:40)
    at Object.frame_parser.(anonymous function) [as 131] (/home/user/node_modules/xbee-api/lib/frame-parser.js:298:16)
    at XBeeAPI.parseFrame (/home/user/node_modules/xbee-api/lib/xbee-api.js:113:27)
    at XBeeAPI.parseRaw (/home/user/node_modules/xbee-api/lib/xbee-api.js:198:26)
    at XBeeAPI.<anonymous> (/home/user/node_modules/xbee-api/lib/xbee-api.js:133:10)
    at Object.SerialPort.options.dataCallback (/home/user/node_modules/serialport/serialport.js:179:15)
    at SerialPortFactory.SerialPort._emitData (/home/user/node_modules/serialport/serialport.js:362:20)
    at afterRead (/home/user/node_modules/serialport/serialport.js:338:20)
    at /home/user/node_modules/serialport/serialport.js:354:9

I have put the console.log code into node_modules/buffer-reader/index.js line 74 onwards:

function MAKE_NEXT_READER(valueName, size) {
    valueName = cap(valueName);
    BufferReader.prototype['next' + valueName] = function() {
        // added debug code
        console.log(this.buf);
        console.log("Offset: " + this.offset + " Size: " + size + " Buf Length: " + this.buf.length);
        console.log("");

        assert(this.offset + size <= this.buf.length, "Out of Original Buffer's Boundary");
        var val = this.buf['read' + valueName](this.offset);
        this.offset += size;
        return val;
    };
}

Here are the outputs:

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 0 Size: 1 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 3 Size: 1 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 4 Size: 1 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 5 Size: 1 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 6 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 8 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 10 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 12 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 14 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 16 Size: 2 Buf Length: 18

<Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>
Offset: 18 Size: 2 Buf Length: 18

Clearly when Offset=18 + Size=2 == 20 > Length=18 at the last row, the error will be triggered.

My serialport code still produces the below data from the serial port:

Port open
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 7e 00 12 83 00 31 2c 00 ff>
<Buffer 04 b5 47>
<Buffer 01 06 04 00 00 02 11 00>
<Buffer ee 14>
<Buffer 7e 00 12 83 00 31 2c>
<Buffer 00 ff 04 b5>
<Buffer 47 01 06 04 00 00>
<Buffer 02 13 00 ed 13>
<Buffer 7e 00 12 83 00>
<Buffer 31 2c 00 ff 04 b5 47 01 06 04 00 00 02 18 00>
<Buffer ed 0e>
<Buffer 7e 00 12 83 00 31>
<Buffer 2c 00 ff 04 b5>
<Buffer 47 01 06 04 00 00>
<Buffer 02 15 00 ed 11>
<Buffer 7e 00 12 83>
<Buffer 00 31 2c 00 ff 04>
<Buffer b5 47 01 06 04 00>
<Buffer 00 02 19 00 ed 0d>

Then I disabled the Encryption and here are the results:

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 0 Size: 1 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 3 Size: 1 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 4 Size: 1 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 5 Size: 1 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 6 Size: 2 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 8 Size: 2 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 10 Size: 2 Buf Length: 14

<Buffer 83 00 31 2c 00 01 06 04 00 00 02 23 00 ed>
Offset: 12 Size: 2 Buf Length: 14

>> { type: 131,
  remote16: '0031',
  rssi: 44,
  receiveOptions: 0,
  data:
   { sampleQuantity: 1,
     channelMask: 1540,
     channels: { ADC0: 1, ADC1: 1, DIO2: 1 },
     analogSamples: [ [Object] ],
     digitalSamples: [ '0' ] } }

The last row is 14=14, not exceeding 14 bytes, the difference between encryption and not encryption is 4 bytes.

I hope this can help further debug the problem cause I have no clue why the code cannot parse this 18 byte buffer <Buffer 83 00 31 2d 00 ff 04 b5 47 01 06 04 00 00 02 15 00 ee>

@jankolkmeier
Copy link
Owner

I briefly read into encryption. I am a bit confused. The xbee specs say that a frame send with encryption would carry a 0x20 byte in the transmit options, however none of the buffers you reported contain a 0x20 byte.

You are using series 1, right?
Are you operating in the right AP mode? (escaping disabled/enabled)?

Could you send me the complete frame that causes the issue, from start delimiter (0x7e) to checksum? And maybe a complete frame that does work?

@xianlin
Copy link

xianlin commented Apr 22, 2015

@jankolkmeier , here are my complete frame that causes the issue:

Serial Port Opend
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7e 00 12 83 00 31 2c 00 ff 04 b5 47 01 06 04 00 00>
<Buffer 01 dd 00 f6 41>
<Buffer 7e 00>
<Buffer 12 83 00 31 2c 00>
<Buffer ff 04 b5 47 01>
<Buffer 06 04 00 00 01 de>
<Buffer 00 f6 40>
<Buffer 7e 00 12 83 00>
<Buffer 31 2c 00 ff 04>
<Buffer b5 47 01 06 04 00>
<Buffer 00 01 dd 00 f6 41>
<Buffer 7e>
<Buffer 00 12 83 00 31 2c>
<Buffer 00 ff 04 b5 47>
<Buffer 01 06 04 00 00 01>
<Buffer dd 00 f5 42>
<Buffer 7e 00 12 83 00>
<Buffer 31 2c 00 ff 04 b5>
<Buffer 47 01 06 04 00 00>
<Buffer 01 dd 00 f5 42>

If I run my customized parser code I will get Assertion error:

Error:

assert.js:93
  throw new assert.AssertionError({
        ^
AssertionError: Out of Original Buffer's Boundary
    at BufferReader.MAKE_NEXT_READER.BufferReader.(anonymous function) [as nextUInt16BE] (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/node_modules/buffer-reader/index.js:77:9)
    at Object.frame_parser.Recieved16BitPacketIO (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/lib/frame-parser.js:243:40)
    at Object.frame_parser.(anonymous function) [as 131] (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/lib/frame-parser.js:300:16)
    at XBeeAPI.parseFrame (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/lib/xbee-api.js:113:27)
    at XBeeAPI.parseRaw (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/lib/xbee-api.js:198:26)
    at XBeeAPI.<anonymous> (/home/xianlin/Code/JS/XBee/node_modules/xbee-api/lib/xbee-api.js:133:10)
    at Object.SerialPort.options.dataCallback (/home/xianlin/Code/JS/XBee/node_modules/serialport/serialport.js:179:15)
    at SerialPortFactory.SerialPort._emitData (/home/xianlin/Code/JS/XBee/node_modules/serialport/serialport.js:362:20)
    at afterRead (/home/xianlin/Code/JS/XBee/node_modules/serialport/serialport.js:338:20)
    at /home/xianlin/Code/JS/XBee/node_modules/serialport/serialport.js:354:9

The parser code is simply as:

var util = require('util')
var SerialPort = require("serialport").SerialPort
var xbee_api = require('xbee-api');

var C = xbee_api.constants;

var xbeeAPI = new xbee_api.XBeeAPI({
  api_mode: 1
});

var serialPort = new SerialPort("/dev/ttyUSB1", {
  baudrate: 57600,
  parser: xbeeAPI.rawParser()
});

// All frames parsed by the XBee will be emitted here
xbeeAPI.on("frame_object", function(frame) {
    console.log(">>", util.inspect(frame));
});

When I disabled the Encryption (EE=0), here are the complete frame that does the work.

Complete Frame Without Issue:

Serial Port Opend
<Buffer 7e 00 0e>
<Buffer 83 00 31 2b 00 01 06 04 00 00 01 c5>
<Buffer 00 f2 5d>
<Buffer 7e 00 0e>
<Buffer 83 00 31 2c 00 01>
<Buffer 06 04 00 00 01 c6>
<Buffer 00 f2 5b>
<Buffer 7e 00 0e>
<Buffer 83 00 31 2c 00 01>
<Buffer 06 04 00 00 01 c6>
<Buffer 00 f2 5b>
<Buffer 7e 00>
<Buffer 0e 83 00 31 2c 00>
<Buffer 01 06 04 00 00 01>
<Buffer c6 00 f2 5b>
<Buffer 7e 00>
<Buffer 0e 83 00 31 2c 00>
<Buffer 01 06 04 00 00 01>
<Buffer c6 00 f3 5a>
<Buffer 7e 00 0e 83 00 31>
<Buffer 2c 00 01 06 04 00>
<Buffer 00 01 c6 00 f2 5b>

After Parser:

>> { type: 131,
  remote16: '0031',
  rssi: 44,
  receiveOptions: 0,
  data:
   { sampleQuantity: 1,
     channelMask: 1540,
     channels: { ADC0: 1, ADC1: 1, DIO2: 1 },
     analogSamples: [ [Object] ],
 digitalSamples: [ '0' ] } }
>> { type: 131,
  remote16: '0031',
  rssi: 44,
  receiveOptions: 0,
  data:
   { sampleQuantity: 1,
     channelMask: 1540,
     channels: { ADC0: 1, ADC1: 1, DIO2: 1 },
     analogSamples: [ [Object] ],
     digitalSamples: [ '0' ] } }
>> { type: 131,
  remote16: '0031',
  rssi: 44,
  receiveOptions: 0,
  data:
   { sampleQuantity: 1,
     channelMask: 1540,
     channels: { ADC0: 1, ADC1: 1, DIO2: 1 },
     analogSamples: [ [Object] ],
     digitalSamples: [ '0' ] } }

I am using Series 1, AP mode = 2 (escaping enabled).
I don't know if there is a 0x20 byte for encryption, I only know 0x20 is for escaping XOR operation?

The serial port read code is as the below (Am I getting the full frame by using the below code?):

var SerialPort = require("serialport").SerialPort;

var serialport = new SerialPort("/dev/ttyUSB1", {
    baudrate: 57600
});

serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data);
  });
});

@jankolkmeier
Copy link
Owner

My mistake, the 0x20 is only in the series 2 transmit status fields!
Thank you for the frames, I'll have a look later if I can figure out what is going wrong!

@jankolkmeier
Copy link
Owner

The error is caused because the frames with encryption have a value of 0xff in the 9th byte. This indicates that there are 255 samples (instead of 0x01=one as in the frames without encryption).
This really has me puzzled, as there is nothing in the specs that explain this value.

The spec does mention that encrypted packets always use their 64 address as the src address for RF packets - but trying that gives an even less meaningful interpretation of the packet...

At this point, I'm clueless, sorry :(

@xianlin
Copy link

xianlin commented Apr 23, 2015

So the problem is that the RF Data part I got under Encryption mode is different from the Non-Encryption mode. It has 4 more bytes.

By right, the RF data content should remain the same content with OR without encryption. But the below results said NO and it puzzled us.

API Mode 2, 802.15.4 Series 1, firmware 10e6

0x83: Recieved Packet 16-bit Address I/O without encryption

Frame: 7E 00 0A 83 00 10 2D 00 01 02 00 01 CC 6F
RF Data: 01 02 00 01 CC


0x83: Recieved Packet 16-bit Address I/O with encryption

Frame: 7E 00 0E 83 00 10 2C 00 FF 04 B5 47 01 02 00 01 CC 71
RF Data: FF 04 B5 47 01 02 00 01 CC

Well, I will ask this on Digi forum and hopefully someone will answer it. Thanks. @jankolkmeier .

@jankolkmeier
Copy link
Owner

Keep me posted! :)

@nzfarmer1
Copy link

This may help in the interim:

Line 146:
if (!S.waiting && S.offset <128) { // Add range checking
S.buffer.writeUInt8(S.b, S.offset++);
}

@jankolkmeier
Copy link
Owner

did you ever figure this out, @xianlin ?

@Dowster64
Copy link

Dowster64 commented Jun 13, 2022

@jankolkmeier, this may be related and I know the cause of the problem. I have XBee3 running 802.15.4 firmware. API mode 1 and no encryption. If I configure 2 digital inputs with a mask and 1 analogue channel with a sample rate of 10 sec, I get two different frame lengths (12 and 10) depending on whether the analog sample is present or not (last 2 bytes), However the channel masks are the same regardless, so the frame parser tries to read the last two bytes (the ADC channel) but this is past the buffer length:

7E 00 0C 83 AF 2E 4A 00 01 08 03 00 03 00 00 46
7E 00 0A 83 AF 2E 48 00 01 08 03 00 01 4A
7E 00 0A 83 AF 2E 4B 00 01 08 03 00 03 45
7E 00 0C 83 AF 2E 4D 00 01 08 03 00 03 00 00 43
7E 00 0A 83 AF 2E 4D 00 01 08 03 00 01 45
7E 00 0A 83 AF 2E 4C 00 01 08 03 00 03 44
7E 00 0C 83 AF 2E 49 00 01 08 03 00 03 00 01 46
7E 00 0A 83 AF 2E 4A 00 01 08 03 00 01 48
7E 00 0A 83 AF 2E 4D 00 01 08 03 00 03 43

Hope this helps someone. The fix should be easy but I am not sure how to apply it

@jankolkmeier
Copy link
Owner

Hi @Dowster64 , thanks for chiming in here, and especially for providing sample packages. I've taken a look, and checked back in the documentation. See pages 113 & 114 here. The 9th byte (offset=8) indicates The number of sample sets included in the payload. According to the channel mask, your package should contain digtial io data (bits 0 and 1) as well as analog data (adc2). As you mention yourself, there is no difference in the channel mask between the messages, and in both the 9th byte is set to 1.

Given that information from channel mask & number of samples sets byte, the parser must expect to get both the 16 bit digital samples (bytes 12 & 13) and one 16 bit analog sample (bytes 14 & 15). So there is no way of knowing for the shorter packages whether byte 14 is part of the expected analog sample or an early checksum.

Unfortunately, the best I can make of this is that your device is not behaving according to specification. What's more, the documentation says that these packages (0x83 16-bit Address IO Samples) are legacy, and instead 0x92 should be used. Perhaps that's a solution?

@Dowster64
Copy link

Hi @jankolkmeier, thanks for the reply. I've made a change to frame-parser.js at the the end of this comment that makes the code work. Regarding whether it should or shouldn't be a valid frame might be moot, because these are the frames that an Xbee makes. These are TH XBee3 with 802.15.4 200D firmware. The frames above were copied from XCTU as they came in.
The length of the frame is presented in the header? of the frame - the next two bytes after 7E.

The Legacy setting in the firmware (AO) is the defualt for 802.15.4 and it seemed to be for 0x80/0x81. Although I can check the other frame type but it does look like I would need to use the full 64bit addresses. I find the MY address very useful.
I see you've linked to the S1 documentation - I'll look and see if the latest 802.15.4 XBee3 (S3) documentation is any different

BTW the checksum and the first 3 bytes are removed by the software prior to the parser and the length of the whole frame is known from the 2nd two bytes?

Thanks for yoiur very quick reply :-)

for( var si=0; si < data.sampleQuantity; si++ ){
var sample = {};
for( var j=0; j <= 5; j++ ){
if( data.channels['ADC'+j] ){
// starts at the 7th byte and moved down by the Digital Samples section
// Check that there are bytes in the buffer to read
if( reader.offset != reader.buf.length ) {
sample['ADC'+j] = reader.nextUInt16BE();
}
}
}
data.analogSamples.push(sample);
}

@Dowster64
Copy link

@jankolkmeier . Using the 0x92 frame (by changing the AO setting to 0 - API Rx Indicator only necessary on the transmitter) and leaving 16 bit addressing as is, packets are generated like this. Here we see that the analog sample mask is correctly set:
7E 00 12 92 FF FF FF FF FF FF FF FF AF 2E 00 01 00 03 00 00 03 91
7E 00 14 92 FF FF FF FF FF FF FF FF AF 2E 00 01 00 03 04 00 03 00 00 8D
7E 00 12 92 FF FF FF FF FF FF FF FF AF 2E 00 01 00 03 00 00 01 93
7E 00 12 92 FF FF FF FF FF FF FF FF AF 2E 00 01 00 03 00 00 03 91
7E 00 14 92 FF FF FF FF FF FF FF FF AF 2E 00 01 00 03 04 00 03 00 00 8D
(Copied from XCTU)
If 16 bit addressing is used and the defaults for AO are left (Legacy 802.15.4 API indicator -2) and the XBee is configured as I have done (a valid configuration), then the 0x83 frame type can be sent without analog data but with the Analog channel mask set. This looks like a bug (or a feature!) of the XBee3 TH 802.15.4 200D firmware. So I would imagine that all XBees don't perform to specification ...

If 16 bit addressing is used and AO is set to 0, then 0x92 frames are generated which set the Analog channel mask consistent with the sample data.
And, of course xbee-api.js works fine :-)

Thanks for your help.

@jankolkmeier
Copy link
Owner

I see your point about "it happens on my real hardware so it should be fixed". But in general, it seems best to implement stuff according to the datasheet rather than according to undocumented behavior... And everything seems to point to that "legacy behavior" being undocumented...

Your suggested workaround (i.e. using the package size/checking if there is more data) is fine, but in a way just passes the same issue (inconsistency between channel mask & actual included samples) down to the user (depending in their implementation, now they have to add another check for whether an expected entry in the samples is actually there or not). A proper "fix" or workaround would be to also correct the channel mask & other properties derived from it, which also feels weird. So I'm hesitant to just include that as a half-fix.

Anyway, glad you got it working now and thanks for sharing the details. This might help others down the line.

@Dowster64
Copy link

Dowster64 commented Jun 13, 2022

Hi @jankolkmeier . Some very revealing information! (well at least I'm happy and as you say I hope this helps others that find this). I do have a one TH S1 XBee running XBEE 802.15.4 function set version 10ef. With the same configuration as before, I would be confident in saying that this firmware behaves as documented, and that the the behaviour of the XBee3 with 802.15.4 is not as documented (exepected).
Here are a few RX (Receive) Packet 16-bit Address IO from the S1 XBee, as copied from XCTU:
7E 00 0A 83 5A 86 39 00 01 00 03 00 01 5E
7E 00 0C 83 5A 86 39 00 01 08 03 00 01 03 FF 54
7E 00 0A 83 5A 86 39 00 01 00 03 00 03 5C
7E 00 0A 83 5A 86 39 00 01 00 03 00 01 5E
7E 00 0C 83 5A 86 39 00 01 08 03 00 01 03 FF 54
7E 00 0A 83 5A 86 38 00 01 00 03 00 03 5D
So the S1 with the listed firmware correctly sets the Analog channel mask (as expected). XBee3 with the listed firmware doesn't change the Analogue channel mask. XCTU, with the XBee3 'handles' the error, potentially by just not reading beyond the end of the frame regardless of the frame's 'instructions'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants