WEBUSB获取串行数据PL2303

时间:2018-07-31 05:17:46

标签: webusb

我正在尝试从通过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,但如何在这里设置。

请帮助。

2 个答案:

答案 0 :(得分:1)

我已将 Gerritpl2303 repository 移植到 WebUSB:https://github.com/Folleon/pl2303-webusb

目前我只需要读取数据,因此我不确定写入是否也有效。但是,至少到目前为止,所有设置和批量读取都运行良好。

请注意,设置波特率失败,输入状态为 stall – 我无法解决这个问题。但是,假设波特率为 9600,它仍然可以工作。

答案 1 :(得分:0)

您确定数据需要解码为文本吗?您要传递给解码()的DataView中的值是什么?

要设置波特率,您需要找到用于配置设备的USB控制传输的文档。我在Prolific的任何数据表中都找不到,但是该芯片的Linux驱动程序可能是一个很好的参考:

https://github.com/torvalds/linux/blob/v4.16/drivers/usb/serial/pl2303.c

该文件中的pl2303_set_termios函数构造发送到设备的命令,以配置诸如波特率和奇偶校验等参数。

相关问题