jacobsa / go-serial

A Go library for dealing with serial ports.
Apache License 2.0
626 stars 120 forks source link

Add Linux support #8

Closed adammck closed 10 years ago

adammck commented 10 years ago

This adds Linux support (including arbitrary baud rates, which is why I'm unable to just use tarm/goserial). It's a new implementation because it uses the Linux-only TCSETS2 ioctl and the termios2 struct, but it shares quite a bit of code with the darwin port. I could refactor the both of them for less redundancy, if you'd prefer that.

I've tested it out on i386 and ARM with actual serial devices, but I don't have an amd64 box handy. Seems to work on a VM with a ptty.

jacobsa commented 10 years ago

Cool, thanks for this.

One question: you said it's a new implementation because it uses termios2, but why does it use termios2? Does the general existing implementation not work on Linux? (I have no idea; just asking.)

adammck commented 10 years ago

The termios implementation works just fine on Linux for standard POSIX baud rates, but unfortunately doesn't support arbitrary rates. It's possible to hack around it with "baud rate aliasing", but it's inaccurate and seems to be deprecated.

adammck commented 10 years ago

Alternatively, we could use the termios interface to set the standard options on Linux, and follow up with calls to TCGETS2 and TCSETS2 to set the custom baud rate. This is the approach which pySerial takes for Unix-like platforms. I avoided it because it seemed kind of gross, given that there is a more modern interface which works, but it would allow us to share the convertOptions function with the existing darwin port.

jacobsa commented 10 years ago

Oh right, I forgot about the arbitrary baud rates; sorry. This is fine with me. Thank you!