Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            ConsoleEx.WriteAppCaption();
            ConsoleEx.WriteLine($"Valid port(s): {Helpers.GetValidPorts()}");

            var cmdLine = new MyCommandLine(args);

            if (cmdLine.ExistsAny(new[] { PAR_HELP_1, PAR_HELP_2, PAR_HELP_3 }))
            {
                ShowHelp();
                return;
            }

            try
            {
                var comCom = new ComComunication(cmdLine.Value(PAR_PORT_LIST));

                Console.WriteLine($"Selected port: {comCom.PortName}");

                var comComThread = comCom.StartCommunication();
                var pipeName     = cmdLine.Exists(PAR_PIPE_NAME) ? cmdLine.Value(PAR_PIPE_NAME) : Helpers.GetPipeName(comCom.PortName);

                var pipeServer       = new PipeServer(pipeName, comCom.SerialPort);
                var pipeServerThread = pipeServer.Start();

                comComThread?.Join();
            }
            catch (Exception ex)
            {
                ConsoleEx.WriteError(ex);
            }
        }
Exemplo n.º 2
0
        /// <param name="args"></param>
        private static void Main(string[] args)
        {
            ConsoleEx.WriteAppCaption();

            //--- Analyzujeme příkazovou řádku
            var cmdLine = new MyCommandLine(args);

            //je help?
            if (cmdLine.ExistsAny(new[] { PAR_HELP_1, PAR_HELP_2, PAR_HELP_3 }))
            {
                ShowHelp();
                return;
            }

            var pipeName = cmdLine.Value(PAR_PIPE_NAME);

            if (string.IsNullOrEmpty(pipeName))
            {
                ConsoleEx.WriteLine("Missing name of pipe.");
                ShowHelp();
                return;
            }

            MessageFromClient message = null;

            try
            {
                message = GetMessage(cmdLine);
            }
            catch (Exception ex)
            {
                ConsoleEx.WriteError(ex);
                ShowHelp();
                return;
            }

            if (message == null)
            {
                ConsoleEx.WriteLine("Missing command.");
                ShowHelp();
                return;
            }

            try
            {
                using (var pipeClient = new NamedPipeClientStream(
                           "."
                           , pipeName
                           , PipeDirection.InOut
                           , PipeOptions.None
                           , TokenImpersonationLevel.Impersonation))
                {
                    pipeClient.Connect(500);

                    ConsoleEx.WriteLine($"Connect to pipe '{pipeName}' successed.");

                    var streamString = new StreamString(pipeClient);
                    streamString.WriteString(JsonConvert.SerializeObject(message));

                    do
                    {
                        ConsoleEx.WriteLine(ConsoleColor.White, streamString.ReadString());
                    } while (pipeClient.IsConnected);

                    pipeClient.Close();

                    return;
                }
            }
            catch (Exception ex)
            {
                ConsoleEx.WriteError(ex);
                ConsoleEx.WriteError($"Connect to {pipeName} failed.");
            }
            //---

            Debug.WriteLine($"Connect to {pipeName} failed.");
        }