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 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 SerialSessionOpenException(SerialEndPoint endpoint, Exception inner) : base(string.Format(ExceptionMessage.FAILED_TO_OPEN_REMOTE_ENDPOINT, endpoint.ToString()), inner) { }
public SerialSessionOpenException(SerialEndPoint endpoint) : this(endpoint, null) { }