parent
e99030b919
commit
e1d3529e22
@ -0,0 +1,47 @@
|
||||
package nnet
|
||||
|
||||
import (
|
||||
"git.noahlan.cn/noahlan/nnet/connection"
|
||||
"git.noahlan.cn/noahlan/ntool/nlog"
|
||||
"github.com/goburrow/serial"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (ngin *Engine) ServerRTU(conf serial.Config) error {
|
||||
err := ngin.setup()
|
||||
if err != nil {
|
||||
nlog.Errorf("%s failed to setup server, err:%v", ngin.LogPrefix(), err)
|
||||
return err
|
||||
}
|
||||
|
||||
port, err := serial.Open(&conf)
|
||||
if err != nil {
|
||||
nlog.Errorf("%s failed to open %s, err:%v", ngin.LogPrefix(), conf.Address, err)
|
||||
return err
|
||||
}
|
||||
|
||||
nlog.Infof("%s now open %s at rate:%d ...", ngin.LogPrefix(), conf.Address, conf.BaudRate)
|
||||
defer func() {
|
||||
_ = port.Close()
|
||||
ngin.Stop()
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
ngin.handle(connection.NewRTUConn(port, &conf))
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ngin.dieChan:
|
||||
wg.Done()
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue