diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-03-24 00:49:12 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-03-24 00:49:12 (GMT) |
commit | 2db0caf47dbc642428dc260832090af83f6a8e1c (patch) | |
tree | 9ef4dc9bc1dec6b5aee6e973c77db7b4ade761c6 /src | |
parent | 04a3a6beeaa199dc1a80a711f26de3a41d98ff74 (diff) |
added serial port handling
Diffstat (limited to 'src')
-rw-r--r-- | src/rhctl/main.go | 9 | ||||
-rw-r--r-- | src/rhctl/serialport.go | 87 |
2 files changed, 95 insertions, 1 deletions
diff --git a/src/rhctl/main.go b/src/rhctl/main.go index 430c287..e86eeb0 100644 --- a/src/rhctl/main.go +++ b/src/rhctl/main.go @@ -74,12 +74,19 @@ func main() { var wg sync.WaitGroup + port, err := SerialOpenAndHandle("/dev/ttyUSB0", B38400) + if err != nil { + rhl.Println("Error opening serial:", err) + return + } + port.tx <- "hello world\n" + if webAddr.Get().(string) != "" { wg.Add(1) go func() { defer wg.Done() rhl.Println("starting web-api:", webAddr.Get().(string)) - //StartControlWeb(webAddr.Get().(string)) + //StartWeb(webAddr.Get().(string)) rhl.Println("web-api finished") }() } 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 +} |