/raspi-serial

Provides access to the serial port on the Raspberry Pi as part of the Raspi.js library suite

Primary LanguageJavaScriptMIT LicenseMIT

Raspi Serial

Gitter

Raspi Serial is part of the Raspi.js suite that provides access to UART ports, either the built-in port or a USB add-on port.

If you have a bug report, feature request, or wish to contribute code, please be sure to check out the Raspi IO Contributing Guide.

Installation

First, be sure that you have installed raspi.

Install with NPM:

npm install raspi-serial

Warning: this module requires GCC 4.8 or newer. This means that you should be running Raspbian Jessie or newer, released in September of 2015.

Example Usage

var raspi = require('raspi');
var Serial = require('raspi-serial').Serial;

raspi.init(function() {
  var serial = new Serial();
  serial.open(() => {
    serial.write('Hello from raspi-serial');
    serial.on('data', function(data) {
      process.stdout.write(data);
    });
  });
});

API

Module Constants

Constant Description
DEFAULT_PORT The port ID of the default serial port, equals `"/dev/ttyAMA0"`
PARITY_NONE Use no parity, one of five possible values for the parity property in the constructor options
PARITY_EVEN Use even parity, one of five possible values for the parity property in the constructor options
PARITY_ODD Use odd parity, one of five possible values for the parity property in the constructor options
PARITY_MARK Use mark parity, one of five possible values for the parity property in the constructor options
PARITY_SPACE Use space parity, one of five possible values for the parity property in the constructor options

new Serial(options)

Instantiates a new Serial instance with the given options, defaulting to the built-in UART port. Check the wiring information wiki for more information.

Arguments:

Argument Type Description
options (optional) Object The configuration options.
Property Type Description
port (optional) String The port to open, defaults to /dev/ttyAMA0
baudRate Number The baud rate, defaults to 9600
dataBits Number The number of data bits in a character, defaults to 8
stopBits Number The number of stop bits in a character, defaults to 1
parity PARITY_NONE | PARITY_EVEN | PARITY_ODD | PARITY_MARK | PARITY_SPACE The parity of a character, defaults to PARITY_NONE

Instance Properties

port

The serial port tied to this instance

Type: String

baudRate

The baud rate tied to this instance

Type: String

dataBits

The number of data bits tied to this instance

Type: String

stopBits

The number of stop bits tied to this instance

Type: String

parity

The parity tied to this instance

Type: String

Instance Methods

write(data, cb)

Write the given data to the serial port.

Arguments:

Argument Type Description
data Buffer | String The data to write to the port
cb (optional) Function The callback to call once writing is complete
Argument Type Description
err String | null The error, if one occurred, else null

Returns: None

flush(cb)

Flushes the serial port.

Arguments:

Argument Type Description
cb (optional) Function The callback to call once writing is complete
Argument Type Description
err String | null The error, if one occurred, else null

Returns: None

Events

on('data', function(data))

Fired whenever data is received on the serial port.

Callback Arguments:

Buffer
Argument Type Description
data The data read from the serial port.

License

The MIT License (MIT)

Copyright (c) 2016 Bryan Hughes (bryan@nebri.us)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.