From 0220709d39b6394964a7dfed3edc9a860a4de81f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 16 Apr 2024 17:17:14 +0200 Subject: [PATCH 1/6] Removed unused variable --- conn.go | 4 ---- 1 file changed, 4 deletions(-) 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) From 9f84d0d4f6d610c8a962c09968e71c8de004a4e0 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 16 Apr 2024 17:18:20 +0200 Subject: [PATCH 2/6] Removed always-true condition --- serialport.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/serialport.go b/serialport.go index 43c810ff..c70c683f 100755 --- a/serialport.go +++ b/serialport.go @@ -161,14 +161,12 @@ 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 { From 726758f21f5fad29c68d7f80189146c142cb7b6f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 17 Apr 2024 11:36:53 +0200 Subject: [PATCH 3/6] Make Write a method of serport --- serial.go | 15 +-------------- serialport.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/serial.go b/serial.go index 96778734..0a144292 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() @@ -328,5 +315,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 c70c683f..7ebb8c27 100755 --- a/serialport.go +++ b/serialport.go @@ -174,6 +174,19 @@ func (p *serport) reader(buftype string) { } } +// 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 + } +} + // this method runs as its own thread because it's instantiated // as a "go" method. so if it blocks inside, it is ok func (p *serport) writerBuffered() { From 38f70e91a3e43af720b12ddf5fe31cbacbd7de64 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 17 Apr 2024 11:38:00 +0200 Subject: [PATCH 4/6] Removed useless counter itemsInBuffer --- serialport.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/serialport.go b/serialport.go index 7ebb8c27..0b6095ec 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 @@ -224,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) From 3bcbd96167a68dc6034621fd1aba7f54e315c7f9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 17 Apr 2024 11:48:56 +0200 Subject: [PATCH 5/6] Made spCloseRead a method of serialport --- serialport.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/serialport.go b/serialport.go index 0b6095ec..7ef6dda7 100755 --- a/serialport.go +++ b/serialport.go @@ -167,7 +167,7 @@ func (p *serport) reader(buftype string) { } } if p.isClosingDueToError { - spCloseReal(p) + p.Close() } } @@ -348,12 +348,12 @@ func spHandlerOpen(portname string, baud int, buftype string) { } func spHandlerClose(p *serport) { - p.isClosing = true h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name) - spCloseReal(p) + p.Close() } -func spCloseReal(p *serport) { +func (p *serport) Close() { + p.isClosing = true p.bufferwatcher.Close() p.portIo.Close() serialPorts.MarkPortAsClosed(p.portName) From c76f9e5afc39f0aed0b1062fdf7e1e36634a6a7f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 17 Apr 2024 11:50:51 +0200 Subject: [PATCH 6/6] Inlined spHandlerClose --- serial.go | 14 +++----------- serialport.go | 5 ----- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/serial.go b/serial.go index 0a144292..64e5b8f7 100755 --- a/serial.go +++ b/serial.go @@ -262,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.") } } diff --git a/serialport.go b/serialport.go index 7ef6dda7..a11483f6 100755 --- a/serialport.go +++ b/serialport.go @@ -347,11 +347,6 @@ func spHandlerOpen(portname string, baud int, buftype string) { serialPorts.List() } -func spHandlerClose(p *serport) { - h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name) - p.Close() -} - func (p *serport) Close() { p.isClosing = true p.bufferwatcher.Close()