summaryrefslogtreecommitdiff
path: root/src/rhctl/serialport.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhctl/serialport.go')
-rw-r--r--src/rhctl/serialport.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/rhctl/serialport.go b/src/rhctl/serialport.go
new file mode 100644
index 0000000..d805bdc
--- /dev/null
+++ b/src/rhctl/serialport.go
@@ -0,0 +1,87 @@
+//
+// rhctl
+//
+// Copyright (C) 2009-2016 Christian Pointner <equinox@helsinki.at>
+//
+// This file is part of rhctl.
+//
+// rhctl is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// any later version.
+//
+// rhctl is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with rhctl. If not, see <http://www.gnu.org/licenses/>.
+//
+
+package main
+
+import (
+ "bufio"
+ "syscall"
+
+ "github.com/schleibinger/sio"
+)
+
+type Baudrate uint32
+
+const (
+ B1200 Baudrate = syscall.B1200
+ B2400 = syscall.B2400
+ B4800 = syscall.B4800
+ B9600 = syscall.B9600
+ B19200 = syscall.B19200
+ B38400 = syscall.B38400
+ B57600 = syscall.B57600
+ B115200 = syscall.B115200
+)
+
+type SerialLine string
+
+type SerialPort struct {
+ port *sio.Port
+ rx <-chan SerialLine
+ tx chan<- SerialLine
+}
+
+func SerialRead(c chan<- SerialLine, port *sio.Port) {
+ scanner := bufio.NewScanner(port)
+ scanner.Split(bufio.ScanLines)
+ for scanner.Scan() {
+ if err := scanner.Err(); err != nil {
+ panic(err.Error())
+ }
+ data := scanner.Text()
+ if len(data) == 0 {
+ continue
+ }
+ c <- SerialLine(data)
+ }
+}
+
+func SerialWrite(c <-chan SerialLine, port *sio.Port) {
+ for data := range c {
+ port.Write([]byte(data))
+ }
+ port.Close()
+}
+
+func SerialOpenAndHandle(device string, speed Baudrate) (port *SerialPort, err error) {
+ port = &SerialPort{}
+ if port.port, err = sio.Open(device, uint32(speed)); err != nil {
+ return
+ }
+ tx := make(chan SerialLine, 1)
+ rx := make(chan SerialLine, 20)
+ go SerialRead(rx, port.port)
+ go SerialWrite(tx, port.port)
+
+ port.rx = rx
+ port.tx = tx
+ return
+}