Help to learn Establish UDP connection Between MATLAB and UNITY 3D.
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (2)
Larasmoyo Nugroho
el 9 de Jun. de 2017
Editada: Larasmoyo Nugroho
el 9 de Jun. de 2017
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 comentario
Rakhi Agarwal
el 12 de Mayo de 2020
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
el 9 de Jun. de 2017
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
0 comentarios
Ver también
Categorías
Más información sobre Application Deployment en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!