better go, but still needs alternative to go-zmq
[svn42.git] / go / door_daemon_zmq / serial_tty.go
index ddd41e6..d7fd7a9 100644 (file)
@@ -9,7 +9,6 @@ import (
     "os"
     "svn.spreadspace.org/realraum/go.svn/termios"
     "log"
-    "sync"
 )
 
 // ---------- Serial TTY Code -------------
@@ -32,53 +31,29 @@ func serialWriter(in <- chan string, serial * os.File) {
     }
 }
 
-var last_read_serial_input [][]byte = [][]byte{{}}
-var last_read_mutex sync.Mutex
-
-func serialReader(topub chan <- [][]byte, serial * os.File) {
+func serialReader(out chan <- [][]byte, serial * os.File) {
     linescanner := bufio.NewScanner(serial)
     linescanner.Split(bufio.ScanLines)
     for linescanner.Scan() {
         if err := linescanner.Err(); err != nil {
             panic(fmt.Sprintf("Error in read from serial: %v\n",err.Error()))
         }
-        fmt.Println("read text", linescanner.Text())
         text := bytes.Fields([]byte(linescanner.Text()))
         if len(text) == 0 {
             continue
         }
-        //~ for len(serial_read) > 5 {
-            //~ //drain channel before putting new line into it
-            //~ //thus we make sure "out" only ever holds the last line
-            //~ //thus the buffer never blocks and we don't need to read from out unless we need it
-            //~ // BUT: don't drain the chan dry, or we might have a race condition resulting in a deadlock
-            //~ <- serial_read
-        //~ }
-        last_read_mutex.Lock()
-        last_read_serial_input = text
-        fmt.Println("Put Text", text)
-        last_read_mutex.Unlock()
-        topub <- text
+        out <- text
     }
 }
 
-//TODO: improve this, make it work for multiple open serial devices
-func GetLastSerialLine() [][]byte {
-    var last_line_pointer [][]byte
-    last_read_mutex.Lock()
-    last_line_pointer = last_read_serial_input
-    last_read_mutex.Unlock()
-    fmt.Println("Retrieve Text", last_line_pointer)
-    return last_line_pointer
-}
-
-func OpenAndHandleSerial(filename string, topub chan <- [][]byte) (chan string, error) {
+func OpenAndHandleSerial(filename string) (chan string, chan [][]byte, error) {
     serial, err :=openTTY(filename)
     if err != nil {
-        return nil, err
+        return nil, nil, err
     }
     wr := make(chan string)
+       rd := make(chan [][]byte, 10)
     go serialWriter(wr, serial)
-    go serialReader(topub, serial)
-    return wr, nil
+    go serialReader(rd, serial)
+    return wr, rd, nil
 }