-
Notifications
You must be signed in to change notification settings - Fork 31
5. APIs and Events
Enable a serial port connecting to CC253X SoC with given spCfg.path
.
Arguments:
-
spCfg
(Object): Configuration of the seiralport instance. Accepted properties ofspCfg
are listed in the following table.Property Type Description path String System path of the serial port to open. e.g., /dev/ttyUSB0
.options Object Port configuration options. -
callback
(Function):function (err) { ... }
Returns:
- none
Examples:
var znp = require('cc-znp');
var spCfg = {
path: '/dev/ttyUSB0',
options: {
baudrate: 115200,
rtscts: true
}
};
znp.init(spCfg, function (err) {
if (err)
console.log(err);
});
This method will close the opened serial port.
Arguments:
-
callback
(Function):function (err) { ... }
Returns:
- none
Examples:
znp.close(function (err) {
if (err)
console.log(err);
});
Invoke TI Z-Stack Monitor and Test Commands.
Arguments:
-
subsys
(Number | String): Subsystem, i.e., set it to1
or'SYS'
to invoke a SYS command. -
cmdId
(Number | String): Command id of which subsys command you like to invoke. See Z-Stack MT API Reference Tables Section. -
valObj
(Object): An argument object of the command. -
callback
(Function):function (err, result) { ... }
. Get called when the 'SRSP' is received.
-
Subsystem
{ "SYS": 1, "MAC": 2, "NWK": 3, "AF": 4, "ZDO": 5, "SAPI": 6, "UTIL": 7, "DBG": 8, "APP": 9 }
Returns:
- none
Examples:
// example of calling ping() from the subsystem 'SYS'
znp.request('SYS', 'ping', {}, function (err, result) {
if (err)
console.log(err);
else
console.log(result); // { capabilities: 121 }
});
// use numbered ids. The first 1 is subsystem 'SYS', and the second 1 is command 'ping'
znp.request(1, 1, {}, function (err, result) {
if (err)
console.log(err);
else
console.log(result); // { capabilities: 121 }
});
Request Shorthands:
- .sysRequest(cmdId, valObj, callback)
- .macRequest(cmdId, valObj, callback)
- .nwkRequest(cmdId, valObj, callback)
- .afRequest(cmdId, valObj, callback)
- .zdoRequest(cmdId, valObj, callback)
- .sapiRequest(cmdId, valObj, callback)
- .utilRequest(cmdId, valObj, callback)
- .dbgRequest(cmdId, valObj, callback)
- .appRequest(cmdId, valObj, callback)
Examples:
// examples with shorthands
znp.sysRequest('ping', {}, function (err, result) {
console.log(result); // { capabilities: 121 }
});
znp.utilRequest('setPanid', { panid: 0xFFFF }, function (err, result) {
console.log(result); // { status: 0 }
});
Exports the dependencies Unpi send() API, you can take it to send some binary data.
Arguments:
-
type
(Number | String): The command type. For exmaple, settype
to1
or'SREQ'
to send a synchronous command. -
subsys
(Number | String): The subsystem. For example, send a command of subsystem 'RPC_SYS_UTIL', you can setsubsys
to7
,'RPC_SYS_UTIL'
, or simply'UTIL'
(the prefix'RPC_SYS_'
can be ignored). -
cmdId
(Number): The command id which is a number according to which subsystem you are using. -
payload
(Buffer): The data payload.
Returns:
- (Buffer): Buffer of the built command packet.
Examples:
// The following calls do the same thing
znp.send('AREQ', 'SYS', 0, new Buffer([ 0 ]));
znp.send('AREQ', 1, 0, new Buffer([ 0 ]));
znp.send(2, 'SYS', 0, new Buffer([ 0 ]));
znp.send(2, 1, 0, new Buffer([ 0 ]));
'ready' event will be fired when the initializing procedure accomplishes.
Examples:
znp.on('ready', function () {
console.log('Initialization completes.');
});
Fired when the serial port is closed.
Examples:
znp.on('close', function () {
console.log('The serialport is closed.');
});
The 'data' event will be fired along with the parsed result. Here is an example of the parsed data to show you the format. The csum
is the checksum calculated from the received binaries. You can use it to check that if the received packet is corrupted.
{
sof: 254,
len: 6,
type: 2,
subsys: 1,
cmd: 128,
payload: <Buffer 02 02 00 02 06 02>,
fcs: 193, // this is the checksum originated from the sender
csum: 193 // this is the checksum calculated from the received binaries
}
Examples:
znp.on('data', function (data) {
console.log(data); // The parsed data
});
When there is an 'AREQ' message coming from ZNP, an 'AREQ' event will be fired along with the message content. Here is an example of the msg format.
{
subsys: 'ZDO',
ind: 'endDeviceAnnceInd',
data: { srcaddr: 63536, nwkaddr: 63536, ieeeaddr: '0x00124b0001ce3631', capabilities: 142 }
}
Examples:
znp.on('AREQ', function (msg) {
console.log(msg);
});