Unity如何从Matlab socket中读取数据

时间:2016-03-13 22:29:59

标签: matlab sockets unity3d

我想在团结中做一些模拟。对于我的项目,我想使用Matlab作为处理层。所以我想使用socket进行通信。

这里Matlab是服务器,这是代码:

clc
clear all
tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server');
while(1)
    b=1
data = membrane(1);
b=2
s = whos('data');
b=3
set(tcpipServer,'OutputBufferSize',s.bytes);
b=4
fopen(tcpipServer);
b=5
fwrite(tcpipServer,'hi');
fclose(tcpipServer);
d=6
end

我使用1,2,..来显示过程

我统一使用此代码

using UnityEngine;
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");
        Debug.Log (readSocket()+" is the message");
        closeSocket ();
        Debug.Log ("Socket is closed");
    }

    // Update is called once per frame
    void Update () {

    }

    public void setupSocket() {
        try {
            mySocket = new TcpClient(Host, Port);
            theStream = mySocket.GetStream();
            theWriter = new StreamWriter(theStream);
            theReader = new StreamReader(theStream);
            socketReady = true;
        }
        catch (Exception e) {
            Debug.Log("Socket error: " + e);
        }
    }

    public String readSocket() {
        if (!socketReady)
            return "";
        if (theStream.DataAvailable)
            return theReader.ReadLine();
        return "";
    }


    public void closeSocket() {
        if (!socketReady)
            return;
        theWriter.Close();
        theReader.Close();
        mySocket.Close();
        socketReady = false;
    }
}

首先我开始使用Matlab,然后我发挥团结。结果是,连接正常完成但我无法读取Matlab发送的数据

这是我在Matlab中得到的

b =

     1


b =

     2


b =

     3


b =

     4


b =

     5


d =

     6


b =

     1


b =

     2


b =

     3


b =

     4

最后这就是我团结一致

socket is set up

is the message

Socket is closed

预期结果为

socket is set up

hi is the message

Socket is closed

我不知道如何阅读数据以及如何实现它。

谢谢

1 个答案:

答案 0 :(得分:0)

这是最简单,最完整的解决方案。 在这个解决方案中,matlab或unity中的每一个都可以用作服务器或客户端。

首先,Matlab作为服务器,统一作为客户端

matlab代码

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#代码

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);
        }
    }
}

现在反过来,统一作为服务器,Matlab作为客户

Unity C#代码

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代码

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);

在每个实现中,服务器应首先在客户端之前运行,否则您将收到以下错误

  

拒绝连接:在Matlab或远程机器中主动拒绝统一连接