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

xbee-api cannot lock port #21

Open
fxsh1301 opened this issue Mar 24, 2017 · 0 comments
Open

xbee-api cannot lock port #21

fxsh1301 opened this issue Mar 24, 2017 · 0 comments

Comments

@fxsh1301
Copy link

感謝使用 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);

    }                
});

});

錯誤

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Error Resource temporarily unavailable Cannot lock port
    at Error (native)

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

1 participant