Coder Social home page Coder Social logo

serialport-js's Introduction

serialport-js

Build Status Coverage Status

pure javascript serial port implementation for node.js, electron and nw.js.

Install

npm i --save serialport-js

Usage

const serialjs = require('serialport-js');

const init = async () => {
    const delimiter = '\n';
    const ports = await serialjs.find();
    if (ports.length) {
        let port = await serialjs.open(ports[0].port, delimiter);

        port.on('data', (data) => {
            console.log(data);
        });
        port.on('error', (error) => {
            console.error(error);
        });
        port.send('foo bar');
    }
};
init();

Methods

find()

Type: Promise<Object[]|Error>

Async function that returns a promise. When resolved it contains a list of the registered serial devices.

findById(id)

Id: String Type: Promise<Object|Error>

Async function that returns a promise. When resolved it contains the found serial device or null when not found.

open(path, delimiter = '\r\n')

Type: Promise<EventEmitter|Error>

Opens a Duplex connection to the serial device. Returns the Port(event.EventEmitter) Object

Port Events

data

Type: String

The data that has been read out of the serial connection.

error

The error that occured.

closed

Is emitted when the connection is closed.

Port Methods

port.send(data)

Sends the data to the serial device.

port.close();

Closes the connection to the serial device. When closed, the event closed is emitted.

Port Variables

  • isOpen
  • serialPort

#node-webkit, nw.js, and seperate node.js thread examples *The only difference with this is that the user must have node installed. It will spawn a node proxy using their local node version and run the pure js serialport implementation. This is great for consumer facing products as there is no need for compilers or dev tools to install the module with your app, users just need node.

    var serialjs=require('serialport-js').node(); //thats the only difference
    //the rest of the implementation is exactly the same.

    serialjs.find(serialDevicesPopulated);

    function serialDevicesPopulated(ports){
        //ports arg is a refrence to serialjs.ports
        console.log(
            ports
        );

        if(!ports[0])
            return;

        serialjs.open(ports[0].port,start,'\n');
    }

    function start(port){
        port.on(
            'data',
            gotData
        );
        
        //if this doesn't show up the port may need a few milliseconds to open
        port.send('howdy doody doo');
    }

    function gotData(data){
        console.log(data);
    }

serialport-js's People

Contributors

ovaar avatar riaevangelist avatar samuelschepp 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.