Help to learn Establish UDP connection Between MATLAB and UNITY 3D.

22 次查看(过去 30 天)
I want to learn establish a communication system between unity3D and MATLAB to transfer Data from MATLAB to The virtual Environment we are working on.

回答(2 个)

Larasmoyo Nugroho
编辑:Larasmoyo Nugroho 2017-6-9
First*, Matlab as server and unity as client
// matlab code
clc
clear all
tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server');
while(1)
data = membrane(1);
fopen(tcpipServer);
rawData = fread(tcpipServer,14,'char');
for i=1:14 rawwData(i)= char(rawData(i));
end
fclose(tcpipServer);
end
// Unity C# code
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System;
using System.IO;
public class Socket : MonoBehaviour {
// Use this for initialization
internal Boolean socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "localhost";
Int32 Port = 55000;
void Start () {
setupSocket ();
Debug.Log ("socket is set up");
}
// Update is called once per frame
void Update () {
}
public void setupSocket() {
try {
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
socketReady = true;
writeSocket("yah!! it works");
Debug.Log ("socket is sent");
}
catch (Exception e) {
Debug.Log("Socket error: " + e);
}
}
}
  1 个评论
Rakhi Agarwal
Rakhi Agarwal 2020-5-12
Do you have a way to get constant data transfer between unity and matlab? From above code, I can send data from Matlab to untiy once and then connection is closed. Is there a way to constantly transfer data from Matlab to unity?

请先登录,再进行评论。


Larasmoyo Nugroho
Second, unity as server and Matlab as client
Unity C# code
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System;
using System.IO;
using System.Text;
public class readSocket : MonoBehaviour {
// Use this for initialization
TcpListener listener;
String msg;
void Start () {
listener=new TcpListener (55001);
listener.Start ();
print ("is listening");
}
// Update is called once per frame
void Update () {
if (!listener.Pending ())
{
}
else
{
print ("socket comes");
TcpClient client = listener.AcceptTcpClient ();
NetworkStream ns = client.GetStream ();
StreamReader reader = new StreamReader (ns);
msg = reader.ReadToEnd();
print (msg);
}
}
}
//Matlab code
clc
clear all
tcpipClient = tcpip('127.0.0.1',55001,'NetworkRole','Client');
set(tcpipClient,'Timeout',30);
fopen(tcpipClient);
a='yah!! we could make it';
fwrite(tcpipClient,a);
fclose(tcpipClient);
in each implementation the server should runs first before the client otherwise you will get the bellow error
Connection refused:in Matlab or remote machine actively refuse the connection in unity

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by