esptool-js icon indicating copy to clipboard operation
esptool-js copied to clipboard

Read from read loop remove generator logic

Open brianignacio5 opened this issue 2 months ago • 5 comments

Fix #224

Description

Using yield and async generator is not efficient logic for reading the serial port.

This PR introduces readloop as an always reading to a buffer and SLIP parsing from buffer. Seems to be more reliable.

This pull request refactors the serial communication logic to simplify data reading and improve reliability. The main changes focus on removing asynchronous generators in favor of simpler async functions, introducing a new sleep utility, and enhancing debugging and buffer management. The updates affect both the esploader.ts and webserial.ts files, resulting in more maintainable and understandable code.

Serial Communication Refactor:

  • Replaced the asynchronous generator-based read logic in Transport with a simpler async function, removing newRead and generator-based readLoop. The new read method now waits for data with a timeout and processes available bytes in a straightforward loop. (src/webserial.ts) [1] [2]
  • Updated all usages of transport.read().next() and transport.newRead() in ESPLoader to use the new transport.read() interface, simplifying packet reading and improving consistency. (src/esploader.ts) [1] [2] [3] [4]

Utility and Buffer Management:

  • Added a new sleep utility function in util.ts and replaced all internal _sleep usages with this shared function. (src/util.ts, src/esploader.ts, src/webserial.ts) [1] [2] [3] [4]
  • Added a peek() method to Transport for safely inspecting the buffer contents without consuming data, improving boot log detection logic. (src/webserial.ts, src/esploader.ts) [1] [2]

Debugging and Logging Enhancements:

  • Added detailed debug logs for register reads and boot/download mode detection to aid troubleshooting and development. (src/esploader.ts) [1] [2]

Bug Fixes and Logic Improvements:

  • Improved SLIP packet parsing: after a packet is found, any remaining bytes are now correctly preserved in the buffer for future reads. (src/webserial.ts) [1] [2] [3]
  • Updated regular expression for boot log detection to more reliably match multiline logs. (src/esploader.ts)

These changes collectively make the serial communication code easier to understand, more robust, and better suited for debugging and extension.

Testing

Manual testing using examples/typescript commands.


Checklist

Before submitting a Pull Request, please ensure the following:

  • [ ] 🚨 This PR does not introduce breaking changes.
  • [ ] All CI checks (GH Actions) pass.
  • [ ] Documentation is updated as needed.
  • [ ] Tests are updated or added as necessary.
  • [ ] Code is well-commented, especially in complex areas.
  • [ ] Git history is clean — commits are squashed to the minimum necessary.

brianignacio5 avatar Dec 12 '25 11:12 brianignacio5