// 受信データの処理 private void executeRecvData(string data) { // デバイス接続通知 if (data.StartsWith("onConnect ")) { deviceName = data.Substring(10); KoshiEventArgs args = new KoshiEventArgs(); args.data = deviceName; onConnect(this, args); } // デバイス切断通知 else if (data.StartsWith("onDisconnect")) { KoshiEventArgs args = new KoshiEventArgs(); onDisconnect(this, args); } // UART受信通知 else if (data.StartsWith("onUpdateUartRx ")) { string param = data.Substring(15); KoshiEventArgs args = new KoshiEventArgs(); args.data = param; onUpdateUartRx(this, args); } }
// デバイス接続通知ハンドラ private void onConnect(object sender, KoshiEventArgs e) { this.BeginInvoke((Action)(() => { string deviceName = e.data; textStatus.Text = "接続中: " + deviceName; })); }
// 受信スレッド関数 private void recvThreadFunc() { while (!toQuit) { //if (stream.DataAvailable) //{ try { //サーバーから送られたデータを受信する string data = reader.ReadLine(); if (data == null) { isOpen = false; stream.Close(); tcpClient.Close(); KoshiEventArgs args = new KoshiEventArgs(); onClosed(this, args); break; } // 受信した文字列の処理 executeRecvData(data); } catch (IOException ex) // TimeoutException ex) { ; // タイムアウト } //} //else //{ // Thread.Sleep(1); //} } }
// ネットワーク切断ハンドラ private void onClosed(object sender, KoshiEventArgs e) { this.BeginInvoke((Action)(() => { isOpen = false; buttonOpen.Text = "開く"; textStatus.Text = "未接続"; })); }
// デバイス切断通知ハンドラ private void onDisconnect(object sender, KoshiEventArgs e) { this.BeginInvoke((Action)(() => { textStatus.Text = "未接続"; })); }