能够通过 TcpClient 类和方法从设备读取数据如下。
一旦我通过以太网连接使用 Ipaddress 和端口建立连接,就可以读取数据字节。我的读取方法读取特定字节数的网络流数据并根据需要修剪字符串并更新到数据库并尝试在datagridview中显示更新的数据。它不会在datagridview中显示任何数据。
任何人都可以建议我该怎么做?
添加代码:
private void ReceiveMethod()
{
try
{
string IP1 = textIP.Text.Trim();
string port1 = textPort.Text.Trim();
int port = Convert.ToInt32(port1);
NetworkStream ns;
int bytesRead = 0;
byte[] buffer = new byte[9];
try
{
IPAddress ipAddress = System.Net.IPAddress.Parse(Ip1);
client = new TcpClient();
client.Connect(IP1, port);
while (true)
{
ns = client.GetStream();
ns.Read(buffer, (int)bytesRead, buffer.Length - (int)bytesRead);
ASCIIEncoding encoder = new ASCIIEncoding();
msg = encoder.GetString(buffer);
GetData(msg);
}
client.Close();
}
GetData(string data)
{
I'm trimming the data value according to my need
and the updateing one filed of db and trying to populate table data in gridview
//if i remove the while(true) in the Receivemethod am able to view the data in
datagridview but i want to read continuous data from network stream and want
to show updated data in datagrid view
}