Skip to content

realglobe-Inc/sugo-module-serialport

Repository files navigation

sugo-module-serialport

Build Status npm Version JS Standard

SUGOS module to access serial ports

This is a wrapper module of serialport. See serialport for method details.

Requirements

Node.js NPM

Installation

$ npm install sugo-module-serialport --save

Usage

Register module to SUGO-Actor

#!/usr/bin/env node

/**
 * Example usage to register module on actor
 * @see https://github.com/realglobe-Inc/sugo-actor
 */
'use strict'

const sugoModuleSerialport = require('sugo-module-serialport')
const sugoActor = require('sugo-actor')
const co = require('co')

co(function * () {
  let actor = sugoActor('http://my-sugo-cloud.example.com/actors', {
    key: 'my-serial-port-01',
    modules: {
      // Register the module
      serialport: sugoModuleSerialport({})
    }
  })
  yield actor.connect()
}).catch((err) => console.error(err))

Then, call the module from remote caller.

#!/usr/bin/env node

/**
 * Example to call from caller
 * @see https://github.com/realglobe-Inc/sugo-caller
 */
'use strict'

const co = require('co')
const asleep = require('asleep')
const sugoCaller = require('sugo-caller')

co(function * () {
  let caller = sugoCaller('http://my-sugo-cloud.example.com/callers', {})
  let actor = caller.connect('my-serial-port-01')

  // Access to the module
  let sp = actor.get('serialport')

  // get list
  let list = yield sp.list()
  // get port path on Mac
  let portPath = list.find((port) => port.comName.startsWith('/dev/cu.usbserial')).comName

  // open the port
  yield sp.connect(portPath, {
    baudRate: 57600
  })

  yield asleep(1000)
  yield sp.write(Buffer.from('#M6'))
  yield asleep(3000)
  yield sp.write(Buffer.from('#M0'))
  yield asleep(1000)
  yield sp.close()
}).catch((err) => console.error(err))

Methods

The following methods are available from remote callers for the module.

.ping(pong) -> string

Test the reachability of an module.

Param Type Description
pong string Pong message to return

.assert() -> boolean

Test if the actor fulfills system requirements

.list() -> array

Retrieves a list of available serial ports with metadata.

.connect(path, options)

Create a new connection

Param Type Description
path string path
options object options

.close()

Close the given serial port.

.drain()

Waits until all output data has been transmitted to the serial port.

.flush()

Flushes data received but not read.

.isOpen()

Returns true if the port is open.

.open()

Open a serial port.

.pause()

Pauses an open connection.

.resume()

Resumes a paused connection.

.set()

Sets flags on an open port.

.update()

Changes the baudrate for an open port.

.write(data)

Write data to the given serial port.

Param Type Description
data object Buffer data to write

Events

The following events my be emitted from the module.

Param Description
"open" the port is opened and ready for writing.
"close" the port is closed.
"disconnect" happen before a close event if a disconnection is detected.
"data" there is an data.
"error" there is an error.

License

This software is released under the MIT License.

Links

About

SUGOS module to access serial ports

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published