Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Convert UDP Receive script to Send instead (or as well)..

Discussion in 'Scripting' started by hardwiredstudios, Apr 24, 2017.

  1. hardwiredstudios

    hardwiredstudios

    Joined:
    Jan 27, 2017
    Posts:
    6
    Hey all :)

    I'm currently using the scripts below (found somewhere on here or somewhere haha) to send data via UDP from Max/MSP to Unity and its working perfectly.

    However, i now need it to send data back to Max/MSP. What modifications do i need to do to the code to do that? I'm guessing its not so simple as to change all the words "Read" / "Receive" to "Write" / "Send" but other UDP send scripts I've tried, communicate with Max but I get an error that end saying the message is basically in the wrong format.

    I just need the reverse of what the UDPReceive and Read scripts do, as they're working. I'm a total newbie to Unity and C# so kind of stuck filling in many blanks in my knowledge and don't fully understand what i'm doing so any help would be greatly appreciated.

    Thanks,

    Paul..


    UDPReceive.cs (attached to empty "MaxSendRev" object)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Text;
    6. using System.Net;
    7. using System.Net.Sockets;
    8. using System.Text.RegularExpressions;
    9. using System.Threading;
    10.  
    11. public class UdpReceive : MonoBehaviour
    12. {
    13.    public int port = 2002;
    14.    private UdpClient client;
    15.    private IPEndPoint RemoteIpEndPoint;
    16.    private Thread t_udp;
    17.    public float[] maxValues;
    18.  
    19.  
    20.    void Start()
    21.    {
    22.        client = new UdpClient(port);
    23.        RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    24.        t_udp = new Thread(new ThreadStart(UDPRead));
    25.        t_udp.Name = "UDP thread";
    26.        t_udp.Start();
    27.  
    28.  
    29.        //FilterData(test);
    30.    }
    31.  
    32.    public void UDPRead()
    33.    {
    34.        while (true)
    35.        {
    36.            try
    37.            {
    38.                //Debug.Log("listening UDP port " + port);
    39.                byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
    40.                string returnData = Encoding.ASCII.GetString(receiveBytes);
    41.                // parsing //
    42.                FilterData(returnData);
    43.            }
    44.            catch (Exception e)
    45.            {
    46.                Debug.Log("Not so good " + e.ToString());
    47.            }
    48.            Thread.Sleep(20);
    49.        }
    50.    }
    51.  
    52.    void OnDisable()
    53.    {
    54.        if (t_udp != null) t_udp.Abort();
    55.        client.Close();
    56.    }
    57.  
    58.    public float MaxValue(int index)
    59.    {
    60.        return maxValues[index];
    61.    }
    62.  
    63.    public void FilterData(string dataString)
    64.    {
    65.        string[] splitString = dataString.Split(":"[0]);
    66.        maxValues = new float[splitString.Length];
    67.  
    68.        for( int i=0; i<maxValues.Length; i++ ) {
    69.            maxValues[i] = float.Parse(splitString[i]);
    70.        }      
    71.    }
    72. }

    Read.cs (attached to "Pivot" parent of Handle)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Read : MonoBehaviour {
    6.  
    7.    private UdpReceive udpRec;
    8.    public GameObject PivotX;
    9.    public GameObject HandleX;
    10.    public GameObject ZombieX;
    11.  
    12.    void Start ()
    13.    {
    14.        Application.runInBackground = true;
    15.        //using find //
    16.        // THIS FIND A SPECIFIC GAME OBJECT THAT THE RECEIVE SCRIPT IS ATTACHED TO BUT THE ACTUAL MANIPULATION HAPPENS
    17.        // TO AN OBJECT THAT THE READ SCRIPT IS ATTACHED TO..
    18.        udpRec = GameObject.Find("MaxSendRev").GetComponent(typeof(UdpReceive)) as UdpReceive;
    19.  
    20.        // FIND THE RELEVANT GAMEOBJECTS..
    21.        PivotX = GameObject.Find ("Pivot");
    22.        HandleX = GameObject.Find ("Handle");
    23.    }
    24.  
    25.    // Update is called once per frame
    26.    void Update ()
    27.    {
    28.        if (udpRec.maxValues.Length <= 0) {
    29.            return;
    30.        }
    31.  
    32.        // COORDS TO ROTATE THE HANDLE..
    33.        float rotHX = ClampAngle(udpRec.MaxValue(0));
    34.        float rotHY = ClampAngle(udpRec.MaxValue(1));
    35.        float rotHZ = ClampAngle(udpRec.MaxValue(2));
    36.  
    37.        // COORDS TO ROTATE THE DOOR..
    38.        float rotDX = ClampAngle(udpRec.MaxValue(3));
    39.        float rotDY = ClampAngle(udpRec.MaxValue(4));
    40.        float rotDZ = ClampAngle(udpRec.MaxValue(5));
    41.  
    42.        // ROTATE THE HANDLE..
    43.        HandleX.transform.localEulerAngles = new Vector3(rotHX, rotHY, rotHZ);
    44.        // ROTATE THE DOOR..
    45.        PivotX.transform.localEulerAngles = new Vector3(rotDX, rotDY, rotDZ);
    46.    }
    47.  
    48.    public static float ClampAngle(float angle)
    49.    {
    50.        if(angle < -360f)
    51.        angle += 360f;
    52.        if(angle > 360)
    53.        angle -= 360;
    54.  
    55.        return Mathf.Clamp(angle, -360f, 360f);
    56.    }
    57. }
    58.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. Gautzillaa

    Gautzillaa

    Joined:
    May 23, 2018
    Posts:
    6
    Hello Paul,

    I am very interested in communication between MaxMSP and Unity for a research program.
    I am very new to Unity and I know nothing about C# (I just have some very basic skills in programming), and I am trying to figure out how to translate a game object in my Unity scene using MaxMSP.

    EDIT: I had a lot of issues because of my very low knowledge in both C# scripting and Unity. I finally managed to do this, and I'll just explain how in case it could help some people that want to do more or less the same thing.

    I modified your code to have this:

    Read.cs (attached to "HP")

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class Read : MonoBehaviour {
    5.    private UdpReceive udpRec;
    6.    public GameObject HPX;
    7.    void Start ()
    8.    {
    9.        Application.runInBackground = true;
    10.        //using find //
    11.        // THIS FIND A SPECIFIC GAME OBJECT THAT THE RECEIVE SCRIPT IS ATTACHED TO BUT THE ACTUAL MANIPULATION HAPPENS
    12.        // TO AN OBJECT THAT THE READ SCRIPT IS ATTACHED TO..
    13.        udpRec = GameObject.Find("MaxSendRev").GetComponent(typeof(UdpReceive)) as UdpReceive;
    14.        // FIND THE RELEVANT GAMEOBJECTS..
    15.        HPX = GameObject.Find ("HP");
    16.    }
    17.    // Update is called once per frame
    18.    void Update ()
    19.    {
    20.        if (udpRec.maxValues.Length <= 0) {
    21.            return;
    22.        }
    23.        // COORDS TO TRANSLATE THE LOUDSPEAKER..
    24.        float posZ = udpRec.MaxValue(0);
    25.        // TRANSLATE THE LS..
    26.        HPX.transform.position = new Vector3(0, 0, posZ);
    27.    }
    28.  
    29. }
    Then I had some issues because I forgot to send the floats through a "tosymbol" object in MaxMSP. Now I can just move my HP game object in the Unity Scene by scrolling the slider in this patch:

    <pre><code>
    ----------begin_max5_patcher----------
    430.3oc0T00aBCBE84Zx9OP304b.0O2ekEiAaYSLTnAntZL6+93CqQc0rF0X
    xdga3vE3bNWtr6odIvkpZlABdC7NHIYmCIIf4QRZ.RfEz5LA0DRDJYeoVtF1
    e+ZVVsMfWkWZXxbfPkQEqTFKffPjC4wyCY415K3CfkTa1Jt7yEZVlMRCLFO.
    0GjNYhOfSCABY.BLuYWF6VAKbZGNHYUgpxJX1.GQGAykMn3.32O0yGcg92rj
    eFfFLpMENNsCRjLkDT5zqPg3F3HlcaIKd1vODJp6dl2pEPt6Vvq.LZPqVPWp
    xjgAwmN7+fEXD7bltMsNZZGz5LTPjgQ7Pz0o0RplVvrL8BljtLtIzEMhK4A2
    +NAqxrsXoRzp6LrK86jw9vnY20WBc0.hoAEb4u9MLPY+BmYKFUkNq4dZjI3H
    ZmyLVtjZ4J4QYgOMoU77bl7jpXN23qrA+Cc4BUm4k+un+lWmy9GAwvcgXmS+
    G.w7sycfX2Vob+SNZY4Fl1r+nibx0xsVo8ymzONmKiyIw4Z1FdyVhWFjpcMV
    VWWUkN1qTOdeaGrP491RVwad3G3fa3G.jSeuPA
    -----------end_max5_patcher-----------
    </code></pre>


    Many thanks Paul for sharing these codes,
    Gauthier
     
    Last edited: May 23, 2018