Chrome extension: how to use serial port (now that

2019-03-16 02:47发布

问题:

Aim: I am attempting to write a little "thing" that can talk from my browser to the COM port of an Arduino.

Issue: My first attempt is to write an extension. Therefore I am requesting permission for serial in my manifest file. When loading the unpackaged extension via developer mode I receive the following error: 'serial' is only allowed for packaged apps, but this is a extension.

I see here that packaged app is an outdated term and I guess they just mean Chrome app. Now unfortunately it seems that Chrome is also discontinuing these Chrome apps as discussed here.

Question: So how should I access the serial functionality of Chrome apps/extensions/whatever new name you come up with?

回答1:

You could continue on the same path of writing a chrome app and using the chrome.serial API, but use NW.js as the runtime of your app. It supports the Chrome App APIs. Essentially you just write your Chrome App but instead of opening it in Chrome you open it in NW.js executable.

https://nwjs.io/

Or you could use a Node.js package such as serialport to access your COM port

https://github.com/EmergingTechnologyAdvisors/node-serialport

In this case you could write it as either NW.js app, or an Electron app. Electron is quite popular lately. You have access to the node module ecosystem and also Chromium - open source part of Chrome web browser so you can still do all the HTML/JavaScript/CSS you want.

https://github.com/electron/electron

This is also one of the suggested paths from Google: https://developer.chrome.com/apps/migration#native