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. |
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:
Undefined Serial::close | ( | Function|Undefined | callback | ) |
Closes Serial connection releases system resources. Devices not closed manually, will be closed automatically during the shutdown.
callback | Function|Undefined callback to receive result or error |
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.
length | Number amount of the data to read (in bytes) |
callback | Function|Undefined callback to receive result or error |
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.
callback | Function|Undefined callback to receive result or error |
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.
options | Object configuration object, see possible options below |
callback | Function|Undefined callback to receive result or error |
Object
Serial
instanceOptions
String
serial port address (OS-dependent), examples: /dev/ttyUSB1
, COM4
, vid:pid
Number
connection baud rateString
enables parity checking, supported values: NONE
, EVEN
, ODD
, MARK
, SPACE
Number
number of data bitsNumber
number of stop bitsNumber
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.
dataHex | String string encoded as hexadecimal to write to device |
callback | Function|Undefined callback to receive result or error |
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.
data | String string to write to device |
callback | Function|Undefined callback to receive result or error |
Number
number of bytes written to device