Turn off a USB port and turn it on by C #

0

When I get an error in the connection, and the program restarts it does not connect automatically I have to disconnect and connect the USB port to connect. Do I want to turn off that port and turn it on by C # is it possible?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.ComponentModel;
using System.IO.Ports;
using System.Threading;
using System.Data.SqlClient;
using System.Data;


namespace Processes_Remote_computer
{
    class ProcessMonitorSample
    {    
        public static void Main()
        {
            int data2 = 1;

            while (data2 == 1) {

                Console.Clear();

                try

            {                    

                //conexion puerto serial
                SerialPort spPuertoSerie = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

                    spPuertoSerie.Open();
                int data1 = 1;

                    // -------------------------------------------------------------------------
                    while (data1 == 1)
                {

                    // conexion de la PC al HMI
                    Process[] targetProcess = Process.GetProcessesByName("VUServer");
                    int proLen = targetProcess.Length;

                    // lee el puesto serial 
                    string data4 = spPuertoSerie.ReadExisting();
                    string data3 = "1";

                    //area de comparar data de lectura Serial

                    if (data4 == data3)
                    {

                        Console.WriteLine("bit de envio de data " + data4);
                        Thread.Sleep(1000);   
                        Console.Clear();
                    }

                    //area de  verifica que la PC esta conectada al HMI
                    if (proLen != 0)
                    {
                        Console.WriteLine("The process status is: Running");
                        spPuertoSerie.Write("1");
                        Thread.Sleep(5000);
                        Console.Clear();                                
                        }

                    if (proLen == 0)
                    {                           

                        Console.WriteLine("The process does NOT exist or has exited...");
                        spPuertoSerie.Write(" ");
                        Thread.Sleep(5000);
                        Console.Clear();    

                        }
                }

                }

            catch (Exception ex)
            {
                    Console.WriteLine("Exception Message: " + ex.Message);                       
                    Thread.Sleep(5000);
                }
            //area de salida si hay un error
            Console.Clear();
            Console.WriteLine(" !Espere! Reinciando el Progrma ");

                Thread.Sleep(5000);

            }                
        }           
    }    
}
    
asked by Geraldo Montas 14.09.2018 в 21:54
source

0 answers