Recent Changes - Search:

edit SideBar

serial Module

This module provides an interface to serial ports.


The following functions are implemented by this module:

  • hostSerialPorts(callback): List the available serial ports. Invoke callback with this list.
  • SerialPort(portName, ownerName, timeout, options): Create a SerialPort object for connecting to the specified serial port.
    • portName: The name of the port to open. For example, '/dev/ttyUSB0'.
    • ownerName: The name of the owner assigned to this port, if opening is successful. Typically the accessor's name.
    • timeout: Time in milliseconds before failing.
    • options: Serial port options (FIXME: define).
  • SerialPort.close(): Close the serial port.
  • Open the serial port. The port must be explicitly opened; it will not open by default.
  • supportedReceiveTypes(): Return an array of supported receive data types.
  • supportedSendTypes(): Return an array of supported send data types.

This implementation uses the event emitter pattern common in JavaScript. The events emitted by this object include:

  • data: Emitted whenever new data is received. The handler will be passed the data as an argument.


Require the serial module:

   var serial = require('serial');

Running on CapeCode

Running on node.js

The SerialPort accessor can be invoked with the node host shell. Note: These instructions are for Linux.

Change to the accessors web directory:

   cd /accessors/web

Install serialport:

   npm install serialport

Change to the node host directory:

   cd /accessors/web/hosts/node

You may need to set a NODE_PATH environment variable:

   export NODE_PATH=/accessors/web/hosts/node/node_modules/@accessors-modules

Connect a device to the desired USB port.

Figure out the USB port name (TODO: how? Something we can call in node.js?)

Start the node host shell. Use sudo for permissions to access the USB port.

   sudo -E node nodeHostShell.js

Then, in the node host shell, instantiate the accessor:

   var a = instantiate('test', '/net/SerialPort');

Set the name of the port, using the name of your port:

   a.setParameter('port', '/dev/ttyUSB0');

Initialize the accessor:


Turn on your device to start sending data.

Cape Code Serial modules

  • $PTII/ptolemy/actor/lib/jjs/modules/IMUSensor
    • Uses the classes below:
  • $PTII/ptolemy/actor/lib/jjs/modules/serial
    • serial.js - The serial module implementation for Cape Code. Not included by any accessors, but used by Cape Code demos that use the JavaScript actor
    • test/auto/SerialHelloWorld.xml - a test that reads from two ports
  • $PTII/ptolemy/actor/lib/jjs/modules/xbee

Back to Optional JavaScript Modules

Edit - History - Print - Recent Changes - Search
Page last modified on February 24, 2017, at 09:37 PM