private static void SerialPortDataReceived(int port) { Thread.Sleep(10); byte[] by; try { int len = m_SerialPort.GetIqueue; if (len <= 0) { by = m_Validation.Combination(m_Bytes.ToArray()); if (by != null) { m_Over = true; m_Bytes.Clear(); } return; } by = m_SerialPort.Read(len); m_Bytes.AddRange(by); } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); } }
void HandleMyPortDataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] buffer = new byte[MyPort.BytesToRead]; MyPort.Read(buffer, 0, buffer.Length); ReceiveStream.Write(buffer, 0, buffer.Length); // textviewTextString += StringConverts.BytesToString (buffer); // textviewHexString += StringConverts.BytesToHexString (buffer); // textviewDecString += StringConverts.BytesToDecString (buffer); moverText.Append(StringConverts.BytesToString(buffer)); moverHex.Append(StringConverts.BytesToHexString(buffer)); moverDec.Append(StringConverts.BytesToDecString(buffer)); Gdk.Threads.Enter(); // 准备在线程中更新界面 labelRxStatus.Text = ReceiveStream.Length.ToString(); processScrollEvent(ConvertMode.Text, false); processScrollEvent(ConvertMode.Hex, false); processScrollEvent(ConvertMode.Dec, false); // TextIter iter; // iter = textviewText.Buffer.EndIter; // textviewText.Buffer.Insert (ref iter, StringConverts.BytesToString (buffer)); // if (textviewText.Buffer.CharCount > textViewDataBytesMaxCount) { // TextIter iterStart = textviewText.Buffer.StartIter; // TextIter iterEnd = textviewText.Buffer.StartIter; // if (iterEnd.ForwardChars (textviewText.Buffer.CharCount - textViewDataBytesMaxCount)) { // textviewText.Buffer.Delete (ref iterStart, ref iterEnd); // } // } // if (checkbuttonAutoScrollReceive.Active) { // iter = textviewText.Buffer.EndIter; // textviewText.Buffer.CreateMark ("EndMark", iter, false); // textviewText.ScrollToMark (textviewText.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0); // textviewText.Buffer.DeleteMark ("EndMark"); // } // iter = textviewHex.Buffer.EndIter; // textviewHex.Buffer.Insert (ref iter, StringConverts.BytesToHexString (buffer)); // if (checkbuttonAutoScrollReceive.Active) { // textviewHex.Buffer.CreateMark ("EndMark", iter, false); // textviewHex.ScrollToMark (textviewHex.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0); // textviewHex.Buffer.DeleteMark ("EndMark"); // } // iter = textviewDec.Buffer.EndIter; // textviewDec.Buffer.Insert (ref iter, StringConverts.BytesToDecString (buffer)); // if (checkbuttonAutoScrollReceive.Active) { // textviewDec.Buffer.CreateMark ("EndMark", iter, false); // textviewDec.ScrollToMark (textviewDec.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0); // textviewDec.Buffer.DeleteMark ("EndMark"); // } Gdk.Threads.Leave(); }