Serial library

Provides functionality for serial port communication. Initialize:

var serial = require('serial');

Open serial port instance

openPort (portId, baudrate, cts_rts_control, bits_per_char, parity);

Function creates serial port instance. For example, on TC65i can be used COM0 or COM1.

String portId - specifies what port is used. Use COM0, COM1, USB0, USB1, USB3, USB4

Number baudrate - baudrate specifies communication speed. Use 1200, 2400, 4800, 9600, 19200 ... 115200

Boolean cts_rts_control - sets hardware flow control to ON|OFF. Software flow control is not supported!

Number bits_per_char - bits_per_char sets 7 or 8 bits per character.

String parity - parity specifies type of parity check. Use none,even or odd.

Timeouts

setCharTimeout (timeout);

Function sets an timeout how long it will be wait after last character incoming on serial port. When time is gone, modem invokes function receiveData().

Number timeout - Timeout is specified in milliseconds. Default value is 200 ms.

setTimeout (timeout);

Function sets an timeout how long it will be wait for response from function writeAndRead().

Number timeout - is specified in milliseconds. Default value is 1000 ms.

Write data to serial port

write (data);

ByteBuffer|String|Array data - Data can be type of String, ByteBuffer or Array.

Function writes data to serial port.

writeAndRead (data);

Function writes data to serial port and waits for response. Time for waiting is defined by setTimeout(). Function returns ByteBuffer with a response.

Receive incoming data from serial port

receiveData (data);

Function is invoked from modem when some data arrives on serial port. Timeout for detecting end of data is specified by setCharTimeout().

ByteBuffer data - incoming data from serial port.

Example Serial port

var sys = require('system');
var serial = require('serial');

//port id, baudrate, ctsrts control, bits per char (7,8), parity (none, even, odd)
var com0 = serial.openPort("com1",115200,false,8,"none");

//wait for response on serial port for 10 sec. This is for writeAndRead() function
com0.setTimeout(10000);

//wait after last received char for 100 ms for some next char
com0.setCharTimeout(100);

//when some data is received on serial port, this function will be called
//event from modem
com0.receivedData = function(data){
        //data is ByteBuffer object
        com0.write("\r\nI received this data:"+data.toString());
    }

var response = com0.writeAndRead("\r\nHello, is anybody here?\r\nPress any key please");
if (response==undefined) {
    com0.write("\r\nNobody is here :(\r\n");
} else {
    com0.write("\r\nYeah, welcome!\r\n");
}

results matching ""

    No results matching ""