Я пытаюсь запустить эту демоверсию WebUSB:
https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web
Я использую SparkFun Pro Micro, поэтому я изменил vendorId на 0x1b4f, и он успешно отображается во всплывающем окне, когда я вызываю его
device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x1b4f }] })
После того, как я выбрал устройство и нажал «Подключить», я получаю эту ошибку: DOMException: Устройство недоступно
Pro Micro подключен к USB-порту (в / dev / ttyACM0)
и на него загружен демонстрационный код Arduino, который отправляет последовательное рукопожатие при подключении (см. демонстрационную ссылку выше)
Я также отредактировал файл ArduinoRawHID.rules, включив в него 0x1b4f, и поместил его в /etc/udev/rules.d/
.
Что-нибудь очевидно, что я здесь скучаю?
<html>
<body>
<button id="request-device">Click me</button>
<script>
var device;
let button = document.getElementById('request-device');
button.addEventListener('click', async () => {
device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x1b4f }] })
.then(selectedDevice => {
device = selectedDevice;
return device.open(); // Begin a session.
})
.then(() => device.selectConfiguration(1)) // Select configuration #1 for the device.
.then(() => device.claimInterface(2)) // Request exclusive control over interface #2.
.then(() => device.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x22,
value: 0x01,
index: 0x02})) // Ready to receive data
.then(() => device.transferIn(5, 64)) // Waiting for 64 bytes of data from endpoint #5.
.then(result => {
let decoder = new TextDecoder();
console.log('Received: ' + decoder.decode(result.data));
})
.catch(error => { console.log(error); });
});
</script>
</html>
--- EDIT
Я пытаюсь другой демонстрации, и это говорит, что Pro Micro в паре
Но я получаю «Ошибка подключения: SecurityError: Доступ запрещен».
function serialConnect() {
console.log('Connecting to ' + serialPort.device_.productName + '...');
serialPort.connect().then(() => {
console.log(serialPort);
console.log('Connected.');
document.getElementById("divStepperStatus").innerHTML = "Stepper connected";
serialPort.send(textEncoder.encode('\n'));// 1st command will be lost anyway
serialPort.onReceive = data => {
let textDecoder = new TextDecoder();
var decodedStr = textDecoder.decode(data);
var splitedsStr = decodedStr.trim().split(" ");
var recongizedString = false;
if (splitedsStr[0] && splitedsStr[0] == 'S') {
if (splitedsStr[1]) {
var leftSteps = parseInt(splitedsStr[1]);
recongizedString = true;
document.getElementById("divStepperSteps").innerHTML = 'Steps: ' + leftSteps;
}
}
if (!recongizedString) console.log(textDecoder.decode(data));
}
serialPort.onReceiveError = error => {
console.log('Receive error: ' + error);
};
}, error => {
console.log('Connection error: ' + error);
});
};
Я также сделал модификацию #define USB_VERSION 0x210 для USBCore.h
что-нибудь еще, что мне не хватает, может вызвать ошибку SecurityError?