我使用此Telnet库:http://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library
我将c#控制台程序从该链接转换为C#Winform程序。我能够将数据发送到我的设备(微控制器,具体而言),我实际上是在这部分完成的。问题是我怎么想读取设备/服务器的响应?
如果您要打开链接并阅读代码的某些部分如何使用该库,则会在此处显示如何将数据发送到服务器并从服务器读取数据。
// while connected
while (tc.IsConnected && prompt.Trim() != "exit" )
{
// display server output
Console.Write(tc.Read());
// send client input to server
prompt = Console.ReadLine();
tc.WriteLine(prompt);
// display server output
Console.Write(tc.Read());
}
我使用WriteLine向设备发送数据,正如我所说,我已经完成了这项工作。我只是不知道把tc.Read()放在哪里;参与我的winform计划。
设备连续且非常快地发送长度为89的长字符串!
传感器1:0.10米传感器2:1.51米传感器3:2.03米传感器4:1.7米温度:21°C
顺便说一下,我会将每个值都放到特定的标签上。所以我的代码是使用substring获取值:
private void timer2_Tick(object sender, EventArgs e)
{
//"Sensor1: 0.00meters Sensor2: 0.00meters Sensor3: 0.00meters Sensor4: 0.00meters 21.00degC"
if (connectedChecker1 == true)
{
dataReceived = tc.Read();
if (dataReceived.Length == 89)
{
tempVal.Text = (str1.Substring(80, 5)) + "°C";
frontVal.Text = str1.Substring(9, 10);
leftVal.Text = str1.Substring(29, 10);
backVal.Text = str1.Substring(49, 10);
rightVal.Text = str1.Substring(69, 10);
dataReceived = "";
}
}
}
大声笑,原谅我,我真的不知道把这部分放在哪里。我计算接收数据的长度但是如果在通信过程中发生这种延迟并且C#程序将响应读取为:
gCsensor1:0.10米传感器2:1.51米传感器3:2.03米传感器4:1.7米温度:21度
我现在想把一个while循环放在一个用于连接的按钮中:
private void vesselBoardToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show(ethernetIP.ToString());
//MessageBox.Show(servoUnoIp.ToString());
try
{
//create a new telnet connection for MEGA
tc = new TelnetConnection(ethernetIP, 23);
if (tc.IsConnected)
connectedChecker1 = true;
//create a new telnet connection for Uno
unoTc = new TelnetConnection(servoUnoIp, 23);
if (unoTc.IsConnected)
connectedChecker = true;
statusLbl.Text = "Connected";
//INSERT WHILE LOOP HERE FOR RECEIVING DATA....
}
catch (Exception ex)
{
MessageBox.Show("Check the ip address of the boards");
}
}