gpt4 book ai didi

c# - 通过 UDP C# 将数据作为双 float 发送

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:13 24 4
gpt4 key购买 nike

大家好,

帖子简介:我需要帮助将下面的脚本转换为通过 UDP 作为双 float 而不是 UTF8 发送数据,发送的数据是通过命令“transform.position.x”获取的' 在 Unity 3D 中 :) 如果您想了解更多信息,请继续阅读。

我的大学硕士项目是开发赛车模拟器,我通过 UDP 将车辆位置和旋转数据从模拟软件发送到视觉对象,然后在视觉对象中分配传入的数据流,从而完成了所有工作的基础知识到 3D 车辆模型的每个位置或旋转矢量。现在,作为某些系统分析的一部分,我正在尝试测量由通信或以低得多的频率运行的视觉包对仿真软件造成的任何滞后或延迟。

为了衡量这一点,我想像平常一样在视觉环境中控制一个对象,但随后有另一个单独的脚本获取对象的坐标并将它们直接发送回另一个端口上的模拟软件,这就是我正在使用的脚本麻烦的是数据需要作为双 float 发送回。

发送和接收 UDP 脚本最初来自论坛: https://community.unity.com/t5/Multiplayer-Networking/simple-udp-implementation-send-read-via-mono-c/td-p/154952我尝试修改的 UDP 发送脚本也如下所示

目前,在这两个中,只有接收脚本被使用,当我遇到它时,它已经被修改为接收双 float ,我正在尝试做的是修改发送脚本以发送也是双 float ,它们最初都是为发送和接收 UTF8 编码文本而编写的。

此外,视觉包是 Unity 3D,命令“transform.position.x”是获取对象坐标的命令,因此这将是发送的内容,然后我将添加其他位置和旋转一旦我理解了这个方法,就可以使用向量。

另外,我可能应该提一下,我在编码方面的经验很少,因为这不是我项目的主要部分,但我真的很感激能得到一些帮助,因为我对这个微小的部分一无所知,现在已经用完了时间。

提前致谢!

using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class UDPSend : MonoBehaviour {

private static int localPort;

// prefs
private string IP; // define in init
public int port; // define in init

// "connection" things
IPEndPoint remoteEndPoint;
UdpClient client;

// gui
string strMessage="";


// call it from shell (as program)
private static void Main()
{
UDPSend sendObj=new UDPSend();
sendObj.init();

// testing via console
// sendObj.inputFromConsole();

// as server sending endless
sendObj.sendEndless(" endless infos \n");

}
// start from unity3d
public void Start()
{
init();
}

// OnGUI
void OnGUI()
{
Rect rectObj=new Rect(40,380,200,400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj,"# UDPSend-Data\n127.0.0.1 "+port+" #\n"
+ "shell> nc -lu 127.0.0.1 "+port+" \n"
,style);

// ------------------------
// send it
// ------------------------
strMessage=GUI.TextField(new Rect(40,420,140,20),strMessage);
if (GUI.Button(new Rect(190,420,40,20),"send"))
{
sendString(strMessage+"\n");
}
}

// init
public void init()
{
// Define end point , from which the messages are sent.
print("UDPSend.init()");

// define
IP="127.0.0.1";
port=16;

// ----------------------------
// Send
// ----------------------------
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();

// status
print("Sending to "+IP+" : "+port);
print("Testing: nc -lu "+IP+" : "+port);

}

// inputFromConsole
private void inputFromConsole()
{
try
{
string text;
do
{
text = Console.ReadLine();

// Send the text to the remote client .
if (text != "")
{

// encode data with the UTF8 encoding to binary .
byte[] data = Encoding.UTF8.GetBytes(text);

// Send the text to the remote client .
client.Send(data, data.Length, remoteEndPoint);
}
} while (text != "");
}
catch (Exception err)
{
print(err.ToString());
}

}

// sendData
private void sendString(string message)
{
try
{
//if (message != "")
//{

// encode data with the UTF8 encoding to binary .
byte[] data = Encoding.UTF8.GetBytes(message);

// Send the message to the remote client .
client.Send(data, data.Length, remoteEndPoint);
//}
}
catch (Exception err)
{
print(err.ToString());
}
}


// endless test
private void sendEndless(string testStr)
{
do
{
sendString(testStr);


}
while(true);

}

}

最佳答案

感谢您如此迅速地给出答案,但是我认为这不适用于我的情况,因为模拟软件是基于 Modelica 的,而且据我所知,它不适用于 UTF8 编码,这就是为什么我正在寻求改变它。

然而,在发布这个之后我有了一个想法并使用 Youtube 和其他论坛我能够编写一个脚本将双 float 转换为整数值并将它们乘以 1e6 然后将它们作为整数发送和接收并除以再次在模拟软件中将它们返回为小数。

...然后发现我可以使用该方法直接将其作为 double 发送。

如果它对任何人都有用,我会在这里发布脚本:

using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class LatencyTest : MonoBehaviour {

// Use this for initialization
public void Update () {

// Get coordinates
double x = transform.position.x;

// Create byte array for sending
byte[] coordinates = BitConverter.GetBytes(x);

// Define IP Address and Port
string IP = "127.0.0.1";
int port= 16;
Socket client;
IPEndPoint Dymola;

//Send to IP Address on the port specified above
Dymola = new IPEndPoint(IPAddress.Parse (IP), port);
client = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo (coordinates, Dymola);
//print (coordinates);
}
}

关于c# - 通过 UDP C# 将数据作为双 float 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38235795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com