2

I want nodejs to send and receive messages with xbee. I know that the xbee setup works because I tested it on x-ctu. I tried the following but can't receive the message. It says it's open.

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("COM7", {
    baudrate: 9600,
    parser: xbeeAPI.parseRaw(1000)
});

serialport.on("open", function() {
    console.log("open");
});

// All frames parsed by the XBee will be emitted here
//I think this is the problem
xbeeAPI.on("frame_object", function(frame) {
    console.log(">>", frame);
});
user3167666
  • 144
  • 2
  • 6

2 Answers2

0

I figured it out a few day ago. I relized I could just use serial port library.

user3167666
  • 144
  • 2
  • 6
0

You need to listen to the serial port first and then parse the data with xbee-api

serialport.on('data', function (data) {
    try {
        xbeeAPI.parseRaw(data);
    } catch (e) {
        console.error(e);
    }
    xbeeAPI.on("frame_object", function (frame) {
        console.log(frame);
        // do what do you want with the frame
    }
}

You need to process the frame switch his frame.type, is case of ZIGBEE_RECEIVE_PACKET you need to convert data to string frame.data.toString(), i don't know why using API1 mode but please try to use 57600 baud-rate or higher to avoid the checksum mismatch problems Good luck.