Beispiel #1
0
        /// <summary>
        /// Triggers a registry scan to determine the currently installed LabVIEW versions.
        /// This method is automatically invoked by the static constructor.
        /// </summary>
        public static void Scan()
        {
            // Initialize Versions List
            Versions       = new List <lvVersion>();
            CurrentVersion = null;

            // Scan 32bit Registry
            _ScanRegistry(RegistryView.Registry32);

            // Scan 64bit Registry on x64 Systems
            if (Environment.Is64BitOperatingSystem)
            {
                _ScanRegistry(RegistryView.Registry64);
            }

            // Determine Current Version (default if no exe or version is supplied as argument)
            _GetCurrentVersion();

            Output output = Output.Instance;

            if (CurrentVersion == null)
            {
                output.writeInfo("No installed LabVIEW version discovered!");
            }
            else
            {
                output.writeInfo("Current Version: " + CurrentVersion.ToString());

                output.writeInfo("Detected LabVIEW versions:");
                foreach (var current in Versions)
                {
                    output.writeInfo(current.ToString() + "(" + current.Path + ")");
                }
            }
        }
Beispiel #2
0
        public LvLauncher(string launchPath, string lvPath, int port, string[] args)
        {
            procInfo = new ProcessStartInfo();

            //Prepare the arguments with quotes.
            string preparedArgs = "";

            foreach (string arg in args)
            {
                preparedArgs += "\"" + arg + "\" ";
            }
            output.writeInfo("LabVIEW User Arguments: " + preparedArgs);

            string arguments = "-- -p:" + port + " " + preparedArgs;

            if (isExe(launchPath))
            {
                procInfo.FileName  = launchPath;
                procInfo.Arguments = arguments;
            }
            else
            {
                //gate on a blank LV path which means we don't have LabVIEW Installed.
                if (lvPath == "")
                {
                    throw new System.IO.FileNotFoundException("No LabVIEW.exe found...", "LabVIEW.exe");
                }
                procInfo.FileName  = lvPath;
                procInfo.Arguments = "\"" + launchPath + "\" " + arguments;
            }

            lvProcess           = new Process();
            lvProcess.StartInfo = procInfo;
            lvProcess.Exited   += Process_Exited;
        }
Beispiel #3
0
 public void Start()
 {
     output.writeInfo("Launching Program:");
     output.writeInfo(procInfo.FileName + " " + procInfo.Arguments);
     LvTrackingThread = new Thread(new ThreadStart(_processTrackingThread))
     {
         Name = "LvTrackingThread"
     };
     LvTrackingThread.Start();
     lvStarted.Wait();
 }
Beispiel #4
0
        /// <summary>
        /// waits <paramref name="millisecondsTimeout"/> for a connection from the LabVIEW programm.
        /// </summary>
        /// <param name="millisecondsTimeout">The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.</param>
        /// <returns>true if a connection was established, false if timed out</returns>
        public bool waitOnConnection(int millisecondsTimeout = -1)
        {
            //Blocking call to wait for TCP Client
            output.writeInfo("Waiting for connection on port " + _port);
            Task <TcpClient> clientTask = _listener.AcceptTcpClientAsync();

            if (!clientTask.Wait(millisecondsTimeout))
            {
                return(false);
            }
            _client          = clientTask.Result;
            _clientConnected = true;
            _stream          = _client.GetStream();
            output.writeInfo("Client Connected");
            return(true);
        }
        public LvLauncher(string launchPath, string lvPath, int port, string[] args)
        {
            procInfo = new ProcessStartInfo();

            //Prepare the arguments with quotes.
            string preparedArgs = "";

            foreach (string arg in args)
            {
                preparedArgs += "\"" + arg + "\" ";
            }
            output.writeInfo("LabVIEW User Arguments: " + preparedArgs);

            string arguments = "-- -p:" + port + " " + preparedArgs;

            if (isExe(launchPath))
            {
                procInfo.FileName  = launchPath;
                procInfo.Arguments = arguments;
            }
            else
            {
                procInfo.FileName  = lvPath;
                procInfo.Arguments = "\"" + launchPath + "\" " + arguments;
            }

            process           = new Process();
            process.StartInfo = procInfo;
            process.Exited   += Process_Exited;
        }
Beispiel #6
0
        static int Main(string[] args)
        {
            int    exitCode = 0;
            Output output   = Output.Instance;

            string[]   cliArgs, lvArgs;
            lvComms    lvInterface   = new lvComms();
            lvMsg      latestMessage = new lvMsg("NOOP", "");
            LvLauncher launcher      = null;
            CliOptions options       = new CliOptions();

            lvVersion current = LvVersions.CurrentVersion;

            splitArguments(args, out cliArgs, out lvArgs);
            //CommandLine.Parser.Default.ParseArguments(cliArgs, options);
            if (!CommandLine.Parser.Default.ParseArguments(cliArgs, options))
            {
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }
            if (options.Version)
            {
                output.writeMessage(Assembly.GetExecutingAssembly().GetName().Version.ToString());
                Environment.Exit(0);
            }

            output.setVerbose(options.Verbose);
            output.writeInfo("LabVIEW CLI Started - Verbose Mode");
            output.writeInfo("Version " + Assembly.GetExecutingAssembly().GetName().Version);
            output.writeInfo("LabVIEW CLI Arguments: " + String.Join(" ", cliArgs));
            output.writeInfo("Arguments passed to LabVIEW: " + String.Join(" ", lvArgs));

            if (options.noLaunch)
            {
                output.writeMessage("Auto Launch Disabled");
                // disable timeout if noLaunch is specified
                options.timeout = -1;
            }
            else
            {
                // check launch vi
                if (options.LaunchVI == null)
                {
                    output.writeError("No launch VI supplied!");
                    return(1);
                }
                if (!File.Exists(options.LaunchVI))
                {
                    output.writeError("File \"" + options.LaunchVI + "\" does not exist!");
                    return(1);
                }

                List <string> permittedExtensions = new List <string> {
                    ".vi", ".lvproj"
                };
                string ext = Path.GetExtension(options.LaunchVI).ToLower();
                if (!permittedExtensions.Contains(ext))
                {
                    output.writeError("Cannot handle *" + ext + " files");
                    return(1);
                }

                try
                {
                    launcher         = new LvLauncher(options.LaunchVI, lvPathFinder(options), lvInterface.port, lvArgs);
                    launcher.Exited += Launcher_Exited;
                    launcher.Start();
                }
                catch (KeyNotFoundException ex)
                {
                    // Fail gracefully if lv-ver option cannot be resolved
                    string bitness = options.x64 ? " 64bit" : string.Empty;
                    output.writeError("LabVIEW version \"" + options.lvVer + bitness + "\" not found!");
                    output.writeMessage("Available LabVIEW versions are:");
                    foreach (var ver in LvVersions.Versions)
                    {
                        output.writeMessage(ver.ToString());
                    }
                    return(1);
                }
                catch (FileNotFoundException ex)
                {
                    output.writeError(ex.Message);
                    return(1);
                }
            }

            // wait for the LabVIEW application to connect to the cli
            connected = lvInterface.waitOnConnection(options.timeout);

            // if timed out, kill LabVIEW and exit with error code
            if (!connected && launcher != null)
            {
                output.writeError("Connection to LabVIEW timed out!");
                launcher.Kill();
                launcher.Exited -= Launcher_Exited;
                return(1);
            }

            do
            {
                latestMessage = lvInterface.readMessage();

                switch (latestMessage.messageType)
                {
                case "OUTP":
                    Console.Write(latestMessage.messageData);
                    break;

                case "EXIT":
                    exitCode = lvInterface.extractExitCode(latestMessage.messageData);
                    output.writeMessage("Recieved Exit Code " + exitCode);
                    stop = true;
                    break;

                case "RDER":
                    exitCode = 1;
                    output.writeError("Read Error");
                    stop = true;
                    break;

                default:
                    output.writeError("Unknown Message Type Recieved:" + latestMessage.messageType);
                    break;
                }
            } while (!stop);

            // close tcp listener
            lvInterface.Close();

            // if killswitch is set, force LabVIEW to exit if it has not closed by itself after a 10s timeout (or the period specified in --timeout)
            if (options.kill)
            {
                int timeout = options.timeout == -1 ? 10000 : options.timeout;
                if (!launcher.lvExited.Wait(timeout))
                {
                    output.writeMessage("Forcing LabVIEW to terminate...");
                    launcher.Kill();
                }
            }

            return(exitCode);
        }
Beispiel #7
0
        static int Main(string[] args)
        {
            int    exitCode = 0;
            Output output   = Output.Instance;

            string[]         cliArgs, lvArgs;
            lvComms          lvInterface      = new lvComms();
            lvMsg            latestMessage    = new lvMsg("NOOP", "");
            LvLauncher       launcher         = null;
            CliOptions       options          = new CliOptions();
            lvVersion        current          = LvVersions.CurrentVersion;
            portRegistration portRegistration = new portRegistration();

            splitArguments(args, out cliArgs, out lvArgs);
            if (!CommandLine.Parser.Default.ParseArguments(cliArgs, options))
            {
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }
            if (options.Version)
            {
                output.writeMessage(getVersionString());
                Environment.Exit(0);
            }

            output.setVerbose(options.Verbose);
            output.writeInfo("LabVIEW CLI Started - Verbose Mode");
            output.writeInfo("Version " + getVersionString());
            output.writeInfo("LabVIEW CLI Arguments: " + String.Join(" ", cliArgs));
            output.writeInfo("Arguments passed to LabVIEW: " + String.Join(" ", lvArgs));

            //Warn about deprecated option.
            if (options.lvExe != null)
            {
                output.writeError("--lv-exe option is deprecated. Alter the script to use --lv-ver. Default will be used this time.");
            }

            //Force a rescan at this point to get an output - the constructor scan does not seem to write to output, possibly due to happening before we set verbose flag.
            LvVersions.Scan();

            if (options.noLaunch)
            {
                output.writeMessage("Auto Launch Disabled");
                // disable timeout if noLaunch is specified
                options.timeout = -1;
            }
            else
            {
                // check launch vi
                if (options.LaunchVI == null)
                {
                    output.writeError("No launch VI supplied!");
                    return(1);
                }
                if (!File.Exists(options.LaunchVI))
                {
                    output.writeError("File \"" + options.LaunchVI + "\" does not exist!");
                    return(1);
                }

                List <string> permittedExtensions = new List <string> {
                    ".vi", ".lvproj", ".exe"
                };
                string ext = Path.GetExtension(options.LaunchVI).ToLower();
                if (!permittedExtensions.Contains(ext))
                {
                    output.writeError("Cannot handle *" + ext + " files");
                    return(1);
                }

                try
                {
                    launcher         = new LvLauncher(options.LaunchVI, lvPathFinder(options), lvInterface.port, portRegistration);
                    launcher.Exited += Launcher_Exited;
                    launcher.Start();
                }
                catch (KeyNotFoundException ex)
                {
                    // Fail gracefully if lv-ver option cannot be resolved
                    string bitness = options.x64 ? " 64bit" : string.Empty;
                    output.writeError("LabVIEW version \"" + options.lvVer + bitness + "\" not found!");
                    output.writeMessage("Available LabVIEW versions are:");
                    foreach (var ver in LvVersions.Versions)
                    {
                        output.writeMessage(ver.ToString());
                    }
                    return(1);
                }
                catch (FileNotFoundException ex)
                {
                    output.writeError(ex.Message);
                    return(1);
                }
            }

            //At this point LV should have launched so now we need to handle Ctrl+C to ensure LV is killed as well.
            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
            {
                output.writeMessage("Cancel key received, closing LabVIEW.");
                launcher.Kill();
            };

            // wait for the LabVIEW application to connect to the cli
            connected = lvInterface.waitOnConnection(options.timeout);
            portRegistration.unRegister();
            output.writeMessage("Client Connected");

            // if timed out, kill LabVIEW and exit with error code
            if (!connected && launcher != null)
            {
                output.writeError("Connection to LabVIEW timed out!");
                launcher.Kill();
                launcher.Exited -= Launcher_Exited;
                return(1);
            }

            //Write the use arguments
            lvInterface.writeArguments(lvArgs);

            while (!stop)
            {
                //strange call because it is async method.
                latestMessage = lvInterface.readMessage().GetAwaiter().GetResult();

                switch (latestMessage.messageType)
                {
                case "OUTP":
                    Console.Write(latestMessage.messageData);
                    break;

                case "EXIT":
                    exitCode = lvInterface.extractExitCode(latestMessage.messageData);
                    output.writeMessage("Received Exit Code " + exitCode);
                    stop = true;
                    break;

                case "RDER":
                    //exitCode = 1;
                    output.writeError("Read Error");
                    if (latestMessage.messageData != "")
                    {
                        output.writeError(": " + latestMessage.messageData);
                    }
                    output.writeError("Since the network stream will be out of sync the application will now exit.");
                    stop = true;
                    break;

                default:
                    output.writeError("Unknown Message Type Received:" + latestMessage.messageType);
                    break;
                }
            }
            ;

            // close tcp listener
            lvInterface.Close();

            // if killswitch is set, force LabVIEW to exit if it has not closed by itself after a 10s timeout (or the period specified in --timeout)
            if (options.kill)
            {
                int timeout = options.timeout == -1 ? 10000 : options.timeout;
                if (!launcher.lvExited.Wait(timeout))
                {
                    output.writeMessage("Forcing LabVIEW to terminate...");
                    launcher.Kill();
                }
            }

            launcher.Close();

            return(exitCode);
        }