Package Exports
- raw-device
- raw-device/raw.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (raw-device) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Introduction
raw-device is base module to remotely control devices using LAN/TCP or RS-232/serial connections.
The device control protocal must be based on sending and receiving sequence of bytes/ASCII characters in request-response schema which is a common for communication protocols.
A base object RAW is thought to be a prototype for other, more specific communication objects, but can be also used as stand-alone, full-functional object for simple scenarios.
Main features
- different modes of connect-disconnect cycle
- requests queuing and timing management
- events driven
- easy extensible
- response data auto management
Usage
const RAW = require('raw-device');
//send-receive data for TCP socket device
const dev1 = new RAW({host: '192.168.4.31', port: 9761});
dev1.emitter.on('device_data', data => console.log(data));
dev1.process('fa 01 00 ff\n');
//send data for Serial device
const dev2 = new RAW({path: 'com2'});
dev2.process('00x00x00x00x14x60', '3Bx78x5D');RAW Object
The primary exported object is RAW, which you'll use directly to communicate with serial or tcp devices or use as prototype for you own objects. This section covers direct use.
Constructor new RAW(AddressObject, OptionsObject)
AddressObject <Object>name <string>default: 'RAWdevice'
//for serialpath <string>Use valid serial path available in system. In Windows it is com1, com2 etc. In Linux it is /dev/ttyS0, /dev/ttyS1 and so on. It is required and has no default value.baudRate <number>default 9600dataBits <number>default 8parity <string>default 'none'stopBits <number>default 1
//for tcphost <string>Use valid IP address. No default value, it is requiredport <number>default 23
OptionsObject <Object>encoding <string>- default 'ASCII'duration <number>default 1500 ms. Inter-command period [ms]. A time for device to process command and to prepare and send a response.disconnect <boolean|number>default true. Connecion cycle scheme. Use true, false or timeout[ms]. True means close connection when command queue is empty, false means do not close connection, number means close connection after time of connection inactivity.splitter <Stream>transform stream to merge incoming data chunks and split data into valid responses. Only single property among delimiter, regex, timeout can be useddelimiter <string>default '\r\n' Split incoming data into valid responses using string delimiterregex <Regex>efault /[\r\n]+/. Split incoming data using regular expressiontimeout <number>default 1100 ms. Timer based splitting. Response is completed if inter-byte time is longer then timeout. Please consider that timeout must be shorter than duration (inter-command period) and disconnect time (if disconnect use timeout scheme)
Method process(...commands)
Commands can be regular strings or hexadecimal encoded strings. Hexadecimal strings can use x, :(colon) or -(minus) as separator.
Internal commands
There are some internal commands which starts with #. They are not sent to device, but are processed by RAW itself.
#pause number- Append additional pause between neighboring commands as number of miliseconds.
Event: responseFromDevice
Emited when device response is properly decoded.
response <Object>dev <string>- device nameraw <Buffer>- not decoded raw responsevalue <string>- response decoded to string value
Event: commandForDevice
Emited when command is properly encoded and sent to device. Of course only encoded property is sent to device itself.
command <Object>name <string>- device namecommand <string>- a command itself, not parsed or encodedencodedstr <string>- command encoded as stringencoded <Buffer>- command encoded as Bufferduration <number>- time [ms] for device to process the command (and generate a possible response).
Event: 'connectionData'
A data which comes directly from device port "as is". Not decoded, merged or chopped by splitter.
data <Buffer>
Event: 'connectionStatus'
Emited when device connection status changes.
statusObj <Object>dev <string>- device nameaddress <string>- device address as stringstatus <string>- connection statusmore <string|Object>- additional status information