cp2102 icon indicating copy to clipboard operation
cp2102 copied to clipboard

Change baud rate during run time?

Open brandonros opened this issue 3 years ago • 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)

brandonros avatar Apr 30 '21 18:04 brandonros