From 57cd9cad87753c0ba5ea8d3e5b571a263f20fc56 Mon Sep 17 00:00:00 2001 From: Iain Nash Date: Fri, 13 Dec 2013 17:24:52 -0800 Subject: [PATCH] Added linux serial support for corsscompiling --- serial_linux.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ serial_posix.go | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 serial_linux.go diff --git a/serial_linux.go b/serial_linux.go new file mode 100644 index 0000000..6b88543 --- /dev/null +++ b/serial_linux.go @@ -0,0 +1,86 @@ +// +build linux,!cgo + +package serial + +import ( + "os" + "syscall" + "io" + "unsafe" +) + +func openPort(name string, baud int) (rwc io.ReadWriteCloser, err error) { + + var bauds = map[int]uint32{ + 50: syscall.B50, + 75: syscall.B75, + 110: syscall.B110, + 134: syscall.B134, + 150: syscall.B150, + 200: syscall.B200, + 300: syscall.B300, + 600: syscall.B600, + 1200: syscall.B1200, + 1800: syscall.B1800, + 2400: syscall.B2400, + 4800: syscall.B4800, + 9600: syscall.B9600, + 19200: syscall.B19200, + 38400: syscall.B38400, + 57600: syscall.B57600, + 115200: syscall.B115200, + 230400: syscall.B230400, + 460800: syscall.B460800, + 500000: syscall.B500000, + 576000: syscall.B576000, + 921600: syscall.B921600, + 1000000: syscall.B1000000, + 1152000: syscall.B1152000, + 1500000: syscall.B1500000, + 2000000: syscall.B2000000, + 2500000: syscall.B2500000, + 3000000: syscall.B3000000, + 3500000: syscall.B3500000, + 4000000: syscall.B4000000, + } + + rate := bauds[baud] + + f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666) + if err != nil { + return nil, err + } + + defer func(){ + if err != nil && f != nil { + f.Close() + } + }() + + fd := f.Fd() + t := syscall.Termios{ + Iflag: syscall.IGNPAR, + Cflag: syscall.CS8 | syscall.CREAD | syscall.CLOCAL | rate, + Cc: [32]uint8{syscall.VMIN: 1}, + Ispeed: rate, + Ospeed: rate, + } + + if _, _, errno := syscall.Syscall6( + syscall.SYS_IOCTL, + uintptr(fd), + uintptr(syscall.TCSETS), + uintptr(unsafe.Pointer(&t)), + 0, + 0, + 0, + ); errno != 0 { + return nil, errno + } + + if err = syscall.SetNonblock(int(fd), false); err != nil { + return + } + + return f, nil +} \ No newline at end of file diff --git a/serial_posix.go b/serial_posix.go index b429223..2eeb608 100644 --- a/serial_posix.go +++ b/serial_posix.go @@ -1,4 +1,4 @@ -// +build !windows +// +build !windows,cgo package serial