程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 WebUSB 读取 GPS 接收器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 WebUSB 读取 GPS 接收器?

开发过程中遇到使用 WebUSB 读取 GPS 接收器的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 WebUSB 读取 GPS 接收器的解决方法建议,希望对你解决使用 WebUSB 读取 GPS 接收器有所启发或帮助;

我正在尝试使用 WebUSB 从 JavaScript 中通过 USB 连接的 GPS 接收器读取 GPS 坐标。

我已经能够连接到接收器;但是,我不确定如何使用 WebUSB 访问 NMEA 消息。

到目前为止,我有以下概念验证代码:

<HTML>
  <head>
    <title>WebUSB serial Sample Application</title>
  </head>


<body>
 <a href="#" ID = "click">Connect</a><br>
 <a href="#" ID = "send">Send Data</a><br>
 <a href="#" ID = "read">Read Data</a><br>
 <script>
 
let y;
let device;

var connectbutton = document.getElementByID('click')
connectbutton.addEventListener('click',function() {
    navigator.usb.requestDevice({
        filters: [{}]
    }).then((SELEctedDevicE) => {
        device = SELEctedDevice;
        return device.open()
            .then(() => device.SELEctConfiguration(1))
            .then(() => device.claimInterface(device.configuration.interfaces[0].interfacenumber))
            .then(() => device.SELEctAlternateInterface(device.configuration.interfaces[0].interfacenumber,0))
            .then(() => {
                y = device;
            })
    });
})

var sendbutton = document.getElementByID('send')    
var sendDecoder = new TextDecoder()
sendbutton.addEventListener('click',async () => {
y.controlTransferOut({
    requestType: 'class',recipIEnt: 'interface',request: 0x22,value: 0x01,index: 0x00});
    
y.controlTransferIn({
        requestType: 'standard',recipIEnt: 'device',request: 0x06,value: 0x0302,index: 0x409
    },255)
        .then(result => {
            let decoder = new TextDecoder()
            console.log(sendDecoder.decode(result.data));
            console.log('sent req');
        }).catch(error => {
            console.log(error);
        });
});

 </script>
 </body>
 </HTML>

transferOut 语句允许我从设备读取供应商名称。所以,我知道我已连接并且可以与之通信。我只是不知道从设备访问纬度/经度需要什么特定命令。

更新: 以下是设备配置信息的快照

使用 WebUSB 读取 GPS 接收器

解决方法

为了实现您的目标,您需要了解几个层次。首先是设备实现的USB接口。

在您发布的示例代码中,有两个调用向设备发送命令。第一个是 controlTransferOut(),它将标准 USB CDC-ACM SET_CONTROL_LINE_STATE 请求 (0x22) 发送到设备以启用 DTR 信号 (0x01)。如果您的设备实现了 USB CDC-ACM 协议,那么这是写入操作,因为它向设备发出信号,表明主机软件已准备好接收数据。如果您的设备未实现此协议,则该命令将被忽略或失败。您已经声明了第一个接口,要了解该接口实现的 USB 协议,您应该检查 device.configuration.interfaces[0].alternates[0].interfaceClassdevice.configuration.interfaces[1].alternates[0].interfaceClass。 USB CDC-ACM 设备将有一个接口为 2 类,一个接口为 10 类。类 2 用于控制接口,而类 10 是数据接口。如果您的设备没有这两个接口,那么它可能没有实现 USB CDC-ACM 协议,您必须先弄清楚它使用的是什么协议。

您进行的第二个调用是 controlTransferIn(),它发送标准 USB GET_DESCRIPTOR 请求 (0x06) 以从设备读取字符串描述符。传递 0x0302 要求它读取索引 2 处的字符串描述符(类型 3)。这将适用于任何 USB 设备(假设索引正确),因此不会告诉您是否已经弄清楚接口支持什么协议。

假设这是一个 USB CDC-ACM 接口,那么您需要查看 device.configuration.interfaces[1].alternates[0].endpoints 并找出 IN 和 OUT 端点的端点编号。这些是您将传递给 transferIn()transferOut() 以从设备发送和接收数据的内容。

了解完所有内容后,您需要弄清楚如何让设备开始发送 NMEA 消息。如果你很幸运,那么在默认模式下它会自动发送它们,你可以开始调用 transferIn() 来接收它们。否则,您将不得不弄清楚向设备发送什么命令以将其置于正确的模式。如果您有该设备的文档或用其他语言编写的支持该设备的示例代码,那么这将非常有助于解决这个问题。

,

我终于解决了这个问题。答案(无论如何对我来说)是购买一个不同的 GPS 接收器,它实现了 CDC-ACM 接口,因为这个协议似乎有更多的例子和更好的文档。

以下概念验证代码有效:

<html>
  <head>
    <title>WebUSB serial Sample Application</title>
  </head>


<body>
 <a href="#" id = "click">Connect</a><br>
 <a href="#" id = "read">Read Data</a><br>
 <script>
 
let y;
let device;

var connectButton = document.getElementById('click')
connectButton.addEventListener('click',function() {
    navigator.usb.requestDevice({
        filters: [{}]
    })
    .then((SELEctedDevicE) => {
        device = SELEctedDevice;
        return device.open();
    })
    .then(() => device.SELEctConfiguration(1))
    .then(() => device.claimInterface(device.configuration.interfaces[0].interfacenumber))
    .then(() => device.claimInterface(device.configuration.interfaces[1].interfacenumber))
    .then(() => device.SELEctAlternateInterface(device.configuration.interfaces[0].interfacenumber,0))
    .then(() => device.SELEctAlternateInterface(device.configuration.interfaces[1].interfacenumber,0))
    .then(() => {
        y = device;
    })
})

var readButton = document.getElementById('read')
var readDecoder = new TextDecoder()
var readLoop = () => {
    y.transferIn(2,64)
        .then(result => {
            let decoder = new TextDecoder()
            console.log(readDecoder.decode(result.data.buffer));
            readLoop();
        }).catch(error => {
            console.log(error);
        });
};

readButton.addEventListener('click',async () => {
    readLoop();
});


 </script>
 </body>
 </html>

大佬总结

以上是大佬教程为你收集整理的使用 WebUSB 读取 GPS 接收器全部内容,希望文章能够帮你解决使用 WebUSB 读取 GPS 接收器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。