我開發了一個從秤讀取數據的項目。 我可以讀取刻度寄給我的數據;但是,收到的數據集不方便。規模品牌是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";
}
是否有任何代碼錯誤?
* ReadLine可能會出錯,但您不會知道它,因爲您正在使用空的「catch」塊隱藏錯誤。 –
你打電話給ReadWithNewLine? – rene