从C#应用程序向Lua应用程序发送数据使用套接字

我的 C# 应用程序中的此函数将向另一台PC上的 lua 应用程序发送字母 U:

private void drive_Click(object sender, RoutedEventArgs e)
{
    Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.180");
    IPEndPoint remote = new IPEndPoint(ipAddress, 1337);
    soc.Connect(remote);

    byte[] commands = System.Text.Encoding.ASCII.GetBytes("U");
    soc.Send(commands);
}

这是在 lua 中接收来自 C# 应用程序的命令并将其写入串行端口的小脚本(它工作正常,我使用 netcat 发送字符并且一切都正常。)

local socket = require("socket")
local server = assert(socket.bind("*", 1337))

wserial=io.open("/dev/ttyATH0","w")

while 1 do
  local client = server:accept()
  client:settimeout(10)

  local line, err = client:receive()

  if not err then client:
    wserial:write(line)
  end

  wserial:flush()
  client:close()
end

我做错了什么?我没有收到任何数据...

谢谢。

点赞
用户88888888
用户88888888

Ok, 答案是将我的 C# 代码更改为以下内容:

TcpClient tcp = new TcpClient("192.168.1.4", 1337);
string cmd = "U\n";
byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace("\0xFF", "\0xFF\0xFF"));
tcp.GetStream().Write(buf, 0, buf.Length);

注意

string cmd = "U\n";

你必须添加换行符,否则它将无效。

2013-04-07 19:27:12