Giter Site home page Giter Site logo

mikamaunula / browser-serialport Goto Github PK

View Code? Open in Web Editor NEW

This project forked from garrows/browser-serialport

0.0 2.0 0.0 817 KB

Robots in the browser. Just like node-serialport but for browser/chrome apps.

License: MIT License

Makefile 0.06% JavaScript 99.18% CSS 0.05% Arduino 0.71%

browser-serialport's Introduction

browser-serialport

Robots in the browser. Just like node-serialport but for browser apps.

Why not Node.js?

Nodebots are awesome but HTML5 apps have access to a lot of APIs that make sense for robotics like the GamepadAPI, WebRTC Video and Data, Web Speech API, etc. Also you get a nice GUI and its easier to run. I have also made a fork of Johnny-Five to work with Browserify as well by modifying it's dependancy Firmata to use browser-serialport.

Restrictions

You will not be able to add this to your normal website.

This library only works in a Chrome Packaged App as this is the only way to get access to the serial ports API in the browser.

If you want help making your first Chrome App, read the "Create Your First App" tutorial.

There is currently no Firefox extension support but that might come soon if possible.

Known incompatibilities with node-serialport

  • Parsers not implemented
  • Inconsistent error messages
  • Chrome has a slightly different options set:
    • dataBits: 7, 8
    • stopBits: 1, 2
    • parity: 'none', 'even', 'mark', 'odd', 'space'
    • flowControl: 'RTSCTS'

Installation

npm install browser-serialport

To Use

Opening a serial port:

var SerialPort = require("browser-serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
});

When opening a serial port, you can specify (in this order).

  1. Path to Serial Port - required.
  2. Options - optional and described below.

The options object allows you to pass named options to the serial port during initialization. The valid attributes for the options object are the following:

  • baudrate: Baud Rate, defaults to 9600. Should be one of: 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, or 50. Custom rates as allowed by hardware is supported.
  • databits: Data Bits, defaults to 8. Must be one of: 8, 7, 6, or 5.
  • stopbits: Stop Bits, defaults to 1. Must be one of: 1 or 2.
  • parity: Parity, defaults to 'none'. Must be one of: 'none', 'even', 'mark', 'odd', 'space'
  • buffersize: Size of read buffer, defaults to 255. Must be an integer value.
  • parser: The parser engine to use with read data, defaults to rawPacket strategy which just emits the raw buffer as a "data" event. Can be any function that accepts EventEmitter as first parameter and the raw buffer as the second parameter.

Note, we have added support for either all lowercase OR camelcase of the options (thanks @jagautier), use whichever style you prefer.

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.

Assuming you are connected to a serial console, you would for example:

serialPort.on("open", function () {
  console.log('open');
  serialPort.on('data', function(data) {
    console.log('data received: ' + data);
  });
  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 instanciate the serialport with an additional flag.

var SerialPort = require("browser-serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.open(function (error) {
  if ( error ) {
    console.log('failed to open: '+error);
  } else {
    console.log('open');
    serialPort.on('data', function(data) {
      console.log('data received: ' + data);
    });
    serialPort.write("ls\n", function(err, results) {
      console.log('err ' + err);
      console.log('results ' + results);
    });
  }
});

List Ports

You can also list the ports along with some metadata as well.

var serialPort = require("browser-serialport");
serialPort.list(function (err, ports) {
  ports.forEach(function(port) {
    console.log(port.comName);
    console.log(port.pnpId);
    console.log(port.manufacturer);
  });
});

Parsers

Browser-serialport doesn't as of 2.0.0 support parsers.

You can get updates of new data from the Serial Port as follows:

serialPort.on("data", function (data) {
  sys.puts("here: "+data);
});

You can write to the serial port by sending a string or buffer to the write method as follows:

serialPort.write("OMG IT WORKS\r");

Enjoy and do cool things with this code.

browser-serialport's People

Contributors

garrows avatar jacobrosenthal avatar korynunn avatar mikamaunula avatar monteslu avatar phated avatar soldair avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.