Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change baud rate during run time? #15

Open
brandonros opened this issue Apr 30, 2021 · 1 comment
Open

Change baud rate during run time? #15

brandonros opened this issue Apr 30, 2021 · 1 comment

Comments

@brandonros
Copy link

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)
@gniezen
Copy link
Member

gniezen commented May 4, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants