private bool SendCommCMD(string send, string receive) { string str = ""; m_serialPort.Write(send); if (receive.Length > 0) { str = m_serialPort.ReadLine() + m_serialPort.NewLine; } else { Thread.Sleep(30); m_serialPort.ReadExisting(); } return(str.IndexOf(receive) != -1); }
private void Scan() { string packet_data = ""; string serial_data = ""; int length = 0; Encoding encoding = Encoding.GetEncoding("windows-1252"); SerialPortSTB.Encoding = encoding; while (true) { if (CommState == CommunicationState.HWFind) { Thread.Sleep(10); if (IsExtendedFindHW) { ExtendedFindHW(); } else { NormalFindHW(); } } while (CommState == CommunicationState.Ready) { try { if (SerialPortSTB.IsOpen) { if (SerialPortSTB.BytesToRead > 0) { serial_data = SerialPortSTB.ReadExisting(); if (serial_data.Length > 0) { packet_data += serial_data; serial_data = ""; if (packet_data.Length > 0) { do { length = packet_data.Length; EventCallbackCaller(ref packet_data); }while (length != packet_data.Length && packet_data.Length != 0); } } } } else { lock (objectLock) CommState = CommunicationState.Idle; } } catch (UnauthorizedAccessException ex) { STBLogger.AddEvent(this, STBLogger.EventLevel.Error, "UnauthorizedAccessException while reading port.", ex.Message + ex.Source + ex.StackTrace); lock (objectLock) CommState = CommunicationState.Idle; CommCallbackCaller(CommEvent.CommLost); } catch (InvalidOperationException exception2) { STBLogger.AddEvent(this, STBLogger.EventLevel.Error, "The port was closed", exception2.Message + exception2.Source + exception2.StackTrace); lock (objectLock) CommState = CommunicationState.Idle; CommCallbackCaller(CommEvent.CommLost); } Thread.Sleep(1); } if (CommState == CommunicationState.Bootloader) { Thread.Sleep(5); } if (CommState == CommunicationState.Idle || CommState == CommunicationState.Invalid) { try { if (SerialPortSTB.IsOpen) { SerialPortSTB.Close(); } } catch (UnauthorizedAccessException ex) { STBLogger.AddEvent(this, STBLogger.EventLevel.Error, "UnauthorizedAccessException while closing port.", ex.Message + ex.Source + ex.StackTrace); } catch (Exception ex) { STBLogger.AddEvent(this, STBLogger.EventLevel.Error, ToString() + " caused exception " + ex.Message, ex.Message + ex.Source + ex.StackTrace); } if (AbortThread.WaitOne(500, false)) { return; } } } }