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

Provide API for reading/writing to serial port via channels #11

Closed
tiziano88 opened this issue Jan 4, 2014 · 4 comments
Closed

Provide API for reading/writing to serial port via channels #11

tiziano88 opened this issue Jan 4, 2014 · 4 comments

Comments

@tiziano88
Copy link

I suggest adding a new method

func OpenPortChan(c *Config) (chan<- []byte, <-chan []byte, error)

that returns a pair of channels that can be used to read and write to the serial port directly, by spawning a couple of goroutines redirecting input/output from/to the port. I have a pull request almost ready, will send it for discussion soon.

@tarm
Copy link
Owner

tarm commented Jan 17, 2015

Hi @tiziano88,

I often end up wrapping the serial port in channel code like that as well, so it is likely a common need.

I think an API like that would be interesting and I'd be happy to review/merge a new API. Please send it when you have a chance.

@tarm tarm closed this as completed Nov 17, 2015
@wichert
Copy link

wichert commented Jun 12, 2016

@tiziano88 Have you made any progress on this?

@tiziano88
Copy link
Author

No, sorry, haven't worked on this for a long time, feel free to pick it up

@tarm
Copy link
Owner

tarm commented Jun 12, 2016

I've been thinking about it more and I don't think it's the right API.

It's certainly convenient initially, but if you want to have timeouts, handle error, and do retries then a channel API does not work as well.

Here's a quick example:
https://play.golang.org/p/zk_03P071w

I annotated that example with comments about some of the issues. By the time you add bidirectional communication (errors, closing of connections, reusing of buffers), then a blocking Read()/Write() api often ends up being simpler.

I recently refactored a bunch of my code to STOP using a pattern like that because I wanted better error handling.

The pattern I am moving towards is having a dedicated goroutine (or 2 if needed) per device. That goroutine does blocking Read()s, handles errors and retries, and then there is a higher level API to communicate with the device. The way I have it, a single goroutine does not handle bytes from multiple devices so no select or channels are needed.

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

3 participants