admin管理员组文章数量:1425761
The code below works on my Xubuntu machine, but now I'm on Kubuntu and it isn't working anymore - it won't open the port.
The Arduino IDE works fine (can write code to the board) and I'm able to select the device (Arduino Uno) in Chrome, but the code will stop when I try to open the port: Uncaught (in promise) DOMException: Failed to open serial port
or required member baudRate is undefined
will e up.
const filters = [
// Filter on devices with the Arduino Uno USB Vendor/Product IDs.
{ usbVendorId: 0x2341, usbProductId: 0x0043 },
{ usbVendorId: 0x2341, usbProductId: 0x0001 },
];
async function getPortAndStartReading() {
if (!portFound) {
const port = await navigator.serial.requestPort({ filters });
await port.open({ baudRate: 9600 }) //problem here
reader = port.readable.getReader();
outputStream = port.writable
readLoop();
if (port) {
connectionToPortSuccessfulMessage = 'Connection successful'
setPortFound(true)
}
}
}
I've tried changing the permissions on the serial port by following this, so now if I run groups user
I get user : user adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare
, but it still won't work.
I've also checked chrome://device-log
to see if I could find any errors but all I get is info about (physically) adding or removing a USB device.
The code below works on my Xubuntu machine, but now I'm on Kubuntu and it isn't working anymore - it won't open the port.
The Arduino IDE works fine (can write code to the board) and I'm able to select the device (Arduino Uno) in Chrome, but the code will stop when I try to open the port: Uncaught (in promise) DOMException: Failed to open serial port
or required member baudRate is undefined
will e up.
const filters = [
// Filter on devices with the Arduino Uno USB Vendor/Product IDs.
{ usbVendorId: 0x2341, usbProductId: 0x0043 },
{ usbVendorId: 0x2341, usbProductId: 0x0001 },
];
async function getPortAndStartReading() {
if (!portFound) {
const port = await navigator.serial.requestPort({ filters });
await port.open({ baudRate: 9600 }) //problem here
reader = port.readable.getReader();
outputStream = port.writable
readLoop();
if (port) {
connectionToPortSuccessfulMessage = 'Connection successful'
setPortFound(true)
}
}
}
I've tried changing the permissions on the serial port by following this, so now if I run groups user
I get user : user adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare
, but it still won't work.
I've also checked chrome://device-log
to see if I could find any errors but all I get is info about (physically) adding or removing a USB device.
- I'm having the same issue in Windows, but not on OSX. – MikeiLL Commented Dec 7, 2020 at 23:48
-
1
In the device log, if you plugin and unplug your device, do you see:
Serial->Event: Serial Device Added path=COM3, etc
andUSB->USER: USB Device added, etc
and similar for removed? If I log the output ofport.getSignals
, that's where I'm seeing a Promise with State of "rejected" and Result of "DOMException: The device has been lost" – MikeiLL Commented Dec 8, 2020 at 0:03 - Also, for me, initial connection works, but subsequent connections produce the error. – MikeiLL Commented Dec 8, 2020 at 0:21
2 Answers
Reset to default 4I believe the member name has recently been changed from 'baudrate' to 'baudRate'. At least in my case changing from 'baudrate' (which used to work) to 'baudRate' fixed it for me. Could it perhaps be Kubuntu is using an older chrome version that expects 'baudrate'.
Maybe permission and port select manuality, repost and script below. stackoverflow.
developer.mozilla
<button onclick="SerialRiderStart()">Connect</button>
<button id="Send" onclick="SerialWriter()">Send</button>
<button id="Read" onclick="SerialReader()">Read</button>
<script>
var port = "null";
//////////////////////////Serial begin/////////////////////////
async function SerialRiderStart() {
port = await navigator.serial.requestPort();
await port.open({ baudRate: 9600});
console.log(port.getInfo()); //Start debug.
}
//////////////////////////Serial read//////////////////////////
async function SerialReader() {
let reader = port.readable.getReader();
let ReadData = await reader.read();
reader.releaseLock();
console.log(ReadData.value); //Read debug.
}
//////////////////////////Serial write/////////////////////////
async function SerialWriter() {
let data = new Uint8Array([79, 75, 32]); //"OK" String.
let writer = port.writable.getWriter();
await writer.write(data);
writer.releaseLock();
console.log(data); //Send debug.
}
</script>
本文标签:
版权声明:本文标题:javascript - Web Serial API - Uncaught (in promise) DOMException: Failed to open serial portrequired member baudRate is undefine 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745429332a2658255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论