[英]How can I filter out random new lines in a RichTextBox?
我正在使用非標准串行庫來讀取高速 CAN 總線數據。 在低速時我沒有觀察到這個問題,但在全速(大約每秒 125 條消息)時, RichTextBox
輸出隨機的新行。 它很少發生,可能每 2-3k 行一次。
不需要的新行:
每條消息的消息 ID 增加 1,因此我可以看到沒有發生消息丟失。
串行receiveHandler()
方法的代碼:
private void receiveHandler(object sender, DataStreamEventArgs e)
{
string line = System.Text.Encoding.Default.GetString(e.Response);
//Console.WriteLine("1) The length of '{0}' is {1}", line, line.Length);
// line.Split attempts to split the string before it is completely written
//string[] stringValues = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
//int a = Int32.Parse(stringValues[0]);
//Console.WriteLine("1) The messge count '{0}' is {1}", line, a);
if(line.Length > 63 && !String.IsNullOrEmpty(line))
{
richCANBox.Invoke(new MethodInvoker(delegate { richCANBox.AppendText(line); }));
}
// Clear messages in buffer after clear button pressed.
// TODO: Try waiting for thread to finish before clearing using join
if (stopMessage)
{
richCANBox.Invoke(new MethodInvoker(delegate { richCANBox.Clear(); }));
}
}
每個消息長度是 64,所以我嘗試過濾掉比它短的消息並使用String.IsNullOrEmpty()
但我仍然得到隨機行。 我還將處理調用之前可用的字節數從 0 增加到 30,以防止在所有字節到達之前訪問數據,但這並沒有幫助。
if (readBytes > 30)
{
OnSerialReceiving(buf);
}
我對 C# 的經驗很少,因此選擇RichTextBox
因為它很容易保存數據捕獲。 關於我如何過濾掉新行或我可以在哪里尋找這個錯誤的任何建議?
完整存儲庫: https : //github.com/BrandonVP/HyperCAN
編輯:傳輸的設備是 ESP32,從 Arduino IDE 終端或 VS Micro 終端查看它的輸出時,我沒有得到任何隨機返回線。 我正在使用 AX-Fast 串行庫,因為標准庫無法足夠快地傳輸數據。 另外,我從評論中嘗試了string.IsNullOrWhitespace()
,但沒有幫助。
試試這個:
richCANBox.Text = richCANBox.Text.Replace(" ","");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.