admin管理员组文章数量:1023758
I am trying to connect to the serial port from a web page. I found the Serial API which can support that.
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);
I am trying to connect to the serial port from a web page. I found the Serial API which can support that.
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);
But the navigator.serial.requestPort()
call is failing; I find that the navigator.serial is undefined. I'm pretty sure the serial port is connected to my puter. Are functions on navigator.serial and SerialPort in Serial API unimplemented? My chrome version is 74.0.3729.131. My system is Ubuntu 16.04
-
Now available on Chrome 77 beta, under
chrome://flags/#enable-experimental-web-platform-features
. – BenMorel Commented Aug 21, 2019 at 22:08
2 Answers
Reset to default 3Posting @Benjamin 's answer for the sake of clarity. Serial port connection's are still an "experimental" feature in Chrome early 2020. You have to turn on the following option:
chrome://flags/#enable-experimental-web-platform-features
Just copy paste that line in the Chrome address bar and enable it.
The Serial API is not yet pletely implemented in Chrome. I see that you have already found the issue in the Chromium bug tracker for the implementation. Updates on implementation progress will be posted there. It is currently in the "started" state, not "fixed".
I am trying to connect to the serial port from a web page. I found the Serial API which can support that.
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);
I am trying to connect to the serial port from a web page. I found the Serial API which can support that.
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);
But the navigator.serial.requestPort()
call is failing; I find that the navigator.serial is undefined. I'm pretty sure the serial port is connected to my puter. Are functions on navigator.serial and SerialPort in Serial API unimplemented? My chrome version is 74.0.3729.131. My system is Ubuntu 16.04
-
Now available on Chrome 77 beta, under
chrome://flags/#enable-experimental-web-platform-features
. – BenMorel Commented Aug 21, 2019 at 22:08
2 Answers
Reset to default 3Posting @Benjamin 's answer for the sake of clarity. Serial port connection's are still an "experimental" feature in Chrome early 2020. You have to turn on the following option:
chrome://flags/#enable-experimental-web-platform-features
Just copy paste that line in the Chrome address bar and enable it.
The Serial API is not yet pletely implemented in Chrome. I see that you have already found the issue in the Chromium bug tracker for the implementation. Updates on implementation progress will be posted there. It is currently in the "started" state, not "fixed".
本文标签: javascriptAre functions on navigatorserial and SerialPort in Serial API unimplementedStack Overflow
版权声明:本文标题:javascript - Are functions on navigator.serial and SerialPort in Serial API unimplemented? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745603607a2158601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论