【发布时间】:2021-02-05 23:52:29
【问题描述】:
我正在尝试使用 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 语句允许我从设备中读取供应商名称。所以,我知道我已经连接并且可以与之通信。我只是不知道从设备访问纬度/经度需要哪些特定命令。
【问题讨论】: