tidepool-org/cp2102

Change baud rate during run time?

Opened this issue · 1 comments

I have a need to go from 250k baud to 2m baud during run time. Any suggestions?

I came up with this but I'm missing the data because it is sent so quickly in between baud changes:

const fillBuffer = (chunk) => {
  for (let i = 0; i < chunk.length; ++i) {
    buffer[pointer] = chunk[i]
    pointer += 1
    if (pointer === 4096) {
      console.log('warning: buffer wrapped')
      pointer = 0
    }
  }
}

const initConnection = async (baudRate) => {
  const connection = new CP2102(0x10c4, 0xea60, { baudRate })
  await new Promise((resolve, reject) => connection.on('ready', resolve))
  connection.on('data', fillBuffer)
  return connection
}

let connection = await initConnection(250000)
...
await new Promise((resolve, reject) => connection.close(resolve))
connection.off('data', fillBuffer)
connection = await initConnection(2000000)

Are you missing the data at the beginning of the baud change? If so, I'm wondering if it would help to try to change the baud rate without re-connecting, see https://github.com/tidepool-org/cp2102/blob/develop/index.js#L57-L63 for the USB control transfer that does this.

If it's missing data because of fillBuffer being too slow, maybe used typed arrays (i.e., Uint8Array) instead of loops to copy in the data, which should be faster?

Alternatively, you could also look into using the Web Serial API which is now available in most Chromium-based browsers.