|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "github.com/tarm/goserial" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +type serport struct { |
| 12 | + // The serial port connection. |
| 13 | + portConf *serial.Config |
| 14 | + portIo io.ReadWriteCloser |
| 15 | + |
| 16 | + // Keep track of whether we're being actively closed |
| 17 | + // just so we don't show scary error messages |
| 18 | + isClosing bool |
| 19 | + |
| 20 | + // Buffered channel of outbound messages. |
| 21 | + send chan []byte |
| 22 | +} |
| 23 | + |
| 24 | +func (p *serport) reader() { |
| 25 | + //var buf bytes.Buffer |
| 26 | + for { |
| 27 | + ch := make([]byte, 1024) |
| 28 | + n, err := p.portIo.Read(ch) |
| 29 | + |
| 30 | + // read can return legitimate bytes as well as an error |
| 31 | + // so process the bytes if n > 0 |
| 32 | + if n > 0 { |
| 33 | + log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(ch)) |
| 34 | + h.broadcastSys <- []byte("{p: '" + p.portConf.Name + "', d: '" + string(ch[:n]) + "'}\n") |
| 35 | + } |
| 36 | + |
| 37 | + if p.isClosing { |
| 38 | + strmsg := "Shutting down reader on " + p.portConf.Name |
| 39 | + log.Println(strmsg) |
| 40 | + h.broadcastSys <- []byte(strmsg) |
| 41 | + break |
| 42 | + } |
| 43 | + |
| 44 | + if err == io.EOF || err == io.ErrUnexpectedEOF { |
| 45 | + // hit end of file |
| 46 | + log.Println("Hit end of file on serial port") |
| 47 | + } |
| 48 | + if err != nil { |
| 49 | + log.Println(err) |
| 50 | + h.broadcastSys <- []byte("Error reading on " + p.portConf.Name + " " + |
| 51 | + err.Error() + " Closing port.") |
| 52 | + break |
| 53 | + } |
| 54 | + |
| 55 | + // loop thru and look for a newline |
| 56 | + /* |
| 57 | + for i := 0; i < n; i++ { |
| 58 | + // see if we hit a newline |
| 59 | + if ch[i] == '\n' { |
| 60 | + // we are done with the line |
| 61 | + h.broadcastSys <- buf.Bytes() |
| 62 | + buf.Reset() |
| 63 | + } else { |
| 64 | + // append to buffer |
| 65 | + buf.WriteString(string(ch[:n])) |
| 66 | + } |
| 67 | + }*/ |
| 68 | + /* |
| 69 | + buf.WriteString(string(ch[:n])) |
| 70 | + log.Print(string(ch[:n])) |
| 71 | + if string(ch[:n]) == "\n" { |
| 72 | + h.broadcastSys <- buf.Bytes() |
| 73 | + buf.Reset() |
| 74 | + } |
| 75 | + */ |
| 76 | + } |
| 77 | + p.portIo.Close() |
| 78 | +} |
| 79 | + |
| 80 | +func (p *serport) writer() { |
| 81 | + for data := range p.send { |
| 82 | + n2, err := p.portIo.Write(data) |
| 83 | + log.Print("Just wrote ") |
| 84 | + log.Print(n2) |
| 85 | + log.Print(" bytes to serial: ") |
| 86 | + log.Print(data) |
| 87 | + if err != nil { |
| 88 | + errstr := "Error writing to " + p.portConf.Name + " " + err.Error() + " Closing port." |
| 89 | + log.Fatal(errstr) |
| 90 | + h.broadcastSys <- []byte(errstr) |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + msgstr := "Shutting down writer on " + p.portConf.Name |
| 95 | + log.Println(msgstr) |
| 96 | + h.broadcastSys <- []byte(msgstr) |
| 97 | + p.portIo.Close() |
| 98 | +} |
| 99 | + |
| 100 | +func spHandlerOpen(portname string, baud int) { |
| 101 | + |
| 102 | + log.Print("Inside spHandler") |
| 103 | + |
| 104 | + var out bytes.Buffer |
| 105 | + |
| 106 | + out.WriteString("Opening serial port ") |
| 107 | + out.WriteString(portname) |
| 108 | + out.WriteString(" at ") |
| 109 | + out.WriteString(strconv.Itoa(baud)) |
| 110 | + out.WriteString(" baud") |
| 111 | + log.Print(out.String()) |
| 112 | + |
| 113 | + //h.broadcast <- []byte("Opened a serial port bitches") |
| 114 | + h.broadcastSys <- out.Bytes() |
| 115 | + |
| 116 | + conf := &serial.Config{Name: portname, Baud: baud} |
| 117 | + log.Print("Created config for port") |
| 118 | + log.Print(conf) |
| 119 | + |
| 120 | + sp, err := serial.OpenPort(conf) |
| 121 | + log.Print("Just tried to open port") |
| 122 | + if err != nil { |
| 123 | + //log.Fatal(err) |
| 124 | + log.Print("Error opening port " + err.Error()) |
| 125 | + h.broadcastSys <- []byte("Error opening port. " + err.Error()) |
| 126 | + return |
| 127 | + } |
| 128 | + log.Print("Opened port successfully") |
| 129 | + p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp} |
| 130 | + sh.register <- p |
| 131 | + defer func() { sh.unregister <- p }() |
| 132 | + go p.writer() |
| 133 | + p.reader() |
| 134 | +} |
| 135 | + |
| 136 | +func spHandlerClose(p *serport) { |
| 137 | + p.isClosing = true |
| 138 | + // close the port |
| 139 | + p.portIo.Close() |
| 140 | + // unregister myself |
| 141 | + // we already have a deferred unregister in place from when |
| 142 | + // we opened. the only thing holding up that thread is the p.reader() |
| 143 | + // so if we close the reader we should get an exit |
| 144 | + h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name) |
| 145 | +} |
0 commit comments