我的C#应用程序中的此函数会将字母U发送到另一台PC上的lua应用程序:
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
我究竟做错了什么?我没有收到任何数据...
谢谢。
最佳答案
好的,答案是将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";
您必须添加新的换行符,否则它将不起作用。
关于c# - 使用套接字将数据从C#应用程序发送到Lua应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864735/