예제 #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
        }
예제 #2
0
 public string ReadLine()
 {
     try
     {
         return(serialPort.ReadLine());
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return("");
     }
 }
예제 #3
0
 public string[] GetAvailablePorts()
 {
     try
     {
         return(System.IO.Ports.SerialPort.GetPortNames());
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(null);
     }
 }
예제 #4
0
 public void SysControl()
 {
     Task control = Task.Run(() =>
     {
         while (IsRun && CheckConnect())
         {
             Thread.Sleep(500);
         }
         PortException.AddException("No connection!!!");
         Error();//Check!!@@
     });
 }
예제 #5
0
 public bool SendLine(string message)
 {
     try
     {
         serialPort.WriteLine(message);
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }
예제 #6
0
 public bool Open()
 {
     try
     {
         serialPort.Open();
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }
예제 #7
0
 public bool Close()
 {
     if (!serialPort.IsOpen)
     {
         return(true);
     }
     try
     {
         serialPort.Close();
         return(true);
     }
     catch (Exception e)
     {
         PortException.AddException(e.Message);
         return(false);
     }
 }