Skip to content

Instantly share code, notes, and snippets.

@distributed
Created October 13, 2010 21:15
Show Gist options
  • Save distributed/624933 to your computer and use it in GitHub Desktop.
Save distributed/624933 to your computer and use it in GitHub Desktop.
TTY/Serial Baudrate changer for Go
package tc
// Package contains one function to set TTY speed.
/*#include <stddef.h>
#include <stdlib.h>
#include <termios.h>*/
import "C"
import (
"os"
"unsafe"
)
/* Pass an open TTY/CU. Error handling is probably a bit borked.
Works on Mac OS X 10.5, compiles on Linux, though I never tried
to run it there. */
func Setspeed(f *os.File, speed int) (os.Error) {
serfd := f.Fd()
var tio C.struct_termios;
res, err := C.tcgetattr(C.int(serfd), (*C.struct_termios)(unsafe.Pointer(&tio)))
if res != 0 || err != nil {
return err
}
C.cfmakeraw((*C.struct_termios)(unsafe.Pointer(&tio)))
res, err = C.cfsetspeed((*C.struct_termios)(unsafe.Pointer(&tio)), C.speed_t(speed))
if res != 0 || err != nil {
return err
}
res, err = C.tcsetattr(C.int(serfd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(&tio)))
if res != 0 || err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment