V1.0 - 11 Jan 2018
listserialports() ;
get a list of all serial communication ports
INPUTS: `none
RETURNS: an array of tables, where each table holds description, port name and Hardware ID
THROWS: no
Each element of the returned array has three slots:
slot key | value |
---|---|
port | serial port name (e.g. "COM1" or "/dev/ttyS0") |
descr | a description text, depending on OS |
hwid | a hardware ID string, depending on OS |
Any class instance represents a serial port.
Note: A single port can not have more than one instance configured to it
Virtual prototype:
class DBICon {
constructor(port,timeout[,baud,[bits,[parity,[stopbits,[flowcontrol]]]]]);
function open();
function close();
function isopen();
function available();
function flush();
function read(bytecount[,timeout_ms]) ;
function readline([lineendstring,[timeout_ms]]) ;
function write(bufferobj) ;
function sendbreak(time_ms);
function setbreak(onoff);
function setDTR(onoff);
function setRTS(onoff);
function getCTS();
function getDSR();
function getRI();
function getCD();
function waitcontrol();
function setport(port);
function getport();
function settimeout(timeout_ms[,constanttimeout_ms]) ;
function gettimeout(which) ;
function setbaudrate(baud);
function getbaudrate();
function setbytesize(bitspercharacter);
function getbytesize();
function setparity(paritystring) ;
function getparity();
function setstopbits(stopbits);
function getstopbits();
function setflowcontrol(flowstring);
function getflowcontrol();
}
serialport(port,timeout[,baud,[bits,[parity,[stopbits,[flowcontrol]]]]])
where
port
port id string, as returned in the "port" slots of functionlistserialports
timeout
integer. read timeout in millisecondsbaud
integer baudrate (e.g.2400,9600,19200,57600,115200 and the like)bits
integer bits per character. Values of 5 to 8 allowed onlyparity
string. One ofEven
,Odd
,None
,Mark
orSpace
(or just the starting character of these)stopbits
integer or float. When integer, 10 times the stopbit count (e.g. 10,15 or 20), when float 1.0, 1.5 or 2.0 allowedflowcontrol
string, one ofNone
,Hardware
orSoftware
(or just the starting character of these)
The constructor will automatically open the port, and it will throw an exception on any error.
function open()
opens a closed port
INPUTS: none
RETURNS: none
THROWS: on error opening the port, even when already open!
function close()
closes an open port
INPUTS: none
RETURNS: none
THROWS: on error, including trying to close an already closed port
function isopen()
returns whether the port is open or closed
INPUTS: none
RETURNS: bool, true: port is open, false: port is closed
THROWS: no
function available()
returns the number of bytes in the read buffer available for reading
INPUTS: none
RETURNS: integer number of bytes available for reading
THROWS: when used on misconfigured port
function flush()
flushes all internal I/O buffers of the port
INPUTS: none
RETURNS: none
THROWS: on error
function read(bytecount[,timeout])
blocks until bytecount bytes read from serial port or timeout occurs. Optionally sets a different timeout
INPUTS:
bytecount
: integer, number of bytes to read before method returnstimeout
: integer, optional. Timeout in milliseconds. See note below
RETURNS:null
when no data was read or abuffer
instance holding all read data.
THROWS: on any error except timeouts
Notes:
- Any timeout is stored within the serialport instance and will apply to all further operations. Hence, when
timeout
is provided to read this is the same as:
settimeout(timeout); return read(bytecount);
- the buffer instance returned may hold less bytes than requested when some, but not sufficient data was available for reading until a timeout occured.
function readline([lineend[,timeout]])
blocks until a "line" is read from the serial port or a timeout occurs, wherebytecount bytes read from serial port or timeout occurs.
INPUTS:
lineend
: string, optional. A string determining the line ending. When omitted defaults to "\n" for Linux and "\r\n" for Windowstimeout
: integer, optional. Timeout in milliseconds. Also see notes forread
RETURNS:null
when no data was read at all, or a string holdign the line read from the serial port.
THROWS: on any error except timeouts
Note: The line can have a maximum length of 65535 characters!
function write(data)
writes data to the serial port
INPUTS:
data
: anybuffer
,sockbuf
orblob
instance, or any string
RETURNS::true
when all bytes were written, otherwise the integer number of bytes actually written
THROWS: on any error