public IoSession Open(SerialPortConfig config = null) { using (var scope = ObjectHost.Host.BeginLifetimeScope()) { if (!IsOpened()) { try { config = config ?? new SerialPortConfig(); endpoint = new SerialEndPoint(config.PortName, config.BaudRate); var serial = new SerialConnector(); serial.FilterChain.AddLast("logger", new LoggingFilter()); serial.FilterChain.AddLast("codec", new ProtocolCodecFilter(new PacketCodecFactory())); serial.FilterChain.AddLast("exceptionCounter", scope.Resolve<ExceptionCounterFilter>()); serial.Handler = scope.Resolve<PacketHandler>(); serial.SessionCreated += (sender, e) => { e.Session.SetAttributeIfAbsent(KeyName.SESSION_ERROR_COUNTER, 0); }; future = serial.Connect(endpoint); future.Await(); } catch (Exception ex) { Log.Error(ErrorCode.SerialPortSessionOpenError, ex); throw new SerialSessionOpenException(endpoint, ex); } } } if (IsOpened()) return future.Session; else throw new SerialSessionOpenException(endpoint); }
public IoSession Open(SerialPortConfig config = null) { using (var scope = ObjectHost.Host.BeginLifetimeScope()) { if (!IsOpened()) { try { config = config ?? new SerialPortConfig(); endpoint = new SerialEndPoint(config.PortName, config.BaudRate); var serial = new SerialConnector(); serial.FilterChain.AddLast("logger", new LoggingFilter()); serial.FilterChain.AddLast("codec", new ProtocolCodecFilter(new PacketCodecFactory())); serial.FilterChain.AddLast("exceptionCounter", scope.Resolve <ExceptionCounterFilter>()); serial.Handler = scope.Resolve <PacketHandler>(); serial.SessionCreated += (sender, e) => { e.Session.SetAttributeIfAbsent(KeyName.SESSION_ERROR_COUNTER, 0); }; future = serial.Connect(endpoint); future.Await(); } catch (Exception ex) { Log.Error(ErrorCode.SerialPortSessionOpenError, ex); throw new SerialSessionOpenException(endpoint, ex); } } } if (IsOpened()) { return(future.Session); } else { throw new SerialSessionOpenException(endpoint); } }
public SerialShell() { this.config = new SerialPortConfig(); }