Exemple #1
0
        public bool InitPort(string name, int speed)
        {
            serialPort = new System.IO.Ports.SerialPort();
            string[] ports    = System.IO.Ports.SerialPort.GetPortNames();
            string   portName = "";

            foreach (var port in ports)
            {
                if (port == name)
                {
                    portName = port;
                }
            }
            #region Create Port object,and open serial port
            try
            {
                serialPort = new System.IO.Ports.SerialPort(portName, speed);
                if (serialPort.IsOpen)
                {
                    throw new SystemException("Выбранный порт уже используется.Ошибка!");
                }
                AppDomain.CurrentDomain.ProcessExit += SerialProcessExit;
                return(true);
            }
            catch (Exception e)
            {
                PortException.AddException(e.Message);
                return(false);
            }
            #endregion
        }
Exemple #2
0
 public string ReadLine()
 {
     try
     {
         return(serialPort.ReadLine());
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return("");
     }
 }
Exemple #3
0
 public string[] GetAvailablePorts()
 {
     try
     {
         return(System.IO.Ports.SerialPort.GetPortNames());
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(null);
     }
 }
Exemple #4
0
 public void SysControl()
 {
     Task control = Task.Run(() =>
     {
         while (IsRun && CheckConnect())
         {
             Thread.Sleep(500);
         }
         PortException.AddException("No connection!!!");
         Error();//Check!!@@
     });
 }
Exemple #5
0
 public bool SendLine(string message)
 {
     try
     {
         serialPort.WriteLine(message);
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }
Exemple #6
0
 public bool Open()
 {
     try
     {
         serialPort.Open();
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }
Exemple #7
0
 public bool Close()
 {
     if (!serialPort.IsOpen)
     {
         return(true);
     }
     try
     {
         serialPort.Close();
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }