You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
836 B
Go
48 lines
836 B
Go
1 year ago
|
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
|
||
|
}
|