-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
WIP feat: Async-Iterator interface #1850
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some API comments based on knowledge of other AsyncIterable interfaces.
This comment has been minimized.
This comment has been minimized.
3d72b33
to
a6cb875
Compare
bf17c44
to
92734b3
Compare
This is the next generation of serial port interfaces. Streams are never going away but the async iterator interface is much more flexible. It can be combined with async-iterator tools (such as [streaming-iterables](https://www.npmjs.com/package/streaming-iterables)) to make buffers and parsers, and can even be combined with our existing stream based parsers. This is very experimental. I’ve tried to bring a lot of these changes in https://github.com/node-serialport/node-serialport/tree/reconbot/typescript2 but I haven’t had time for a full typescript rewrite. So maybe this smaller api change lets us get some of these advantages without having to rewrite everything. ## Todo - [ ] api feedback - [ ] docs for website and readme - [ ] abstract away get/set/update borrowing from #1679 for local and remote state and parity with web serial - [ ] tests ## Example Usage ```js const { open, list } = require('@serialport/async-iterator') const ports = await list() const arduinoPort = ports.find(info => (info.manufacture || '').includes('Arduino')) const port = await open(arduinoPort) // read bytes until close for await (const bytes of port) { console.log(`read ${bytes.length} bytes`) } // read 12 bytes const { value, end } = await port.next(12) console.log(`read ${value.length} bytes / port closed: ${end}`) // write a buffer await port.write(Buffer.from('hello!')) ```
92734b3
to
5ab68b6
Compare
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week no further activity occurs. Feel free continue the discussion or ask for a |
So I am porting a Python program that relies very extensively on writing commands via the serial port and getting replies back synchronously. It is not the kind of thing that can be rewritten as callbacks easily. I came across this thread when looking for a better solution. The async iterator is not exactly what I was hoping for,, so I thought I would share what does seem to be working for me:
The whole trick here is to make all the I/O operations into Promises and use ECMA 6 await routines to enforce sequencing. Seems to work well and behaves exactly as I would expect it to work in a sequential language. This way of interacting with a serial port seems natural to me, but I am not a node.js programmer, so that probably plays a part. |
This is the next generation of serial port interfaces. Streams are never going away but the async iterator interface is much more flexible. It can be combined with async-iterator tools (such as streaming-iterables) to make buffers and parsers, and can even be combined with our existing stream based parsers.
This is very experimental. I’ve tried to bring a lot of these changes in https://github.com/node-serialport/node-serialport/tree/reconbot/typescript2 but I haven’t had time for a full typescript rewrite. So maybe this smaller api change lets us get some of these advantages without having to rewrite everything.
Todo
Example Usage
closes #991