wilton.js  v202103141
 All Namespaces Functions
Functions
Serial Namespace Reference

wilton/Serial
Connect to hardware devices using serial protocol. More...

Functions

Undefined close (Function|Undefined callback)
 Close connection to device.
String read (Number length, Function|Undefined callback)
 Read data from device.
String readLine (Function|Undefined callback)
 Read single line of data from device.
Object Serial (Object options, Function|Undefined callback)
 Create Serial instance.
Number writeHex (String dataHex, Function|Undefined callback)
 Write specifed string in hexadecimal encoding to device.
Number writePlain (String data, Function|Undefined callback)
 Write specifed string to device.

Detailed Description

This module allows to interact with hardware devices using RS232 protocol.

Responses from device are returned encoded in hexadecimal encoding, use wilton/hex for decoding.

Underlying serial connection can be closed manually using close() method or it will be closed during the shutdown.

Usage example:

// open connection
var ser = new Serial({
port: "/dev/ttyUSB1",
baudRate: 4800,
parity: "NONE", // "EVEN", "ODD", "MARK", "SPACE"
byteSize: 8,
stopBitsCount: 1,
timeoutMillis: 500
});
// write request
var bytes_written1 = ser.writePlain("foo\r\n");
var bytes_written2 = ser.writeHex("66 6f 6f 0d 0a");
// read response
var respHex = ser.readLine();
// close connection
ser.close();

Function Documentation

Undefined Serial::close ( Function|Undefined  callback)

Closes Serial connection releases system resources. Devices not closed manually, will be closed automatically during the shutdown.

Parameters
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
String Serial::read ( Number  length,
Function|Undefined  callback 
)

Tries to read a specified amount of data from the device. Returned result can contain less data than requested. Returns empty string if no data is available and timeout is exceeded.

Uses timeoutMillis parameter (specified in constructor) as a timeout.

Parameters
lengthNumber amount of the data to read (in bytes)
callbackFunction|Undefined callback to receive result or error
Returns
String device response data in hexadecimal encoding, empty string on timeout
String Serial::readLine ( Function|Undefined  callback)

Tries to read a data from the device until line ending (\n or \r\n) will be read or timeout happens. Line ending is not included into result.

Returned result can contain less data than requested. Returns empty result if no data is available and timeout is exceeded.

Uses timeoutMillis parameter (specified in constructor) as a timeout.

Parameters
callbackFunction|Undefined callback to receive result or error
Returns
String device response data in hexadecimal encoding, empty string on timeout
Object Serial::Serial ( Object  options,
Function|Undefined  callback 
)

Creates Serial object instance and opens the underlying RS232 connection.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Object Serial instance

Options

  • port String serial port address (OS-dependent), examples: /dev/ttyUSB1, COM4, vid:pid
  • baudRate Number connection baud rate
  • parity String enables parity checking, supported values: NONE, EVEN, ODD, MARK, SPACE
  • byteSize Number number of data bits
  • stopBitsCount Number number of stop bits
  • timeoutMillis Number timeout for read and write operations (in milliseconds)
Number Serial::writeHex ( String  dataHex,
Function|Undefined  callback 
)

Writes specified hex-string to device, unlike writePlain() this function may be used to write abritrary (possibly binary) data.

Uses timeoutMillis parameter (specified in constructor) as a timeout.

Parameters
dataHexString string encoded as hexadecimal to write to device
callbackFunction|Undefined callback to receive result or error
Returns
Number number of bytes written to device
Number Serial::writePlain ( String  data,
Function|Undefined  callback 
)

Writes specified string to device converting the bytes of input string into hexadecimal encoding as-is without changing the UTF-16 encoding. Using this method with non-ASCII symbols may lead to unexpected results.

To write UTF-8 bytes to device use writeHex() in conjunction with hex.encodeUTF8().

Uses timeoutMillis parameter (specified in constructor) as a timeout.

Parameters
dataString string to write to device
callbackFunction|Undefined callback to receive result or error
Returns
Number number of bytes written to device