diff --git a/conn.go b/conn.go index 02b13ba8..727a5cad 100644 --- a/conn.go +++ b/conn.go @@ -124,9 +124,6 @@ func uploadHandler(c *gin.Context) { return } - var filePaths []string - filePaths = append(filePaths, filePath) - tmpdir, err := os.MkdirTemp("", "extrafiles") if err != nil { c.String(http.StatusBadRequest, err.Error()) @@ -139,7 +136,6 @@ func uploadHandler(c *gin.Context) { c.String(http.StatusBadRequest, err.Error()) return } - filePaths = append(filePaths, path) log.Printf("Saving %s on %s", extraFile.Filename, path) err = os.MkdirAll(filepath.Dir(path), 0744) diff --git a/serial.go b/serial.go index 96778734..64e5b8f7 100755 --- a/serial.go +++ b/serial.go @@ -83,19 +83,6 @@ func (sh *serialhub) Unregister(port *serport) { sh.mu.Unlock() } -// Write data to the serial port. -func (sh *serialhub) Write(port *serport, data string, sendMode string) { - // if user sent in the commands as one text mode line - switch sendMode { - case "send": - port.sendBuffered <- data - case "sendnobuf": - port.sendNoBuf <- []byte(data) - case "sendraw": - port.sendRaw <- data - } -} - func (sh *serialhub) FindPortByName(portname string) (*serport, bool) { sh.mu.Lock() defer sh.mu.Unlock() @@ -275,18 +262,10 @@ func spErr(err string) { } func spClose(portname string) { - // look up the registered port by name - // then call the close method inside serialport - // that should cause an unregister channel call back - // to myself - - myport, isFound := sh.FindPortByName(portname) - - if isFound { - // we found our port - spHandlerClose(myport) + if myport, ok := sh.FindPortByName(portname); ok { + h.broadcastSys <- []byte("Closing serial port " + portname) + myport.Close() } else { - // we couldn't find the port, so send err spErr("We could not find the serial port " + portname + " that you were trying to close.") } } @@ -328,5 +307,5 @@ func spWrite(arg string) { } // send it to the write channel - sh.Write(port, data, bufferingMode) + port.Write(data, bufferingMode) } diff --git a/serialport.go b/serialport.go index 43c810ff..a11483f6 100755 --- a/serialport.go +++ b/serialport.go @@ -47,9 +47,6 @@ type serport struct { isClosingDueToError bool - // counter incremented on queue, decremented on write - itemsInBuffer int - // buffered channel containing up to 25600 outbound messages. sendBuffered chan string @@ -161,18 +158,29 @@ func (p *serport) reader(buftype string) { // Keep track of time difference between two consecutive read with n == 0 and err == nil // we get here if the port has been disconnected while open (cpu usage will jump to 100%) // let's close the port only if the events are extremely fast (<1ms) - if err == nil { - diff := time.Since(timeCheckOpen) - if diff.Nanoseconds() < 1000000 { - p.isClosingDueToError = true - break - } - timeCheckOpen = time.Now() + diff := time.Since(timeCheckOpen) + if diff.Nanoseconds() < 1000000 { + p.isClosingDueToError = true + break } + timeCheckOpen = time.Now() } } if p.isClosingDueToError { - spCloseReal(p) + p.Close() + } +} + +// Write data to the serial port. +func (p *serport) Write(data string, sendMode string) { + // if user sent in the commands as one text mode line + switch sendMode { + case "send": + p.sendBuffered <- data + case "sendnobuf": + p.sendNoBuf <- []byte(data) + case "sendraw": + p.sendRaw <- data } } @@ -213,10 +221,6 @@ func (p *serport) writerNoBuf() { // if we get here, we were able to write successfully // to the serial port because it blocks until it can write - // decrement counter - p.itemsInBuffer-- - log.Printf("itemsInBuffer:%v\n", p.itemsInBuffer) - // FINALLY, OF ALL THE CODE IN THIS PROJECT // WE TRULY/FINALLY GET TO WRITE TO THE SERIAL PORT! n2, err := p.portIo.Write(data) @@ -343,13 +347,8 @@ func spHandlerOpen(portname string, baud int, buftype string) { serialPorts.List() } -func spHandlerClose(p *serport) { +func (p *serport) Close() { p.isClosing = true - h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name) - spCloseReal(p) -} - -func spCloseReal(p *serport) { p.bufferwatcher.Close() p.portIo.Close() serialPorts.MarkPortAsClosed(p.portName)