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 SerialSession(SerialConnector service, SerialEndPoint endpoint, SerialPort serialPort) : base(service) { _processor = service; base.Config = new SessionConfigImpl(serialPort); if (service.SessionConfig != null) Config.SetAll(service.SessionConfig); _filterChain = new DefaultIoFilterChain(this); _serialPort = serialPort; _endpoint = endpoint; _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived); }
public SerialSession(SerialConnector service, SerialEndPoint endpoint, SerialPort serialPort) : base(service) { _processor = service; base.Config = new SessionConfigImpl(serialPort); if (service.SessionConfig != null) { Config.SetAll(service.SessionConfig); } _filterChain = new DefaultIoFilterChain(this); _serialPort = serialPort; _endpoint = endpoint; _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived); }
/// <inheritdoc/> protected override IConnectFuture Connect0(EndPoint remoteEP, EndPoint localEP, Action <IoSession, IConnectFuture> sessionInitializer) { ISerialSessionConfig config = (ISerialSessionConfig)SessionConfig; SerialEndPoint sep = (SerialEndPoint)remoteEP; SerialPort serialPort = new SerialPort(sep.PortName, sep.BaudRate, sep.Parity, sep.DataBits, sep.StopBits); if (config.ReadBufferSize > 0) { serialPort.ReadBufferSize = config.ReadBufferSize; } if (config.ReadTimeout > 0) { serialPort.ReadTimeout = config.ReadTimeout * 1000; } if (config.WriteBufferSize > 0) { serialPort.WriteBufferSize = config.WriteBufferSize; } if (config.WriteTimeout > 0) { serialPort.WriteTimeout = config.WriteTimeout * 1000; } if (config.ReceivedBytesThreshold > 0) { serialPort.ReceivedBytesThreshold = config.ReceivedBytesThreshold; } IConnectFuture future = new DefaultConnectFuture(); SerialSession session = new SerialSession(this, sep, serialPort); InitSession(session, future, sessionInitializer); try { session.Processor.Add(session); } catch (IOException ex) { return(DefaultConnectFuture.NewFailedFuture(ex)); } _idleStatusChecker.Start(); return(future); }
static void Main(string[] args) { IoConnector serial = new SerialConnector(); // Add two filters : a logger and a codec serial.FilterChain.AddLast("logger", new LoggingFilter()); serial.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8))); serial.ExceptionCaught += (s, e) => e.Session.Close(true); serial.MessageReceived += (s, e) => { Console.WriteLine(e.Message); }; SerialEndPoint serialEP = new SerialEndPoint("COM3", 38400); IConnectFuture future = serial.Connect(serialEP); future.Await(); if (future.Connected) { while (true) { String line = Console.ReadLine(); if (line.Trim().Equals("quit", StringComparison.OrdinalIgnoreCase)) { future.Session.Close(true); break; } future.Session.Write(line); } } else if (future.Exception != null) { Console.WriteLine(future.Exception); } Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }
public SerialSessionOpenException(SerialEndPoint endpoint, Exception inner) : base(string.Format(ExceptionMessage.FAILED_TO_OPEN_REMOTE_ENDPOINT, endpoint.ToString()), inner) { }
public SerialSessionOpenException(SerialEndPoint endpoint) : this(endpoint, null) { }