2016-02-13 36 views
0

我開發了一個從秤讀取數據的項目。 我可以讀取刻度寄給我的數據;但是,收到的數據集不方便。規模品牌是Desis。TCPClient不正確讀取數據

樣品輸出如下。

Output: 

T,GS 17.27 g 
ST,GS 17.27 g 
T,GS 17.27 g 
T,GS 17.27 g 
27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
30.89 g 

ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 
ST,GS 17.27 g 

收到的數據必須是ST,GS 17.27 g連續。

我的書面代碼:

public void Connect() 
{ 
      m_tcpClient = new TcpClient(); 

      m_tcpClient.Connect(m_hostAdress, m_port); 

} 

public string ReadWithNewLine() 
{ 

      this.Connect(); 
      m_netWorkStream = m_tcpClient.GetStream(); 
      m_streamReader = new StreamReader(m_netWorkStream); 

      if (m_streamReader != null) 
      { 
       try 
       { 
        m_readText = m_streamReader.ReadLine().TrimEnd(); 

       } 
       catch (Exception ex) 
       { 

       } 
      } 
      return m_readText + "\r\n"; 
} 

是否有任何代碼錯誤?

+0

* ReadLine可能會出錯,但您不會知道它,因爲您正在使用空的「catch」塊隱藏錯誤。 –

+0

你打電話給ReadWithNewLine? – rene

回答

0

我不認爲你的代碼中有任何錯誤。但我認爲你一次只讀一行。如果你想讀的所有行一次,然後就可以使用

m_readText = m_streamReader.ReadToEnd().TrimEnd(); 

但如果你想按行讀入行,並獲取數據,你在問問題的話,我認爲發件人在該序列發送數據。

+0

我使用Thread.Timer類,我需要逐行閱讀,我想看到超級終端的輸出。在超級終端中,我逐行看到數據。我該怎麼辦? – user2895809

0

您正在ReadWithNewLine的每次調用中重新創建TcpClient。如果你這樣做了,那麼另一端的TcpServer必須關閉舊連接並啓動一個新連接,然後協商每次會丟失幾個字節的連接。

住在儘可能靠近你目前的設計我建議你試試你的設置有以下變化:

public void Connect() 
    { 
     if (m_tcpClient == null) 
     { 
      m_tcpClient = new TcpClient(); 
      m_tcpClient.Connect(m_hostAdress, m_port); 
      m_netWorkStream = m_tcpClient.GetStream(); 
      m_streamReader = new StreamReader(m_netWorkStream); 
     } 
    } 

    public string ReadWithNewLine() 
    { 

     this.Connect(); 

     try 
     { 
      m_readText = m_streamReader.ReadLine().TrimEnd(); 
     } 
     catch (Exception allExceptions) 
     { 
      m_readText = "Exception:" + allExceptions.Message; 
      if (m_tcpClient!=null) 
      { 
       m_tcpClient.Close(); 
       m_tcpClient = null; // reset connection 
      } 
     } 
     return m_readText + "\r\n"; 
    } 

通知我在Connect方法如何檢查確認存在已經是TcpClient的一個實例。如果不是,我創建一個並建立流和讀者。

您第一次打電話ReadWithNewLine它調用Connect並設置與您的規模的連接。在下一次調用中,Connect將不會執行任何操作,並重新使用現有的讀取器來獲取字節。

我也稍微改變了你的異常處理。我至少可以做的是提供一些反饋並重置TcpClient。您可能需要進一步加強以僅捕獲那裏發生的特定異常。

0

有很多方法可以用來從TCPClient中檢索響應數據。我使用流式代碼來格式化輸出。

 private int TCPTimeOut = 10000; 
     private int NetStreamTimeOut = 10000; 
     private string ipAddress = ""; 
     private int port; 
     private int intThreadSleep; 
     private bool disposed = false; 
     private TcpClient tcpClient = new TcpClient(); 

     private void GetTCPData() 
     { 
      tcpClient.Connect(ipAddress, port); 
      tcpClient.ReceiveTimeout = TCPTimeOut; 
      NetworkStream stream = tcpClient.GetStream(); 
      stream.ReadTimeout = NetStreamTimeOut; 

      Byte[] data = new Byte[5210]; 
      string responseData = String.Empty; 

      while (true) 
      { 
       Int32 bytes = stream.Read(data, 0, data.Length); 
       System.Threading.Thread.Sleep(intThreadSleep); 
       responseData = responseData + System.Text.Encoding.ASCII.GetString(data, 0, bytes); 

       if (bytes > 10) 
       { 
        var table = responseData.Split(new string[] { "\r\n", "\r", "\n" }, 
               StringSplitOptions.None); 

        if (table.Count() > 3) 
        { 
         string line1 = table[0]; 
         string line2 = table[1]; 
         string line3 = table[2]; 
         string line4 = table[3]; 
         string line5 = table[4]; 
         string line6 = table[5]; 

         // SaveData(ipAddress, line1, line2, line3, line4, line5, line6); 
        } 
       } 
       responseData = String.Empty; 
      } 

      if (tcpClient != null) 
      { 
       tcpClient.GetStream().Close(); 
       tcpClient.Close(); 
      } 

     }