UART

Universal Asynchronous Receiver/Transmitter(UART), is a common hardware interface and widely used for asynchronous communication of devices.

  • To know more about UART, please visit here.

Prerequisites

Using UART

Please read your datasheet to confirm the interface is GPIO.

Configuring driver.json

Please declare that your interface is UART in driver.json. The declaration should be inputs with UART as its type.

{
...
"inputs": {
"uart": {
"type": "uart",
"args": {
"baudRate": 57600,
"stopBits": 1,
"dataBits": 8,
"parity": "none",
"flowControl": "none"
}
}
}
}

Interface Properties

The following properties is accepted for UART:

  • baudRate - UART baud rate
  • stopBits - stop bits
  • dataBits- data bits
  • parity - set UART parity
    • none - no parity
    • odd - odd parity
    • even - even parity
  • flowControl - how the flow control works
    • none - no flow control
    • hard - hardware flow control
    • soft - software flow control

Writing a Driver

Reading Data

  • The driver can read data from UART. Here is an example:
var data=0;
...

function readContent(callback) {
uart.read(function (error, data) {
if (error) {
callback(error);
return;
}

callback(undefined, data);
});
}

Writing Data

The driver can also write data to UART. Here is an example:

function writeContent(data, callback) {
uart.write(data, callback);
}

Application

The following example will demostrate how to write a driver with UART interface. An UART interface will be declared in driver.json, with 19200 Bd/s baud rate, 1 stop bits, 8 data bits, no parity, and no flow control.

"models": [
"waveshare-fingerprint"
],
"inputs": {
"uart": {
"type": "uart",
"args": {
"baudRate": 19200,
"stopBits": 1,
"dataBits": 8,
"parity": "none",
"flowControl": "none"
}
}
}

We will provide two menthods for this module - write text and read text:

  • writeText, write text to UART
  • readText, read text from UART
'use strict';

var driver = require('ruff-driver');

module.exports = driver({
attach: function (inputs) {
this._uart = inputs['uart'];
},
exports: {
writeText: function (data, callback) {
this._uart.write(data, callback);
},

readText: function (callback) {
this._uart.read(function (error, data) {
if (error) {
callback(error);
return;
}

callback(undefined, data.toString());
});
}
}
});