我正在尝试从通过 RS-232 串行通信进行通信的秤上获取读数,并将值使用 WebUsb API 传输到 Web 应用程序中。我正在获取数据,但解码后它是这样的�
。我在串行终端中获得了正确的数据。
这是数据的代码。
`navigator.usb.requestDevice({ filters: []})
.then((requestedDevice) => {
device = requestedDevice;
}).then(() => {
console.log(device);
return device.open();
}).then(() => device.selectConfiguration(1)) // Select configuration #1 for the device.
.then(() => {
return device.reset();
}).then(() => device.claimInterface(0))
.then(() => {
return device.transferIn(1, 16)
})
.then((data) => {
console.log(data)
console.log(new TextDecoder().decode(data.data));
}).catch(err => {
console.log(err);
}); `
我是否遗漏了一些与此相关的内容?是波特率设置吗,我知道波特率是9600,但是这里如何设置。
请帮忙。
您确定数据需要解码为文本吗?您传递给decode() 的DataView 中的值是什么?
要设置波特率,您需要查找用于配置设备的 USB 控制传输的文档。我在 Prolific 的任何数据表中都找不到这一点,但是该芯片的 Linux 驱动程序可能是一个很好的参考:
https://github.com/torvalds/linux/blob/v4.16/drivers/usb/serial/pl2303.c https://github.com/torvalds/linux/blob/v4.16/drivers/usb/serial/pl2303.c
The pl2303_set_termios
该文件中的函数构造发送到设备的命令,以便配置波特率和奇偶校验等参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)