diff --git a/go.mod b/go.mod index 4544601..9da1151 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,3 @@ module github.com/robinson/gos7 -go 1.21.1 - -require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 - -require golang.org/x/sys v0.12.0 // indirect +go 1.21 diff --git a/go.sum b/go.sum index b36cffd..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +0,0 @@ -github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= -github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/serial.go b/serial.go deleted file mode 100644 index ffbdd6c..0000000 --- a/serial.go +++ /dev/null @@ -1,105 +0,0 @@ -package gos7 - -// Copyright 2018 Trung Hieu Le. All rights reserved. -// This software may be modified and distributed under the terms -// of the BSD license. See the LICENSE file for details. - -//tarm serial from https://github.com/tarm/serial -//godoc: https://godoc.org/github.com/tarm/serial - -import ( - "io" - "log" - "sync" - "time" - - "github.com/tarm/serial" -) - -const ( - // Default timeout - serialTimeout = 5 * time.Second - serialIdleTimeout = 60 * time.Second -) - -// serialPort has configuration and I/O controller. -type serialPort struct { - // Serial port configuration. - serial.Config - - Logger *log.Logger - IdleTimeout time.Duration - - mu sync.Mutex - // port is platform-dependent data structure for serial port. - port io.ReadWriteCloser - lastActivity time.Time - closeTimer *time.Timer -} - -func (mb *serialPort) Connect() (err error) { - mb.mu.Lock() - defer mb.mu.Unlock() - - return mb.connect() -} - -// connect connects to the serial port if it is not connected. Caller must hold the mutex. -func (mb *serialPort) connect() error { - if mb.port == nil { - port, err := serial.OpenPort(&mb.Config) - if err != nil { - return err - } - mb.port = port - } - return nil -} - -func (mb *serialPort) Close() (err error) { - mb.mu.Lock() - defer mb.mu.Unlock() - - return mb.close() -} - -// close closes the serial port if it is connected. Caller must hold the mutex. -func (mb *serialPort) close() (err error) { - if mb.port != nil { - err = mb.port.Close() - mb.port = nil - } - return -} - -func (mb *serialPort) logf(format string, v ...interface{}) { - if mb.Logger != nil { - mb.Logger.Printf(format, v...) - } -} - -func (mb *serialPort) startCloseTimer() { - if mb.IdleTimeout <= 0 { - return - } - if mb.closeTimer == nil { - mb.closeTimer = time.AfterFunc(mb.IdleTimeout, mb.closeIdle) - } else { - mb.closeTimer.Reset(mb.IdleTimeout) - } -} - -// closeIdle closes the connection if last activity is passed behind IdleTimeout. -func (mb *serialPort) closeIdle() { - mb.mu.Lock() - defer mb.mu.Unlock() - - if mb.IdleTimeout <= 0 { - return - } - idle := time.Now().Sub(mb.lastActivity) - if idle >= mb.IdleTimeout { - mb.logf("modbus: closing connection due to idle timeout: %v", idle) - mb.close() - } -} diff --git a/serial_test.go b/serial_test.go deleted file mode 100644 index 6307606..0000000 --- a/serial_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package gos7 - -import ( - "bytes" - "io" - "testing" - "time" -) - -type nopCloser struct { - io.ReadWriter - - closed bool -} - -func (n *nopCloser) Close() error { - n.closed = true - return nil -} - -func TestSerialCloseIdle(t *testing.T) { - port := &nopCloser{ - ReadWriter: &bytes.Buffer{}, - } - s := serialPort{ - port: port, - IdleTimeout: 100 * time.Millisecond, - } - s.lastActivity = time.Now() - s.startCloseTimer() - - time.Sleep(150 * time.Millisecond) - if !port.closed || s.port != nil { - t.Fatalf("serial port is not closed when inactivity: %+v", port) - } -}