简体   繁体   English

如何在 `deno` 中使用`serialport` 库?

[英]How to use `serialport` library with `deno`?

while experimenting with deno to get readings from arduino, i got stuck on proper usage of serialport lib.在尝试使用 deno 从 arduino 获取读数时,我陷入了正确使用串行端口库的问题。 i tried the following:我尝试了以下方法:


according to an article packages from pika.dev can be used.根据pika.dev一篇文章,可以使用包。 but from pika i get:但从 pika 我得到:
However, no web-optimized "module" entrypoint was found in its package.json manifest.


also using jpsm.org didn't work either:也使用jpsm.org也不起作用:

import * as SerialPort from "https://dev.jspm.io/serialport";

breaks with:打破:
error: Uncaught TypeError: exists is not a function
probably because of the missing type headers that should be available with npm as @types/serialport but not available on pika.dev or jspm.io.可能是因为缺少类型标头,应该在 npm 作为@types/serialport但在 pika.dev 或 jspm.io 上不可用。


using npm to install serialport and @types/serialport and referring to their local path as following, wasn't the solution either:使用 npm 安装serialport@types/serialport并引用它们的本地路径如下,也不是解决方案:

// @deno-types="./node_modules/@types/serialport/index.d.ts"
import * as SerialPort from "https://dev.jspm.io/serialport";

the error is:错误是:
error: relative import path "stream" not prefixed with / or./ or../ Imported from "file:///home/user/code/deno-serial-test/node_modules/@types/serialport/index.d.ts"

meaning it complains because the npm downloaded library dependencies aren't relative paths.这意味着它会抱怨,因为 npm 下载的库依赖项不是相对路径。 this would mean i would need to change/patch all library modules.这意味着我需要更改/修补所有库模块。


the code i'm trying to run is:我试图运行的代码是:

// import * as SerialPort from "https://dev.jspm.io/npm:serialport@9.0.0/lib/index.dew.js";

// @deno-types="./node_modules/@types/serialport/index.d.ts"
import * as SerialPort from "https://dev.jspm.io/serialport";

async function readAvailablePorts() {
    const ports = await SerialPort.list();
    console.log('Available SerialPorts: ', ports);
}

readAvailablePorts();


any ideas how to solve would be appreciated.任何如何解决的想法将不胜感激。

You can't use NPM serialport package on Deno, it relies heavily on Node.js APIs, including stream which hasn't been polyfilled by Node Compatibility Library You can't use NPM serialport package on Deno, it relies heavily on Node.js APIs, including stream which hasn't been polyfilled by Node Compatibility Library

according to an article packages from pika.dev can be used.根据 pika.dev 的一篇文章,可以使用包。 but from pika i get:但从 pika 我得到:

You can use packages that uses plain JavaScript, not packages that rely on Node.js APIs.您可以使用使用普通 JavaScript 的包,而不是依赖于 Node.js API 的包。


You'll have to build your own package or wait until one is written if you want to get readings from Arduino in Deno如果您想从 Deno 中的 Arduino 获取读数,您必须构建自己的 package 或等到写入一个


You can read more regarding using NPM modules on Deno on: How to use npm module in DENO?您可以阅读更多关于在 Deno 上使用 NPM 模块的信息: 如何在 DENO 中使用 npm 模块?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM