简体   繁体   English

串行 API 中 navigator.serial 和 SerialPort 上的函数是否未实现?

[英]Are functions on navigator.serial and SerialPort in Serial API unimplemented?

I am trying to connect to the serial port from a web page.我正在尝试从网页连接到串行端口。 I found the Serial API which can support that.我找到了可以支持的串行 API

 var Serial = {}; (function() { 'use strict'; /* * */ Serial.debug = true; /* * */ // Serial.device; /* * */ Serial.log = function(message) { if(Serial.debug) { let pre; if(!document.querySelector('pre')) { pre = document.createElement('pre'); document.body.appendChild(pre); } pre = document.querySelector('pre'); pre.innerHTML += `\\n${message}`; } } /* * */ Serial.request = function() { const requestOptions = { // Filter on devices with the Arduino USB vendor ID. filters: [{ vendorId: 0x0403 }], }; // Request an Arduino from the user. console.log(navigator); console.log(navigator.serial); const port = navigator.serial.requestPort(requestOptions); // Open and begin reading. port.open({ baudrate: 19200 }); const reader = port.in.getReader(); while (true) { const {done, data} = reader.read(); if (done) break; console.log(data); } } /* * */ Serial.port = { /* * */ device:{}, /* * */ connect:function() { let loop = () => { this.device.transferIn(5, 64).then(result => { Serial.log(result); loop(); }, error => { WebUSB.log(error); }); } console.log(this.device); return this.device.open( {baudrate: 19200 }) .then(() => this.device.selectConfiguration(1)) .then(() => this.device.claimInterface(1)) .then(() => this.device.controlTransferOut({requestType: 'class', recipient: 'interface', request: 0x22, value: 0x01, index: 0x02})) .then(() => {loop}) .then( result => { Serial.log('successfull'); } ) .catch( error => { Serial.log(error); } ); }, /* * */ send:function() { let d = new Date(); let h = d.getHours(); let m = d.getMinutes(); let s = d.getSeconds(); if(h < 10){h = `0${h}`}; if(m < 10){m = `0${m}`}; if(s < 10){s = `0${s}`}; let data = `show time ${h}${s % 2 == 1 ? ':' : ' '}${m}${s % 2 == 0 ? ':' : ' '}${s}`; console.log(data); let textEncoder = new TextEncoder(); WebUSB.port.device.transferOut(4, textEncoder.encode(data)); }, /* * */ disconnect:function() { console.log(Serial.port.device) Serial.port.device.close() .then( result => { Serial.log('closed'); document.querySelectorAll('button')[1].parentNode.removeChild(document.querySelectorAll('button')[1]); } ) .catch( error => { Serial.log(error); } );; } } })(); /* * */ window.addEventListener('DOMContentLoaded', connect => { let button = document.createElement('button'); button.innerHTML = 'Connect a USB Device'; button.addEventListener('click', Serial.request); document.body.appendChild(button); }, true);

But the navigator.serial.requestPort() call is failing;但是navigator.serial.requestPort()调用失败了; I find that the navigator.serial is undefined.我发现 navigator.serial 未定义。 I'm pretty sure the serial port is connected to my computer.我很确定串行端口已连接到我的计算机。 Are functions on navigator.serial and SerialPort in Serial API unimplemented?串行 API 中 navigator.serial 和 SerialPort 上的函数是否未实现? My chrome version is 74.0.3729.131.我的 chrome 版本是 74.0.3729.131。 My system is Ubuntu 16.04我的系统是 Ubuntu 16.04

The Serial API is not yet completely implemented in Chrome.串行 API 尚未在 Chrome 中完全实现。 I see that you have already found the issue in the Chromium bug tracker for the implementation.我看到你已经找到了问题的铬bug跟踪系统的实施。 Updates on implementation progress will be posted there.有关实施进度的更新将在此处发布。 It is currently in the "started" state, not "fixed".它目前处于“开始”状态,而不是“固定”状态。

Posting @Benjamin 's answer for the sake of clarity.为了清楚起见,发布@Benjamin 的答案。 Serial port connection's are still an "experimental" feature in Chrome early 2020. You have to turn on the following option: 2020 年初,串行端口连接仍然是 Chrome 中的“实验性”功能。您必须打开以下选项:

chrome://flags/#enable-experimental-web-platform-features

Just copy paste that line in the Chrome address bar and enable it.只需将该行复制粘贴到 Chrome 地址栏中并启用它。

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

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