0

I am trying to get a reading from a weighing scale which communicates through RS-232 serial communication, value into a web application using WebUsb API. I am getting the data but after decoding it is coming like this . I am getting the correct data in the serial terminal. Here is the code for the data.

`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);
  });  `

Am I missing something regarding this. is it the baud rate setting, i know the baud rate is 9600 but how to set it here.

Please Help.

Haresh
  • 207
  • 1
  • 4
  • 11
  • hey hi can you help me ,how you solved this issue ? – gANDALF Oct 03 '20 at 13:54
  • Seems like ,I am caught in same problem -https://stackoverflow.com/questions/64059658/web-usb-transfer-data-from-device-to-browser-not-working?noredirect=1#comment113497428_64059658 – gANDALF Oct 03 '20 at 13:54

2 Answers2

1

Are you sure that the data needs to be decoded as text? What are the values in the DataView you are passing to decode()?

To set the baud rate you need to find documentation for the USB control transfers used to configure the device. I have not been able to find this in any of Prolific's datasheets however the Linux driver for this chip is likely a good reference:

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

The pl2303_set_termios function in that file constructs the commands sent to the device in order to configure parameters such as the baud rate and also parity, etc.

Reilly Grant
  • 5,590
  • 1
  • 13
  • 23
  • I recently released a PL2303 driver for NodeJS (unfortunately using node-usb, not webusb ;) so it may be easier to read than the C driver code: https://github.com/tidepool-org/pl2303/blob/master/index.js – Gerrit Jul 31 '18 at 20:52
  • 1
    @Gerrit Your Library really worked....looking at the library i have wriiten it using webusb.... The issue is setting of baud rate. Thanks a lot. – Haresh Aug 02 '18 at 11:21
1

I've ported Gerrit's pl2303 repository to WebUSB: https://github.com/Folleon/pl2303-webusb

At the moment I only need to read data, thus I'm not sure if writing works as well. However, at least all the setup and batch reading works nicely so far.

Note though that setting the baud rate failed with the input being of the status stall – and I couldn't get around that. However, it works still, assuming with a baud rate of 9600.

sgruetter
  • 131
  • 1
  • 4