///<summary>停止</summary> ///<param name="isRefresh">是否刷新</param> private void Stop(bool isRefresh) { lock (this) { if (!m_IsStopped) { m_IsStopped = true; try { if (m_IsSimulator) { if (m_SerialPortSimulator != null) { m_SerialPortSimulator.Dispose(); m_SerialPortSimulator = null; } } else { m_SerialPort.Close(); } } catch { } if (isRefresh) { try { m_Handler.Clear(); } catch { } ChangeState(false); } } } }
///<summary>开始</summary> public void Start() { lock (this) { if (m_IsStopped) { if (m_Handler != null) { if (m_IsSimulator) { if (m_SimulatorDataGenerator != null) { m_SerialPortSimulator = new SerialPortSimulator(m_SimulatorDataGenerator, m_Handler); m_IsStopped = false; TriggerErrorEvent(ErrorType.NoError); ChangeState(true); return; } } else { if (PortID < 0) { ChangeState(false); return; } string name; name = GetPortName(PortID); if (name != null) { try { m_SerialPort.PortName = PortNames[PortID]; m_SerialPort.Open(); m_ReadThread = new Thread(Read); m_ReadThread.IsBackground = true; m_ReadThread.Start(); m_IsStopped = false; TriggerErrorEvent(ErrorType.NoError); ChangeState(true); return; } catch { } } } } TriggerErrorEvent(ErrorType.StartFailed); ChangeState(false); } } }