我正在尝试从 WPF C# 应用程序向作为服务器运行的 Arduino 发送一个单词。时不时地不发送完整的工作。
C# 代码
public void send(String message)
{
TcpClient tcpclnt = new TcpClient();
ConState.Content = "Connecting.....";
try
{
tcpclnt.Connect("192.168.0.177", 23);
ConState.Content = "Connected";
String str = message;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
tcpclnt.Close();
}
catch (Exception)
{
ConState.Content = "Not Connected";
return;
}
}
它是如何发送到方法的:
String mes = "back;";
send(mes);
Arduino代码:
if (client.available() > 0) {
// Read the bytes incoming from the client:
char thisChar = client.read();
if (thisChar == ';')
{
//Add a space
Serial.println("");
}
else {
//Print because it's not a space
Serial.write(thisChar);
}
}
Arduino 正在使用聊天服务器示例。我正在发送“回”;和“前进”;穿过。串行监视器上的结果:
back
forwaback
forward
back
forwaforwar