I receive the data which are X, Y, Z position and X, Y, Z rotation by Using UDP real time(60hz)
I used the code by la1n.(simple udp implementation (send/read via mono/c#) - Unity Engine - Unity Discussions)
I write the code myself by using la1n’s code. But I have some troubles.
First, I think I complete string split and parse by using array.
Is there any problem in split and parse?
I have a problem about matching data(x,y,z pos and x,y,z rot) to Object(box).
I attached my code(which are c# and lua script)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour
{
// receiving Thread
Thread receiveThread;
// udpclient object
UdpClient client;
// port
public int port = 8051; // define > init
// infos
public string lastReceivedUDPPacket = "";
// start from shell
private static void Main()
{
UDPReceive receiveObj = new UDPReceive();
receiveObj.init();
string text = "";
do
{
text = Console.ReadLine();
}
while (!text.Equals("exit"));
}
// start from unity3d
public void Start()
{
init();
}
void SetTransform(float XPos, float YPos, float ZPos)
{
transform.position = new Vector3(XPos, YPos, ZPos);
}
void SetRotation(float XRot, float YRot, float ZRot)
{
transform.eulerAngles = new Vector3(XRot, YRot, ZRot);
}
void Update()
{
SetTransform();
SetRotation();
}
// OnGUI
void OnGUI()
{
Rect rectObj = new Rect(40, 10, 200, 400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj, "\nLast Packet: \n" + lastReceivedUDPPacket, style);
}
// init
private void init()
{
// Endpunkt definieren, von dem die Nachrichten gesendet werden.
print("UDPSend.init()");
// define port
port = 8051;
// status
print("Sending to 127.0.0.1 : " + port);
print("Test-Sending to this Port: nc -u 127.0.0.1 " + port + "");
// ----------------------------
// Abhören
// ----------------------------
// Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
// Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// Unity Application Quit Function
void OnApplicationQuit()
{
stopThread();
}
// Stop reading UDP messages
private void stopThread()
{
if (receiveThread.IsAlive)
{
receiveThread.Abort();
}
client.Close();
}
// receive thread
private void ReceiveData()
{
client = new UdpClient(port);
while (true)
{
try
{
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
string text = Encoding.UTF8.GetString(data);
// Data Received from simulation
print("Received Data >> " + text);
// latest UDPpacket
lastReceivedUDPPacket = text;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
private void parseDataToBox ( string text )
{
try
{
float[] floatData = Array.ConvertAll(text.Split(';'), float.Parse);
float XPos = floatData[0];
float YPos = floatData[1];
float ZPos = floatData[2];
float XRot = floatData[3];
float YRot = floatData[4];
float ZRot = floatData[5];
}
catch (Exception err)
{
print(err.ToString());
}
}
}
This code is Lua
socket = require("socket")
print(socket._VERSION)
function dataListener:post( t )
local XPos = ship.rb:getPosition():x()
local YPos = ship.rb:getPosition():y()
local ZPos = ship.rb:getPosition():z()
local XXPos = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYPos = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZPos = math.floor( ZPos * 1000 + 0.5 ) / 1000
local XRot = ship.rb:getRotation():x()
local YRot = ship.rb:getRotation():y()
local ZRot = ship.rb:getRotation():z()
local XXRot = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYRot = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZRot = math.floor( ZPos * 1000 + 0.5 ) / 1000
udp=socket.udp();
udp:setpeername("127.0.0.1",8051)
udp:send(string.format("%f; %f; %f; %f; %f; %f", XXPos, YYPos, ZZPos, XXRot, YYRot, ZZRot));
end