我想让nodejs用xbee发送和接收消息。我知道xbee设置可以工作,因为我在x-ctu上测试了它。我尝试了以下方法,但没有收到消息。上面说它是开着的。
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);
});发布于 2014-06-06 23:58:14
我几天前就想明白了。我意识到我可以只使用串口库。
发布于 2021-10-21 09:08:56
您需要先侦听串口,然后使用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
}
}你需要处理的帧开关他的frame.type,是ZIGBEE_RECEIVE_PACKET的情况下你需要转换数据到字符串frame.data.toString(),我不知道为什么使用API1模式,但请尽量使用57600波特率或更高,以避免校验和不匹配的问题,祝你好运。
https://stackoverflow.com/questions/23975642
复制相似问题