"os"
"svn.spreadspace.org/realraum/go.svn/termios"
"log"
+ "sync"
)
// ---------- Serial TTY Code -------------
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
}