示例#1
0
 private bool Open(string name)
 {
     try
     {
         serialPort.PortName = name;
         if (serialPort.IsOpen)
         {
             serialPort.Close();
         }
         serialPort.Open();
         if (IsOpen)
         {
             cBoxSerialPort.IsEnabled = false;
             SerialToggle.IsChecked   = true;
             OnOpened();
             return(true);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("serial port : {0}", (object)ex);
         if (IsOpen)
         {
             serialPort.Close();
             cBoxSerialPort.IsEnabled = true;
             SerialToggle.IsChecked   = new bool?(false);
         }
     }
     return(false);
 }
示例#2
0
 public void SettingsSafeUpdate()
 {
     if (MyPort.IsOpen)
     {
         MyPort.Close();
         SettingsSynchronization();
         MyPort.Open();
     }
 }
示例#3
0
 public void CloseSerial()
 {
     m_ComPort.Close();
 }
示例#4
0
        private static void OnTimerEvent(object sender, ElapsedEventArgs e)
        {
            m_StartTimer = true;
            byte[] by = null;
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    if (m_StopTimer)
                    {
                        return;
                    }
                    foreach (string portName in SearchSerialPort.SerialPortNames)
                    {
                        if (m_StopTimer)
                        {
                            return;
                        }

                        m_Over = false;
                        if (m_SerialPort == null)
                        {
                            m_SerialPort = SerialPortManager.CreateSerialPort();
                            m_SerialPort.DataReceived += SerialPortDataReceived;
                        }

                        m_SerialPort.PortName = portName;
                        try
                        {
                            m_SerialPort.BaudRate = WinApi.B19200;
                            m_SerialPort.Open();

                            if (!SerialPortManager.Device1.SerialPortDevice.IsOpen)
                            {
                                if (m_StopTimer)
                                {
                                    return;
                                }
                                try
                                {
                                    by = PortAgreement.DistanceDeviceEncryption(DefaultParams.DEVICEDEFAULTCLIENTNUMBER, DefaultParams.DEVICEDEFAULTPASSWORD);
                                    m_SerialPort.Write(by);

                                    bool ret = WaitResult(SerialPortManager.Device1);
                                    if (ret)
                                    {
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                                    Console.WriteLine(ex.Message);
#endif
                                }
                            }

                            m_SerialPort.SetBaudRate(WinApi.B9600);
                            Thread.Sleep(50);

                            if (!SerialPortManager.Device2.SerialPortDevice.IsOpen)
                            {
                                int address = 1;
                                try
                                {
                                    for (int number = 1; number <= 50; number++)
                                    {
                                        address = number;
                                        if (m_StopTimer)
                                        {
                                            return;
                                        }
                                        by = PortAgreement.CorridorReadCardData(number);
                                        m_SerialPort.Write(by);
                                        Thread.Sleep(50);
                                        if (m_Over)
                                        {
                                            ReplaceSerialPort(SerialPortManager.Device2);
                                            break;
                                        }
                                    }

                                    if (m_Over)
                                    {
                                        continue;
                                    }
                                    bool ret = WaitResult(SerialPortManager.Device2);
                                    if (ret)
                                    {
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                                    Console.WriteLine(ex.Message);
#endif
                                }
                                finally
                                {
                                    SerialPortManager.Device2Address = address;
                                }
                            }

                            m_SerialPort.Close();
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
            }
            finally
            {
                by = null;
                m_Validation.Dispose();
                if (m_SerialPort != null && m_SerialPort.IsOpen)
                {
                    m_SerialPort.Close();
                }
                m_SerialPort = null;
                if (!m_StopTimer && (!SerialPortManager.Device1.SerialPortDevice.IsOpen && !SerialPortManager.Device2.SerialPortDevice.IsOpen))
                {
                    ViewCallFunction.ViewConnectionFailedMessage();
                }
                m_StartTimer = false;
            }
        }
示例#5
0
 public static void CloseSerialPort(SerialPortEx port)
 {
     port.Close();
 }
示例#6
0
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            if (PlayerModel.afrOne != null)
            {
                PlayerModel.afrOne.Dispose();
                PlayerModel.afrOne = null;
            }
            if (PlayerModel.afrTwo != null)
            {
                PlayerModel.afrTwo.Dispose();
                PlayerModel.afrTwo = null;
            }

            if (PlayerModel.loopOne != null)
            {
                PlayerModel.loopOne.Dispose();
                PlayerModel.loopOne = null;
            }
            if (PlayerModel.loopTwo != null)
            {
                PlayerModel.loopTwo.Dispose();
                PlayerModel.loopTwo = null;
            }

            if (PlayerModel._dsOne != null)
            {
                PlayerModel._dsOne.Dispose();
                PlayerModel._dsOne = null;
            }
            if (PlayerModel._dsTwo != null)
            {
                PlayerModel._dsTwo.Dispose();
                PlayerModel._dsTwo = null;
            }

            if (PlayerModel.wgOne != null)
            {
                PlayerModel.wgOne = null;
            }
            if (PlayerModel.wgTwo != null)
            {
                PlayerModel.wgTwo = null;
            }


            try
            {
                ISerial serialPort = new SerialPortEx(TesterConfig.Instance.GetApplicationCom);
                serialPort.Wirte("09", 1);
                serialPort.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Assert(false, "程序关闭时写入09失败");
                throw;
            }
            if (Cache.Instance.Contexts != null && Cache.Instance.Contexts.Any())
            {
                try
                {
                    //关闭所有的串口
                    Cache.Instance.Contexts.ForEach(c =>
                    {
                        c.Release();
                    });
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
        }