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?