X-Git-Url: https://git.realraum.at/?p=svn42.git;a=blobdiff_plain;f=go%2Fdoor_daemon_zmq%2Fserial_tty.go;fp=go%2Fdoor_daemon_zmq%2Fserial_tty.go;h=ddd41e6730cb5078c283e6a591ee2ef2f82d20a8;hp=4ec786af41763a2c0388251992cc3e4a6baed156;hb=02dbcb404955284e9849d80ea56e9c5e02534d40;hpb=05f5975f0266e628e89cf0f60f84f47d43136747 diff --git a/go/door_daemon_zmq/serial_tty.go b/go/door_daemon_zmq/serial_tty.go index 4ec786a..ddd41e6 100644 --- a/go/door_daemon_zmq/serial_tty.go +++ b/go/door_daemon_zmq/serial_tty.go @@ -9,6 +9,7 @@ import ( "os" "svn.spreadspace.org/realraum/go.svn/termios" "log" + "sync" ) // ---------- Serial TTY Code ------------- @@ -24,37 +25,60 @@ func openTTY(name string) (*os.File, error) { return file, nil } -func SerialWriter(in <- chan string, serial * os.File) { +func serialWriter(in <- chan string, serial * os.File) { for totty := range(in) { serial.WriteString(totty) serial.Sync() } } -func SerialReader(out chan <- [][]byte, topub chan <- [][]byte, serial * os.File) { +var last_read_serial_input [][]byte = [][]byte{{}} +var last_read_mutex sync.Mutex + +func serialReader(topub 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 } - out <- text + //~ 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 } } -func OpenAndHandleSerial(filename string, topub chan <- [][]byte) (chan string, chan [][]byte, error) { +//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) { serial, err :=openTTY(filename) if err != nil { - return nil, nil, err + return nil, err } - var wr chan string - var rd chan [][]byte - go SerialWriter(wr, serial) - go SerialReader(rd, topub, serial) - return wr, rd, nil + wr := make(chan string) + go serialWriter(wr, serial) + go serialReader(topub, serial) + return wr, nil }