From 855d42e6e2565069cb0eedc2788548ef33dacb0e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 25 Mar 2016 00:03:40 +0100 Subject: rename serial port source file diff --git a/src/rhctl/serial_port.go b/src/rhctl/serial_port.go new file mode 100644 index 0000000..d805bdc --- /dev/null +++ b/src/rhctl/serial_port.go @@ -0,0 +1,87 @@ +// +// rhctl +// +// Copyright (C) 2009-2016 Christian Pointner +// +// 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 . +// + +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 +} diff --git a/src/rhctl/serialport.go b/src/rhctl/serialport.go deleted file mode 100644 index d805bdc..0000000 --- a/src/rhctl/serialport.go +++ /dev/null @@ -1,87 +0,0 @@ -// -// rhctl -// -// Copyright (C) 2009-2016 Christian Pointner -// -// 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 . -// - -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 -} -- cgit v0.10.2