###前提・実現したいこと
WindowsFormアプリを作成しています。
発信機から送信された文字列を、PCにCOMポートで接続されている受信機で
受け取り、受け取った文字列を処理したいと思っています。
###発生している問題・エラーメッセージ
SerialPort.ReadLineを使うと、文字列の送受信後に切断できなくなり、
SerialPort.ReadExistingを使うと、即時処理ができなくなってしまいます。
○ReadLine使用時は通信後、切断しようとすると以下のメッセージが出ます。
スレッドの終了またはアプリケーションの要求によって、I/O処理は中止されました。
○ReadExistingはmsdnの公式ページに「即座に使用できるすべてのバイトを読み取ります。」と説明があり、
実際の動作と受信したデータにズレが生じるようです。
例としては、
「値3」のデータが送信されたが、その段階では受信できず、
次の「値5」を送信したときに「値3」と「値5」の両方が
一緒に受信できる、といった様子です。
「値3」が送信されたらその段階で受信したいと考えています。
###聞きたいこと
通信後切断できるReadExistingを使用したいのですが、
即時処理できないのは困ります。
ReadExistingで即時処理する方法はありますでしょうか?
それともReadlineを使用し、エラーを発生させなくした方が良いでしょうか?
もしくは別のRead系関数を使った方が良いでしょうか?
###該当のソースコード
・ReadLine
C#
1 /// <summary> 2 /// データ受信が発生した時のイベント 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void serialPortMain_DataReceived(object sender, SerialDataReceivedEventArgs e) 7 { 8 if (!serialPortMain.IsOpen) 9 { 10 // 接続していない場合は処理を行わない 11 return; 12 } 13 else 14 { 15 string data = string.Empty; 16 17 try 18 { 19 data = serialPortMain.ReadLine(); 20 data = data.Replace("\r", "").Replace("\n", ""); 21 } 22 catch (Exception ex) 23 { 24 MessageBox.Show(ex.Message); 25 } 26 27 AddRecievedDataDelegate add = new AddRecievedDataDelegate(AddRecievedData); 28 textBoxInfo.Invoke(add, data); 29 } 30 } 31 32 /// <summary> 33 /// 34 /// </summary> 35 /// <param name="data"></param> 36 private delegate void AddRecievedDataDelegate(string data); 37 /// <summary> 38 /// 39 /// </summary> 40 /// <param name="data"></param> 41 private void AddRecievedData(string data) 42 { 43 textBoxInfo.Text += data + "\r\n"; 44 textBoxInfo.SelectionStart = textBoxInfo.Text.Length; 45 textBoxInfo.ScrollToCaret(); 46 47 // 文字列調整 48 StringPartition(data); 49 }
:
・ReadExisting
C#
1 /// <summary> 2 /// データ受信が発生した時のイベント 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void serialPortMain_DataReceived(object sender, SerialDataReceivedEventArgs e) 7 { 8 if (!serialPortMain.IsOpen) 9 { 10 // 接続していない場合は処理を行わない 11 return; 12 } 13 else 14 { 15 string data = string.Empty; 16 17 try 18 { 19 data = serialPortMain.ReadExisting(); 20 } 21 catch (Exception ex) 22 { 23 MessageBox.Show(ex.Message); 24 } 25 26 AddRecievedDataDelegate add = new AddRecievedDataDelegate(AddRecievedData); 27 textBoxInfo.Invoke(add, data); 28 } 29 } 30 31 // 受信データ 32 private string TestStr = string.Empty; 33 34 /// <summary> 35 /// 36 /// </summary> 37 /// <param name="data"></param> 38 private delegate void AddRecievedDataDelegate(string data); 39 /// <summary> 40 /// 41 /// </summary> 42 /// <param name="data"></param> 43 private void AddRecievedData(string data) 44 { 45 textBoxInfo.Text += data; 46 textBoxInfo.SelectionStart = textBoxInfo.Text.Length; 47 textBoxInfo.ScrollToCaret(); 48 49 TestStr += data; 50 if (TestStr.EndsWith("\r") || TestStr.EndsWith("\n")) 51 { 52 // 改行コードまできたら 53 string[] str = TestStr.Split(new string[] { "\r\n" }, StringSplitOptions.None); 54 55 for(int i = 0; i < str.Length;i++) 56 { 57 if(str[i].Length == 0) { continue; } 58 59 // 改行コードが残っていたら 60 string str1 = str[i].Replace("\r", "").Replace("\n", ""); 61 62 // 文字列調整 63 StringPartition(str1); 64 } 65 66 TestStr = string.Empty; 67 } 68 }
###補足情報(言語/FW/ツール等のバージョンなど)
C#、Visual Studio 2015

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/15 05:31 編集
2017/02/15 05:54 編集
2017/02/15 06:01 編集
2017/02/15 06:00
2017/02/15 06:07
2017/02/15 06:19
2017/02/15 07:26