SUGOS module to access serial ports
This is a wrapper module of serialport. See serialport for method details.
$ npm install sugo-module-serialport --save
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))
The following methods are available from remote callers for the module.
- .ping(pong) -> string
- .assert() -> boolean
- .list() -> array
- .connect(path, options)
- .close()
- .drain()
- .flush()
- .isOpen()
- .open()
- .pause()
- .resume()
- .set()
- .update()
- .write(data)
Test the reachability of an module.
Param | Type | Description |
---|---|---|
pong | string |
Pong message to return |
Test if the actor fulfills system requirements
Retrieves a list of available serial ports with metadata.
Create a new connection
Param | Type | Description |
---|---|---|
path | string |
path |
options | object |
options |
Close the given serial port.
Waits until all output data has been transmitted to the serial port.
Flushes data received but not read.
Returns true if the port is open.
Open a serial port.
Pauses an open connection.
Resumes a paused connection.
Sets flags on an open port.
Changes the baudrate for an open port.
Write data to the given serial port.
Param | Type | Description |
---|---|---|
data | object |
Buffer data to write |
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. |
This software is released under the MIT License.