package nnet import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/ntool/nlog" "github.com/goburrow/serial" "sync" ) // ListenSerial 监听串口消息 func (ngin *Engine) ListenSerial(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.NewSerialConn(port, &conf)) go func() { for { select { case <-ngin.dieChan: wg.Done() default: } } }() wg.Wait() return nil }