[問題] 關於Serial Port 接收資料處理的問題

看板C_Sharp作者 (dada)時間8年前 (2015/12/11 22:41), 8年前編輯推噓4(406)
留言10則, 5人參與, 最新討論串1/1
大家好, 請問一下,下面這個程式碼,想使用在接收資料後進行處理 發生了str2其實為空的狀態,不知道什麼原因 while(true) { while(comport.BytesToRead > 0) { str = Convert.ToChar(comport.ReadChar()).ToString(); str2 += str; richTextBox1.Text += str; } if (str2 == "test") richTextBox6.Text += str2; } 跪求 各位稍微指點一下 一個人好難玩 orz -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.127.222.1 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1449844878.A.6F4.html ※ 編輯: wasidada (59.127.222.1), 12/11/2015 22:55:18

12/12 19:13, , 1F
用中斷看comport.ReadChar()有東西嗎?
12/12 19:13, 1F
有東西

12/12 19:36, , 2F
用debug模式用滑鼠去看str有沒有值
12/12 19:36, 2F

12/12 19:36, , 3F
就是把中斷點設在那
12/12 19:36, 3F

12/12 21:27, , 4F
有清空緩衝區嗎
12/12 21:27, 4F
我之前沒設定,剛剛設定了改善很多了 ...至少現在不會延遲 @@

12/12 21:29, , 5F
開發環境是在win7 x64嗎
12/12 21:29, 5F
WIN 10 X64 ※ 編輯: wasidada (59.127.222.1), 12/12/2015 22:58:52 ※ 編輯: wasidada (59.127.222.1), 12/12/2015 22:59:34

12/13 23:39, , 6F
STR2沒初始化啊 而且一班這種不斷加長字串長度的狀況
12/13 23:39, 6F

12/13 23:39, , 7F
會用stringbuilder 型別 不會直接用string
12/13 23:39, 7F

12/13 23:41, , 8F
而且 看你流程 沒有str2 好像沒差 為何這麼做?
12/13 23:41, 8F

12/14 05:51, , 9F
把收到的字元,累加上去變成一個字串
12/14 05:51, 9F

12/14 08:07, , 10F
為何不等buffer區 接收足夠數量後 再一次接收呢
12/14 08:07, 10F
文章代碼(AID): #1MQk2ERq (C_Sharp)