Esempio 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);
            }
        }
Esempio n. 2
0
        private static MessageFromClient GetCmdByLineMessage(MyCommandLine cmdLine)
        {
            if (!cmdLine.ExistsAll(new [] { PAR_CMD_DOFILE, PAR_CMD_LINE }))
            {
                return(null);
            }

            var fileName = cmdLine.Value(PAR_CMD_DOFILE);

            if (string.IsNullOrEmpty(fileName))
            {
                throw new Exception("File not specified.");
            }

            var lineIndex = cmdLine.Value(PAR_CMD_LINE, 0) - 1; //řádky jsou v VS Code číslovány od jedničky, převádím je tedy na index

            if (lineIndex < 0)
            {
                throw new Exception("Line not specified.");
            }

            //--- vytáhneme příslušný řádek (očekáváme, že řádky jsou číslovány od jedničky)
            var lines = System.IO.File.ReadAllLines(fileName);

            if (lines.Length < lineIndex)
            {
                throw new Exception($"Line {lineIndex+1} in file {fileName} not exists.");
            }

            var command = lines[lineIndex];

            //---

            return(new MessageFromClient
            {
                Command = "CMD",
                Parameters = new List <string> {
                    command
                }
            });
        }
Esempio n. 3
0
        private static MessageFromClient GetCmdMessage(MyCommandLine cmdLine)
        {
            if (!cmdLine.Exists(PAR_CMD_COMMAND))
            {
                return(null);
            }

            var command = cmdLine.Value(PAR_CMD_COMMAND);

            if (string.IsNullOrEmpty(command))
            {
                throw new Exception("Command not specified.");
            }

            return(new MessageFromClient
            {
                Command = "CMD",
                Parameters = new List <string> {
                    command
                }
            });
        }
Esempio n. 4
0
        private static MessageFromClient GetUploadMessage(MyCommandLine cmdLine)
        {
            if (!cmdLine.Exists(PAR_CMD_UPLOAD))
            {
                return(null);
            }

            var fileName = cmdLine.Value(PAR_CMD_UPLOAD);

            if (string.IsNullOrEmpty(fileName))
            {
                throw new Exception("File not specified.");
            }

            return(new MessageFromClient
            {
                Command = "UPLOAD",
                Parameters = new List <string> {
                    fileName
                }
            });
        }
Esempio n. 5
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.");
        }