Skip to content

Instantly share code, notes, and snippets.

@japboy
Last active December 15, 2015 18:59
Show Gist options
  • Save japboy/5308446 to your computer and use it in GitHub Desktop.
Save japboy/5308446 to your computer and use it in GitHub Desktop.
Just a test script to communicate with serial ports
node_modules
'use strict'
stream = require 'stream'
binary = require 'binary'
serialport = require 'serialport'
SerialPort = serialport.SerialPort
listPorts = ->
serialport.list (err, ports) ->
ports.forEach (port) ->
console.log port.comName, port.pnpId, port.manufacturer
class Device
_port: ''
_options: {}
_serialport: {}
_stream: {}
data: (data) =>
console.dir data
open: =>
@_stream = @_serialport.readStream
@_serialport.on 'data', @data
console.log "Port '#{@_port}' opened."
close: =>
console.log "Port '#{@_port}' closed."
exit: =>
pid = process.getuid()
console.log "Process #{pid} terminated."
constructor: ->
@_serialport = new SerialPort @_port, @_options
@_serialport.on 'open', @open
@_serialport.on 'close', @close
process.on 'exit', @exit
class Arduino extends Device
constructor: ->
@_port = '/dev/tty.usbmodemfd13431' # Arduino
@_options =
baudRate: 115200
parser: serialport.parsers.readline '\n'
super()
class RaspberryPi extends Device
constructor: ->
usb = '/dev/ttyUSB0'
gpio = '/dev/ttyAMA0'
@_port = gpio
@_options =
baudRate: 115200
super()
class MindSet extends Device
read: (data) =>
console.log data
parse: (data) =>
b = binary()
b.word8bu(data).tap(@read)
constructor: ->
linux = '/dev/rfcomm9'
osx = '/dev/tty.MindSet-DevB'
@_port = osx
@_options =
baudRate: 57600
super()
listPorts()
mindset = new MindSet()
{
"name": "gist-demo-node-serialport",
"version": "0.0.0",
"description": "Just a test script to communicate with serial ports",
"repository": "https://gist.github.com/5308446.git",
"author": "Yu Inao",
"license": "Unlicense",
"dependencies": {
"binary": "~0.3.0",
"serialport": "~1.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment