Giter VIP home page Giter VIP logo

ngx-serial's Introduction

NgxSerial

NgxSerial eases the use of the Serial Web API in Angular.

You can establish a bidirectional communication with any device supporting RS232 protocol (native or over USB) using only a web browser, forget running local applications !

Examples of devices could be Arduino like boards, PLC's, Smart Card Readers, Access control devices.

This Video show the library in action

Installation

First, install the package using npm install

npm install ngx-serial

Use

You have available ngx-serial on your project, add the following import in your component.

import { NgxSerial } from 'ngx-serial';

You can use the class as follow.

serial: NgxSerial;

constructor() {
    this.serial = new NgxSerial(this.dataHandler);
}

Data Handler will receive read data, so here you can implement your read logic. It will read until LF (can be changed passing the string to the constructor) is received.

dataHandler(data: string) {
    console.log(data);
}

To request permissions to use the COM port and connect , use the method connect().

this.serial.connect(); 

To use connect() a user interaction is required, for example call this method when the user click a button.

To send data to the serial device use sendData(), this method accept a string as paremeter.

this.serial.sendData("Data\n");

To free resources execute the method close()

this.serial.close();

Default options values are.

baudRate: 9600, dataBits: 8, parity: 'none', bufferSize: 256, flowControl: 'none'

In the constructor you can pass optionally an options Object and the control character (\n by default).

let options = { baudRate: 9600, dataBits: 8, parity: 'none', bufferSize: 256, flowControl: 'none' };
this.serial = new NgxSerial(this.dataHandler, options, "\n");

connect() and close() accept a callback function returning the serial port. Can be used to check the connection state.

if(!this.port){
    this.serial.connect((port:any)=>{
    this.port = port;
});

if(this.port)
    this.serial.sendData("L1\n");

if(this.port)
    this.serial.close((port:any)=>{
    this.port = port;
});


 <div *ngIf="this.port">
    <span class = "greendot"></span>
    <span>Connected</span>
</div>
<div *ngIf="!this.port">
    <span class = "reddot"></span>
    <span>Disconnected</span>
</div>

To check an example application made on Angular click here The Arduino Code is located on src/assets/ngx-serial-example.ino

Broswer support

At this moment (21/06/2021) Chrome, Opera and Edge are supporting Web Serial API

ngx-serial's People

Contributors

archocron 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.