我想在Ionic 2项目中使用串口。我对iOS和Android使用Cordova BLE,但Cordova BLE不支持Windows 10.首先,我在node-module中使用了npm install serialport
。没关系。当我使用.js文件和nodejs工作时,我可以通过ex获取串口中的数据。 ' COM3&#39 ;.以下是我的JavaScript代码:
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;
当我像node index.js
一样在nodejs中运行时,每个人都认为没问题。我很容易得到串口数据。
但现在我无法在TypeScript中使用此方法。我的.ts代码如下。
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);
});
});
}
}
我在上面打电话。这样:
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();
}
}
我收到此错误:
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)
我可以在TypeScript中为我的Ionic 2项目使用JavaScript函数,还是可以在TypeScript中使用串口?
答案 0 :(得分:0)
要在typescript中使用javascript模块,您需要拥有该模块的声明文件。检查github of definitelyTyped。
可提供串行端口类型:npm link
只是做:
npm install --save-dev @types/serialport
还要确保你的tsconfig.json有:
"typeRoots": [
"node_modules/@types"
]
要导入:
import * as SerialPort from 'serialport';