You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var util = require('util');
var SerialPort = require('serialport');
var xbee_api = require('xbee-api');
var C = xbee_api.constants;
var xbeeAPI = new xbee_api.XBeeAPI({
api_mode: 2
});
var serialport = new SerialPort("/dev/ttyS0", {
baudrate: 9600,
parser: xbeeAPI.rawParser()
});
var d= new Date();
var yyyy =d.getFullYear()
var MM = (d.getMonth()+1<10 ? '0' : '')+(d.getMonth()+1);
var dd = (d.getDate()<10 ? '0' : '')+d.getDate();
var h = (d.getHours()<10 ? '0' : '')+d.getHours();
var m = (d.getMinutes()<10 ? '0' : '')+d.getMinutes();
var s = (d.getSeconds()<10 ? '0' : '')+d.getSeconds();
serialport.on("open", function () {
serialport.write(xbeeAPI.buildFrame(frame_obj_ack));
console.log('Sent to serial port.');
});
*/
// All frames parsed by the XBee will be emitted here
xbeeAPI.on("frame_object", function (frame) {
var d= new Date();
var yyyy =d.getFullYear()
var MM = (d.getMonth()+1<10 ? '0' : '')+(d.getMonth()+1);
var dd = (d.getDate()<10 ? '0' : '')+d.getDate();
var h = (d.getHours()<10 ? '0' : '')+d.getHours();
var m = (d.getMinutes()<10 ? '0' : '')+d.getMinutes();
var s = (d.getSeconds()<10 ? '0' : '')+d.getSeconds();
感謝使用 Node.js Taiwan AMA,以下附上簡單提問範例供參考,請把內容改成你自己遇到的問題
目的
在xbeeapi.on接收封包的同時
能夠週期性的發送另外的封包
使用的工具
在Pi3上安裝node.js v6.9.5
透過tx/rx連結xbee coordinator
操作流程
xbeeApi.on負責接收封包並根據來源節點地址回送ack及資訊
但想要再加入另外週期發送封包的動作
遇到的問題
若放入xbeeApi.on中需待有資料進入才會執行該段動作
在外部的話則是會有port被lock住的問題
嘗試過的解法
同上
程式碼
var util = require('util');
var SerialPort = require('serialport');
var xbee_api = require('xbee-api');
var C = xbee_api.constants;
var xbeeAPI = new xbee_api.XBeeAPI({
api_mode: 2
});
var serialport = new SerialPort("/dev/ttyS0", {
baudrate: 9600,
parser: xbeeAPI.rawParser()
});
var d= new Date();
var yyyy =d.getFullYear()
var MM = (d.getMonth()+1<10 ? '0' : '')+(d.getMonth()+1);
var dd = (d.getDate()<10 ? '0' : '')+d.getDate();
var h = (d.getHours()<10 ? '0' : '')+d.getHours();
var m = (d.getMinutes()<10 ? '0' : '')+d.getMinutes();
var s = (d.getSeconds()<10 ? '0' : '')+d.getSeconds();
/*
var frame_obj_ack = {
type: 0x10,
id: 0x01,
destination64: "0013a20040d8da93" ,
broadcastRadius: 0x00,
options: 0x00,
// data: ""+h+m+s+dd+MM+yyyy
data:"hi"
};
serialport.on("open", function () {
serialport.write(xbeeAPI.buildFrame(frame_obj_ack));
console.log('Sent to serial port.');
});
*/
// All frames parsed by the XBee will be emitted here
xbeeAPI.on("frame_object", function (frame) {
var d= new Date();
var yyyy =d.getFullYear()
var MM = (d.getMonth()+1<10 ? '0' : '')+(d.getMonth()+1);
var dd = (d.getDate()<10 ? '0' : '')+d.getDate();
var h = (d.getHours()<10 ? '0' : '')+d.getHours();
var m = (d.getMinutes()<10 ? '0' : '')+d.getMinutes();
var s = (d.getSeconds()<10 ? '0' : '')+d.getSeconds();
console.log(">>", frame);
console.log( ""+h+"-"+m+"-"+s+"-"+dd+"-"+MM+"-"+yyyy);
var recadd = frame.remote64; //source address
console.log(recadd);
if(recadd!=null){
recadd = frame.remote64;
var frame_obj_ack = {
type: 0x10,
id: 0x01,
destination64: recadd ,
broadcastRadius: 0x00,
options: 0x00,
data: ""+h+m+s+dd+MM+yyyy
};
serialport.write(xbeeAPI.buildFrame(frame_obj_ack));
console.log(recadd);
});
錯誤
The text was updated successfully, but these errors were encountered: