Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to use the serial port in my Ionic 2 project. I use Cordova BLE for iOS and Android but Cordova BLE does not support Windows 10. First I used npm install serialport in node-module. That's fine. When I use .js file and nodejs to work I can get data in the serial port via ex. 'COM3'. Below is my JavaScript code:

SerialPort =require("serialport")

var socketServer;
var serialPort;
var portName = 'COM3';
var sendData = "";

function startServer() {

serialListener();
}

function serialListener() {

var receivedData = "";

serialPort = new SerialPort(portName, {
    baudrate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
});

serialPort.on("open", function () {

    console.log('open serial communication');

    // Listens to incoming data
    serialPort.on('data', function (data) {

        receivedData += data.toString();

        if (receivedData.indexOf('E') >= 0 && receivedData.indexOf('B') >= 0) {

            sendData = receivedData.substring(receivedData.indexOf('B') + 1,  receivedData.indexOf('E'));

            receivedData = '';
        }

        // send the incoming data to browser with websockets.
        //socketServer.emit('update', sendData);

        console.log(sendData);
    });
 });
  }
  exports.start = startServer;

When I run in nodejs as like node index.js Every think is OK. I easily get serial port data.

But now I can not use this method in TypeScript. My .ts code is below.

   import { Component, Injectable} from '@angular/core';

   declare function require(name: string);

   var SerialPort=require('serialport');



 @Injectable()
 export class ServicePort {

public socketServer: any;
public serialPort: any;
public portName: any = 'COM3';
public sendData: any = "";

public constructor() {

    console.log("Geldi");

    console.log(SerialPort);
    //this.serialListener();
}

serialListener() {

    var receivedData = "";

    this.serialPort = new SerialPort(this.portName, {
        baudrate: 9600,
        dataBits: 8,
        parity: 'none',
        stopBits: 1,
        flowControl: false
    });

    this.serialPort.on("open", () => {

        console.log('open serial communication');

        this.serialPort.on('data', (data)=> {

            receivedData += data.toString();

            if (receivedData.indexOf('E') >= 0 && receivedData.indexOf('B') >= 0) {

                this.sendData = receivedData.substring(receivedData.indexOf('B') + 1, receivedData.indexOf('E'));

                receivedData = '';
            }

            // send the incoming data to browser with websockets.
            //socketServer.emit('update', sendData);

            console.log(this.sendData);
        });
    });
}
}

I call above .ts like this:

import { Component} from '@angular/core';
import { NavController, Platform} from 'ionic-angular';
import {ServicePort} from '../serialport/servicePort';


 @Component({
   selector: 'page-home',
   templateUrl: 'home.html',
   providers: [ServicePort]
 })
export class HomePage {

  constructor(public navCtrl: NavController, private platform: Platform,         private portservice: ServicePort) {

    this.portservice.serialListener();
  }
 }

I get this error:

Uncaught TypeError: exists is not a function
at Function.getRoot (main.js:99030)
at bindings (main.js:98926)
at Object.<anonymous> (main.js:111823)
at Object.<anonymous> (main.js:111856)
at __webpack_require__ (main.js:20)
at Object.<anonymous> (main.js:112063)
at Object.<anonymous> (main.js:112527)
at __webpack_require__ (main.js:20)
at Object.<anonymous> (main.js:113162)
at __webpack_require__ (main.js:20)

Can I use JavaScript function in my TypeScript for my Ionic 2 project or can I use the serial port in TypeScript?

share|improve this question
    
anyone have a solution ? – artemitSoft Jan 18 at 6:39

To use a javascript module in typescript you need to have a declarations file of the module. Check github of definitelyTyped.

Serialport types is available:npm link

Just do:

npm install --save-dev @types/serialport

Also ensure your tsconfig.json has:

"typeRoots": [
  "node_modules/@types"
]

To import:

import * as SerialPort from 'serialport';
share|improve this answer
    
I take this mistake : Uncaught TypeError: exists is not a function at Function.getRoot (bindings.js:153) at bindings (bindings.js:55) at Object.<anonymous> (bindings.js:3) at Object.<anonymous> (bindings.js:34) at webpack_require (bootstrap 18e72d9…:19) at Object.<anonymous> (serialport.js:12) at Object.<anonymous> (serialport.js:474) at webpack_require (bootstrap 18e72d9…:19) at Object.<anonymous> (main.js:59007) at webpack_require (bootstrap 18e72d9…:19) – artemitSoft Jan 17 at 12:07
    
are you calling a function exists? – suraj Jan 17 at 12:19
    
no just I call as console.log(SerialPort) for try. Can you use in ionic 2. Because its not working in my project. – artemitSoft Jan 17 at 12:24
    
I havent used serialport.. But the above steps are used to use a javascript module in ionic 2.. github.com/DefinitelyTyped/DefinitelyTyped/blob/master/… maybe this will help – suraj Jan 17 at 12:25
    
How can use in ionic2 , How can call this method, because my javascript is working in nodejs. – artemitSoft Jan 17 at 12:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.