From a43a67d27a070e70e46eb728282f10ce8de2b163 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 3 Apr 2016 00:56:56 -0400 Subject: [PATCH] Add more details to the readme around many sections - Installation now has more navigation links - The `.list` function is now documented - We now have more valid examples with consistent quotes - Command line tools are now kinda documented --- README.md | 119 +++++++++++++++++++++++++++--------------- sandbox/disconnect.js | 53 ------------------- 2 files changed, 77 insertions(+), 95 deletions(-) delete mode 100644 sandbox/disconnect.js diff --git a/README.md b/README.md index 06ef8c1..7cba87a 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ For support you can open a [github issue](https://github.com/voodootikigod/node-serialport/issues/new), for discussions, designs, and clarifications, we recommend you join our [Gitter Chat room](https://gitter.im/voodootikigod/node-serialport) -***** +*** Imagine a world where you can write JavaScript to control blenders, lights, security systems, or even robots. Yes, I said robots. That world is here and now with node-serialport. It provides a very simple interface to the low level serial port code necessary to program [Arduino](http://www.arduino.cc/) chipsets, [X10](http://www.smarthome.com/manuals/protocol.txt) wireless communications, or even the rising [Z-Wave](http://www.z-wave.com/modules/ZwaveStart/) and [Zigbee](http://www.zigbee.org/) standards. The physical world is your oyster with this goodie. For a full break down of why we made this, please read [NodeBots - The Rise of JS Robotics](http://www.voodootikigod.com/nodebots-the-rise-of-js-robotics). -***** +*** For getting started with node-serialport, we recommend you begin with the following articles: @@ -18,12 +18,17 @@ For getting started with node-serialport, we recommend you begin with the follow * [NodeBots - The Rise of JS Robotics](http://www.voodootikigod.com/nodebots-the-rise-of-js-robotics) - A survey article of why one would want to program robots in JS. * [Johnny-Five](https://github.com/rwaldron/johnny-five#hello-johnny) - The Johnny-Five Robotics and IoT library's introduction "Hello Johnny" (awesome). - -**** +*** * [Installation](#installation-instructions) +* [Installation Special Cases](#installation-special-cases) + * [Windows](#windows) + * [Mac OS X](#mac-os-x) + * [Ubuntu Linux](#ubuntu-linux) + * [Alpine Linux](#alpine-linux) + * [Raspberry Pi Linux](#raspberry-pi-linux) * [Usage](#usage) - * [Open Event](#open-event) + * [The Open Event](#the-open-event) * [Listing Ports](#listing-ports) * [Parsers](#parsers) * [Methods](#methods) @@ -37,9 +42,11 @@ For getting started with node-serialport, we recommend you begin with the follow * [drain()](#drain-callback) * [close()](#close-callback) * [Events](#events) +* [Command Line Tools](#command-line-tools) + * [Serial Port List](#serial-port-list) + * [Serial Port Terminal](#serial-port-terminal) - -**** +*** ## Installation Instructions @@ -55,7 +62,7 @@ We are using [node-pre-gyp](https://github.com/mapbox/node-pre-gyp) to compile a This assumes you have everything on your system necessary to compile ANY native module for Node.js. This may not be the case, though, so please ensure the following are true for your system before filing an issue about "Does not install". For all operatings systems, please ensure you have Python 2.x installed AND not 3.0, node-gyp (what we use to compile) requires Python 2.x. -#### Windows: +#### Windows * Windows 7, Windows 8.1, and Windows 10 are supported. * Might just download and install with no extra steps. If the downloaded binary fails you'll have to build it with the following steps. @@ -65,11 +72,11 @@ This assumes you have everything on your system necessary to compile ANY native * Install [Python 2.7.6](http://www.python.org/download/releases/2.7.6/) matching the bitness of your operating system. For any questions, please refer to their [FAQ](http://docs.python.org/2/faq/windows.html). Default settings are perfect. * Open the 'Visual Studio Command Prompt' and add Python to the path. -#### Mac OS X: +#### Mac OS X Ensure that you have at a minimum the xCode Command Line Tools installed appropriate for your system configuration. If you recently upgraded the OS, it probably removed your installation of Command Line Tools, please verify before submitting a ticket. To compile `node-serialport` with Node.js 4.x+, you will need to use g++ v4.8 or higher. -#### Desktop (Debian/Ubuntu) Linux: +#### Ubuntu Linux You know what you need for your system, basically your appropriate analog of build-essential. Keep rocking! Ubuntu renamed the `node` binary `nodejs` which can cause problems building `node-serialport`. The fix is simple, install the [nodejs-legacy package](https://packages.debian.org/sid/nodejs-legacy) that symlinks `/usr/bin/nodejs => /usr/bin/node` or install the more up to date nodejs package from [Chris Lea's PPA](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#ubuntu-mint-elementary-os). @@ -87,7 +94,7 @@ sudo apt-get install build-essential npm install serialport ``` -#### Alpine Linux: +#### Alpine Linux [Alpine](http://www.alpinelinux.org/) is a (very) small distro, but it uses the musl standard library instead of glibc (that most other Linux distros use), so it requires compilation. @@ -102,7 +109,7 @@ sudo apk add --no-cache make gcc g++ python linux-headers udev npm install serialport --build-from-source=serialport ``` -#### Raspberry Pi Linux: +#### Raspberry Pi Linux Follow the instructions for [setting up a Raspberry pi for use with Johnny-Five and Raspi IO](https://github.com/nebrius/raspi-io/wiki/Getting-a-Raspberry-Pi-ready-for-NodeBots). These projects use Node Serialport under the hood. @@ -122,34 +129,36 @@ When opening a serial port, you can specify (in this order). 1. Path to Serial Port - required. 1. Options - optional and described below. -Open Event ----------- +### The Open Event -You MUST wait for the open event to be emitted before reading/writing to the serial port. The open happens asynchronously so installing 'data' listeners and writing -before the open event might result in... nothing at all. +You MUST wait for the open event to be emitted before writing to the serial port. The open happens asynchronously so writing before the open event will result in an error. Assuming you are connected to a serial console, you would for example: ```js -serialPort.on("open", function () { +var SerialPort = require('serialport').SerialPort; +var serialPort = new SerialPort('/dev/tty-usbserial1'); +serialPort.on('data', function(data) { + console.log('data received: ' + data); +}); + +serialPort.on('open', function () { console.log('open'); - serialPort.on('data', function(data) { - console.log('data received: ' + data); - }); - serialPort.write("ls\n", function(err, results) { + serialPort.write('ls\n', function(err, results) { console.log('err ' + err); console.log('results ' + results); }); }); ``` -You can also call the open function, in this case instantiate the serialport with an additional flag. +You can also use the open function to open the port, but you must set the openImmediately flag to false so it does not open on it's own. ```js -var SerialPort = require("serialport").SerialPort -var serialPort = new SerialPort("/dev/tty-usbserial1", { +var SerialPort = require('serialport').SerialPort +var opt = { baudrate: 57600 -}, false); // this is the openImmediately flag [default is true] +}; +var serialPort = new SerialPort('/dev/tty-usbserial1', opt, false); // this is the openImmediately flag [default is true] serialPort.open(function (error) { if ( error ) { @@ -159,7 +168,7 @@ serialPort.open(function (error) { serialPort.on('data', function(data) { console.log('data received: ' + data); }); - serialPort.write("ls\n", function(err, results) { + serialPort.write('ls\n', function(err, results) { console.log('err ' + err); console.log('results ' + results); }); @@ -167,13 +176,30 @@ serialPort.open(function (error) { }); ``` -Listing Ports ----------- +### Listing Ports -You can also list the ports along with some metadata as well. +`.list(callback)` + +Retrieves a list of available serial ports with metadata. + +* `callback` is a required function that looks should look like: `function (err, ports) { ... }`. `ports` will be an array of objects with port info. Only the `comName` is guaranteed, all the other fields are retrieved as available. The `comName` is either the path or identifier (eg `COM1`) used to open the serialport. ```js -var serialPort = require("serialport"); +// example port information +{ + comName: '/dev/cu.usbmodem1421', + manufacturer: 'Arduino (www.arduino.cc)', + serialNumber: '757533138333964011C1', + pnpId: '', + locationId: '0x14200000', + vendorId: '0x2341', + productId: '0x0043' +} + +``` + +```js +var serialPort = require('serialport'); serialPort.list(function (err, ports) { ports.forEach(function(port) { console.log(port.comName); @@ -183,27 +209,26 @@ serialPort.list(function (err, ports) { }); ``` -Parsers -------- +### Parsers Out of the box, node-serialport provides two parsers one that simply emits the raw buffer as a data event and the other which provides familiar "readline" style parsing. To use the readline parser, you must provide a delimiter as such: ```js -var serialport = require("serialport"); -var SerialPort = serialport.SerialPort; // localize object constructor +var serialport = require('serialport'); +var SerialPort = serialport.SerialPort; -var sp = new SerialPort("/dev/tty-usbserial1", { - parser: serialport.parsers.readline("\n") +var port = new SerialPort('/dev/tty-usbserial1', { + parser: serialport.parsers.readline('\n') }); ``` To use the raw parser, you just provide the function definition (or leave undefined): ```js -var serialport = require("serialport"); -var SerialPort = serialport.SerialPort; // localize object constructor +var serialport = require('serialport'); +var SerialPort = serialport.SerialPort; -var sp = new SerialPort("/dev/tty-usbserial1", { +var port = new SerialPort('/dev/tty-usbserial1', { parser: serialport.parsers.raw }); ``` @@ -212,15 +237,16 @@ var sp = new SerialPort("/dev/tty-usbserial1", { You can get updates of new data from the Serial Port as follows: ```js -serialPort.on("data", function (data) { - sys.puts("here: "+data); +port.on('data', function (data) { + console.log('Data: ' + data); }); ``` You can write to the serial port by sending a string or buffer to the write method as follows: ```js -serialPort.write("OMG IT WORKS\r"); +port.write('Hi Mom!'); +port.write(new Buffer('Hi Mom!')); ``` Enjoy and do cool things with this code. @@ -359,3 +385,12 @@ Callback is called with no arguments when the port is closed. In the event of an ### .on('error', callback) Callback is called with an error object whenever there is an error. + +## Command Line Tools +If you install `serialport` globally. (eg, `npm install -g serialport`) you'll receive two command line tools. + +### Serial Port List +`serialportlist` will list all available serialports + +### Serial Port Terminal +`serialportterm` provides a basic terminal interface for a serialport. `ctrl+c` will exit. diff --git a/sandbox/disconnect.js b/sandbox/disconnect.js deleted file mode 100644 index ac4371c..0000000 --- a/sandbox/disconnect.js +++ /dev/null @@ -1,53 +0,0 @@ -// disconnect.js -- an attempt track down the disconnected issue. - -//"use strict"; -var serialPort = require("../serialport"); - -var sp; -function activate () { - -if (sp) { - sp = new serialPort.SerialPort(process.env.SERIAL_PORT, { - baudRate: 115200, - parser : serialPort.parsers.readline(';') - }); - - - sp.on('data', function (data) { - console.log('on.data', data); - sp.close(function(err) { - console.log('close()', err); - }); - }); - - sp.on('close', function (err) { - console.log('on.close'); - sp = null; - }); - - - sp.on('disconnect', function (err) { - console.log('on.disconnect'); - - - }); - - - sp.on('error', function (err) { - console.error("on.error", err); - }); - - sp.on('open', function () { - console.log('on.open'); - sp.write("foobar;", function(err, chars) { - console.log('write()', chars, err); - }); - }); - - } -} - -activate() - -//keep alive -setInterval(function() { activate(); }, 10000); \ No newline at end of file