2016-02-13 100 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(); 
      } 

     }