typescript ngx串行/ RS232协议/Angular

syqv5f0l  于 2023-03-13  发布在  TypeScript
关注(0)|答案(1)|浏览(149)

我是Angular的新手,我在RS232协议项目上遇到了麻烦。我想在项目中使用ngx-serial(https://www.npmjs.com/package/ngx-serial)。即使writer的克隆库(https://github.com/archocron/ngx-serial-example)工作正常,但当我尝试在自己的angular项目中导入库时,我可以连接串行端口,但出现了错误。

Error: src/app/app.component.ts:2:27 - error TS2307: Cannot find module 'ngx-serial' or its corresponding type declarations.

2 import { NgxSerial } from 'ngx-serial';

但我已经尝试了以下方法:

npm i ngx-serial
npm install ngx-serial

包含ngx序列号的package.json:

...
"dependencies": {
    "@angular/animations": "^15.2.0",
    "@angular/common": "^15.2.0",
    "@angular/compiler": "^15.2.0",
    "@angular/core": "^15.2.0",
    "@angular/forms": "^15.2.0",
    "@angular/platform-browser": "^15.2.0",
    "@angular/platform-browser-dynamic": "^15.2.0",
    "@angular/router": "^15.2.0",
    "ngx-serial": "^1.0.1",
    "bootstrap-scss": "^5.2.3",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.12.0"
  },
...

以及应用程序组件ts:

import { Component } from '@angular/core';
import { NgxSerial } from 'ngx-serial';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'Display System';

  serial: NgxSerial;
  port: any;

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

  dataHandler(data: string) {
    console.log("From arduino -> " + data);
  }

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

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

      });
  }
}

不幸的是,我仍然得到错误。有什么建议吗?谢谢你的时间。

3bygqnnd

3bygqnnd1#

我以前

npm install ngx-serial --legacy-peer-deps

代替

npm install ngx-serial

而且奏效了!

相关问题