Kaluma
Search…
UART
The uart module supports communication with computers or devices using serial ports (UART). Use require('uart') to access this module.

Class: UART

An instances of UART represents a UART port.

UART.PARITY_NONE

  • <number> = 0.
Do not use a parity bit.

UART.PARITY_ODD

  • <number> = 1.
Use odd parity bit

UART.PARITY_EVEN

  • <number> = 2.
Use even parity bit

UART.FLOW_NONE

  • <number> = 0.
Do not use flow control.

UART.FLOW_RTS

  • <number> = 1.
Use RTS flow control.

UART.FLOW_CTS

  • <number> = 2.
Use CTS flow control.

UART.FLOW_RTS_CTS

  • <number> = 3.
Use both RTS and CTS flow control.

new UART(port[, options])

  • port <number> UART port number. This value should be less than max port number.
  • options <object> The object of UART options. Default values are depends on board (Check in a board page).
    • baudrate <number> Baud rate. One of the [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400].
    • bits <number> Number of bits per a character. One of the [8, 9]. Default:8
    • parity <number>. The parity is one of the UART.PARITY_NONE (0), UART.PARITY_ODD (1), or UART.PARITY_EVEN (2). Default: UART.PARITY_NONE.
    • stop <number> Number of stop bits. One of the [1, 2]. Default: 1.
    • flow <number> Flow control. One of the UART.FLOW_NONE (0), UART.FLOW_RTS (1), UART.FLOW_CTS (2), or UART.FLOW_RTS_CTS (3). Default: UART.FLOW_NONE
    • bufferSize <number> The size of internal read buffer.
    • tx <number> UART TX pin number.
    • rx <number> UART RX pin number.
1
// Create the UART instance and close it
2
const {UART} = require('uart');
3
const options = {
4
baudrate: 9600,
5
bits: 8,
6
partity: UART.PARTIY_NONE,
7
stop: 1,
8
flow: UART.FLOW_NONE,
9
bufferSize: 2048
10
};
11
const serial0 = new UART(0, options);
12
// read or write data...
13
serial0.close();
Copied!

write(data[, options])

  • data <Uint8Array|string> Data to write.
  • options <object>
    • count <number> Indicates how many times to write data. Default: 1.
Writes data to the UART port.
1
const {UART} = require('uart');
2
const serial0 = new UART(0, {baudrate: 38400});
3
4
// Write a string
5
serial0.write('Hello, world\n');
6
7
// Write Uint8Array
8
serial0.write(new Uint8Array([0x00, 0x7f, 0x80, 0xff]));
9
10
serial0.close();
Copied!

close()

Close the UART port.

Event: 'data'

  • data <Uint8Array> Received data.
The data event is emitted whenever data is arrived (buffer size may varies).
1
const {UART} = require('uart');
2
const serial0 = new UART(0);
3
serial0.on('data', (data) => {
4
var s = String.fromCharCode.apply(null, data);
5
print(s);
6
});
Copied!
Last modified 7mo ago