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

22 ビュー (過去 30 日間)
Umang Shah
Umang Shah 2015 年 1 月 29 日
コメント済み: Rakhi Agarwal 2020 年 5 月 12 日
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 日
編集済み: 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
Larasmoyo Nugroho 2017 年 6 月 9 日
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 Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by