javascript - Serial API 中 navigator.serial 和 SerialPort 的功能是否未实现?

标签 javascript google-chrome webusb

我正在尝试从网页连接到串口。我找到了 Serial 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);

但是 navigator.serial.requestPort() 调用失败了;我发现 navigator.serial 未定义。我很确定串行端口已连接到我的计算机。 Serial API 中 navigator.serial 和 SerialPort 的功能是否未实现?我的 chrome 版本是 74.0.3729.131。我的系统是 Ubuntu 16.04

最佳答案

串行 API 尚未在 Chrome 中完全实现。我看你已经找到了the issue在 Chromium bug tracker 中实现。实现进展的更新将在那里发布。它目前处于“启动”状态,而不是“固定”状态。

关于javascript - Serial API 中 navigator.serial 和 SerialPort 的功能是否未实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56126373/

相关文章:

javascript - 使用 WebUSB 的 getDevices 查询所有可用设备,但没有提示消息?

google-chrome - "Failed to execute ' 打开 ' on ' USB设备 ': Access denied."

禁用 Javascript,简短问题

javascript - 记录中间功能可选参数

javascript - 在 Materialise css v1.0 上显示自动完成芯片的图像?

java - 加载扩展时出错无法从 'C:\..\Local\Temp\scoped_dir6312_32763\internal' 加载扩展。禁止加载解压缩的扩展

javascript - 在 Chrome 网络蓝牙 API 中未经用户许可连接到配对设备?

javascript - 你什么时候应该使用转义而不是 encodeURI/encodeURIComponent?

javascript - 如何通过上下文菜单将消息从 Chrome 扩展后台脚本传递到 JavaScript 中的内容脚本?

javascript - 如何更改地址栏的url