Use Node.js in your end devices(QQ: 796448809)
The following shows uart module APIs available for each platform.
Linux (Ubuntu) |
Raspbian (Raspberry Pi) |
NuttX (STM32F4-Discovery) |
TizenRT (Artik053) |
|
---|---|---|---|---|
uart.open | O | O | O | O |
uartport.write | O | O | O | O |
uartport.writeSync | O | O | O | O |
uartport.close | O | O | X | O |
uartport.closeSync | O | O | X | O |
The UART (Universal Asynchronous Receiver/Transmitter) class supports asynchronous serial communication.
Returns with a new UART object.
configuration
{Object}
device
{string} Mandatory configuration.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}. |
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
var Uart = require('uart');
var uart = new Uart();
var configuration = {
device: '/dev/ttyUSB0'
baudRate: 115200,
dataBits: 8,
}
var serial = uart.open(configuration, function(err) {
// Do something.
});
The UARTPort class is responsible for transmitting and receiving serial data.
data
{string}.callback
{Function}.
err {Error |
null}. |
Writes the given data
to the UART device asynchronously.
Example
serial.write('Hello?', function(err) {
if (err) {
// Do something.
}
serial.close();
});
data
{string}.Writes the given data
to the UART device synchronously.
Example
serial.writeSync('Hello?');
serial.close();
callback
{Function}.
err {Error |
null)}. |
Closes the UART device asynchronously.
On NuttX/STM32F4Discovery, Uart.close() blocks after close(). It seems that poll() does not work properly on NuttX for some cases.
Closes the UART device synchronously.
On NuttX/STM32F4Discovery, Uart.close() blocks after close(). It seems that poll() does not work properly on NuttX for some cases.
callback
{Function}
data
{string} A string from the sender.Example
/* ... */
serial.on('data', function(data) {
console.log('read result: ' + data.toString());
});