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

Hey all :slight_smile:

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)

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;

public class UdpReceive : MonoBehaviour
{
   public int port = 2002;
   private UdpClient client;
   private IPEndPoint RemoteIpEndPoint;
   private Thread t_udp;
   public float[] maxValues;


   void Start()
   {
       client = new UdpClient(port);
       RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
       t_udp = new Thread(new ThreadStart(UDPRead));
       t_udp.Name = "UDP thread";
       t_udp.Start();


       //FilterData(test);
   }

   public void UDPRead()
   {
       while (true)
       {
           try
           {
               //Debug.Log("listening UDP port " + port);
               byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
               string returnData = Encoding.ASCII.GetString(receiveBytes);
               // parsing //
               FilterData(returnData);
           }
           catch (Exception e)
           {
               Debug.Log("Not so good " + e.ToString());
           }
           Thread.Sleep(20);
       }
   }

   void OnDisable()
   {
       if (t_udp != null) t_udp.Abort();
       client.Close();
   }

   public float MaxValue(int index)
   {
       return maxValues[index];
   }

   public void FilterData(string dataString)
   {
       string[] splitString = dataString.Split(":"[0]);
       maxValues = new float[splitString.Length];

       for( int i=0; i<maxValues.Length; i++ ) {
           maxValues[i] = float.Parse(splitString[i]);
       }       
   }
}

Read.cs (attached to “Pivot” parent of Handle)

using UnityEngine;
using System.Collections;

public class Read : MonoBehaviour {

   private UdpReceive udpRec;
   public GameObject PivotX;
   public GameObject HandleX;
   public GameObject ZombieX;

   void Start ()
   {
       Application.runInBackground = true;
       //using find //
       // THIS FIND A SPECIFIC GAME OBJECT THAT THE RECEIVE SCRIPT IS ATTACHED TO BUT THE ACTUAL MANIPULATION HAPPENS
       // TO AN OBJECT THAT THE READ SCRIPT IS ATTACHED TO..
       udpRec = GameObject.Find("MaxSendRev").GetComponent(typeof(UdpReceive)) as UdpReceive;

       // FIND THE RELEVANT GAMEOBJECTS..
       PivotX = GameObject.Find ("Pivot");
       HandleX = GameObject.Find ("Handle");
   }

   // Update is called once per frame
   void Update ()
   {
       if (udpRec.maxValues.Length <= 0) {
           return;
       }

       // COORDS TO ROTATE THE HANDLE..
       float rotHX = ClampAngle(udpRec.MaxValue(0));
       float rotHY = ClampAngle(udpRec.MaxValue(1));
       float rotHZ = ClampAngle(udpRec.MaxValue(2));

       // COORDS TO ROTATE THE DOOR..
       float rotDX = ClampAngle(udpRec.MaxValue(3));
       float rotDY = ClampAngle(udpRec.MaxValue(4));
       float rotDZ = ClampAngle(udpRec.MaxValue(5));

       // ROTATE THE HANDLE..
       HandleX.transform.localEulerAngles = new Vector3(rotHX, rotHY, rotHZ);
       // ROTATE THE DOOR..
       PivotX.transform.localEulerAngles = new Vector3(rotDX, rotDY, rotDZ);
   }

   public static float ClampAngle(float angle)
   {
       if(angle < -360f)
       angle += 360f;
       if(angle > 360)
       angle -= 360;

       return Mathf.Clamp(angle, -360f, 360f);
   }
}

Just lookup info on UdpClient and/or UdpClient.Send It’s not Unity specific, there is a lot of information on the web for this, already.
Just an example: UdpClient.Send Method (System.Net.Sockets) | Microsoft Learn

If I missed something to your question, please explain. :slight_smile:

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

using UnityEngine;
using System.Collections;
public class Read : MonoBehaviour {
   private UdpReceive udpRec;
   public GameObject HPX;
   void Start ()
   {
       Application.runInBackground = true;
       //using find //
       // THIS FIND A SPECIFIC GAME OBJECT THAT THE RECEIVE SCRIPT IS ATTACHED TO BUT THE ACTUAL MANIPULATION HAPPENS
       // TO AN OBJECT THAT THE READ SCRIPT IS ATTACHED TO..
       udpRec = GameObject.Find("MaxSendRev").GetComponent(typeof(UdpReceive)) as UdpReceive;
       // FIND THE RELEVANT GAMEOBJECTS..
       HPX = GameObject.Find ("HP");
   }
   // Update is called once per frame
   void Update ()
   {
       if (udpRec.maxValues.Length <= 0) {
           return;
       }
       // COORDS TO TRANSLATE THE LOUDSPEAKER..
       float posZ = udpRec.MaxValue(0);
       // TRANSLATE THE LS..
       HPX.transform.position = new Vector3(0, 0, posZ);
   }

}

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:


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

Many thanks Paul for sharing these codes,
Gauthier