简体   繁体   English

如何使用NW.js和chrome.serial API设置串行端口连接?

[英]How to setup serial port connection using NW.js and chrome.serial API?

I think I can communicate with serial ports in NW.js without such dependencies as node-serialport with precompiled binaries for different platforms. 我认为我可以与NW.js中的串行端口进行通信,而无需像node-serialport这样的依赖以及针对不同平台的预编译二进制文件。

Pure Node.js can't accomplish this task. 纯Node.js无法完成此任务。 But there's integrated Chrome API in NW.js and it has chrome.serial API, that can be used directly in JavaScript to setup serial port connection. 但是NW.js中集成了Chrome API,并且具有chrome.serial API,可以直接在JavaScript中使用它来设置串行端口连接。

How to implement this? 如何实现呢?

List 名单

First of all let's fetch list of devices available to communicate with: 首先,让我们获取可与之通信的设备列表:

chrome.serial.getDevices(function(ports) {
    for (let port of ports) {
        if (port.vendorId) {
            console.log(port);
        }
    }
});

You will get list of all ports with vendorId specified, ie existing devices. 您将获得指定了vendorId的所有端口的列表,即现有设备。

Example result: 结果示例:

{
    displayName: 'Arduino Uno'
    path: 'COM7',
    productId: 67,
    vendorId: 9025
}

Property path then used for connection. 然后将属性path用于连接。


Connect

To connect with default settings: 要使用默认设置进行连接:

var path = 'COM7';

chrome.serial.connect(path, {}, function(CI) {
    console.log('Connection ID: '+ CI.connecionId);
    console.log(CI);
});

Now you're ready! 现在您准备好了!

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

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