UART

Class: UART

The UART (Universal Asynchronous Receiver/Transmitter) class supports asynchronous serial communication.

uart.open(configuration, callback)

  • configuration {Object}
    • device {string} Mandatory configuration. The specified device path.(Linux, Nuttx and TizenRT only)
    • port {number} Mandatory configuration. The specified port number. (Tizen only)
    • baudRate {number} Specifies how fast data is sent over a serial line. Default: 9600.
    • dataBits {number} Number of data bits that are being transmitted. Default: 8.
  • callback {Function}.
    • err {Error|null}.
  • Returns: {UARTPort}.

Opens an UARTPort object with the specified configuration.

The baudRate must be equal to one of these values: [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400].

The dataBits must be equal to one of these values: [5, 6, 7, 8].

On NuttX, you also need to set the properties of the configuration in the NuttX configuration file. Using the NuttX menuconfig, it can be found at the Device Drivers -> Serial Driver Support -> U[S]ART(N) Configuration section.

You can read more information about the usage of the UART on stm32f4-discovery board: STM32F4-discovery.

Example

  1. var uart = require('uart');
  2. var configuration = {
  3. device: '/dev/ttyUSB0'
  4. baudRate: 115200,
  5. dataBits: 8,
  6. }
  7. var serial = uart.open(configuration, function(err) {
  8. // Do something.
  9. });
  10. serial.closeSync();

uart.openSync(configuration)

  • configuration {Object}
    • device {string} Mandatory configuration. The specified device path. (Linux, Nuttx and TizenRT only)
    • port {number} Mandatory configuration. The specified port number. (Tizen only)
    • baudRate {number} Specifies how fast data is sent over a serial line. Default: 9600.
    • dataBits {number} Number of data bits that are being transmitted. Default: 8.
  • Returns: {UARTPort}.

Opens an UARTPort object with the specified configuration.

Example

  1. var uart = require('uart');
  2. var configuration = {
  3. device: '/dev/ttyUSB0'
  4. baudRate: 115200,
  5. dataBits: 8,
  6. }
  7. var serial = uart.openSync(configuration);
  8. serial.closeSync();

Class: UARTPort

The UARTPort class is responsible for transmitting and receiving serial data.

uartport.write(data, callback).

  • data {string}.
  • callback {Function}.
    • err {Error|null}.

Writes the given data to the UART device asynchronously.

Example

  1. var serial = uart.openSync({device: '/dev/ttyUSB0'});
  2. serial.write('Hello?', function(err) {
  3. if (err) {
  4. // Do something.
  5. }
  6. serial.closeSync();
  7. });

uartport.writeSync(data)

  • data {string}.

Writes the given data to the UART device synchronously.

Example

  1. var serial = uart.openSync({device: '/dev/ttyUSB0'});
  2. serial.writeSync('Hello?');
  3. serial.closeSync();

uartport.close([callback])

  • callback {Function}.
    • err {Error|null)}.

Closes the UART device asynchronously.

uartport.closeSync()

Closes the UART device synchronously.

Event: ‘data’

  • callback {Function}
    • data {Buffer} A data from the sender.

Example

  1. /* ... */
  2. serial.on('data', function(data) {
  3. console.log('read result: ' + data.toString());
  4. });