Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. using System.Threading;
  7. using UnityEngine;
  8. using System.Collections;
  9.  
  10. public class DJISimulatorReceiver : MonoBehaviour {
  11.  
  12.     private const int listenPort = 5566;
  13.     bool done;
  14.     UdpClient listener = null;
  15.     IPEndPoint groupEP = null;
  16.     string received_data;
  17.     byte[] receive_byte_array;
  18.     Thread receiveThread;
  19.  
  20.     void Start () {
  21.         done = false;
  22.         listener = new UdpClient(listenPort);
  23.         groupEP = new IPEndPoint(IPAddress.Any, listenPort);
  24.         receiveThread = new Thread(new ThreadStart(ReceiveData));
  25.         receiveThread.IsBackground = true;
  26.         receiveThread.Start();
  27.     }
  28.  
  29.     void Update () {
  30.         // TODO 2: UPDATE VIRTUAL DRONE's POSITION!!
  31.     }
  32.  
  33.     void ReceiveData()
  34.     {
  35.         while(true)
  36.         {
  37.             try
  38.             {
  39.                 receive_byte_array = listener.Receive(ref groupEP);
  40.                 received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
  41.                 Debug.Log(">>" + received_data);
  42.                 // TODO 1: RESEARCH HOW TO PROCESS received_data (JSON). FORMAT:
  43.                 // { “Roll”: 0.0, “Pitch”: 0.0, “Yaw”: 0.0, “WorldX”: 0.0, “WorldY”: 0.0, “WorldZ”: 0.0, “WorldLatitude”: 0.0, “WorldLongitude”: 0.0, “VelocityX”: 0.0, “VelocityY”: 0.0, “VelocityZ”:  0.0, “Time”: 0, “TransformState”: 0, “MotorStarted”: 0, “FlyingState”: 0 “ProductType”: 0, “SimulatorCommand”: 0 }
  44.             }
  45.             catch (Exception e)
  46.             {
  47.                 Console.WriteLine(e.ToString());
  48.             }
  49.         }
  50.     }
  51.  
  52.     void OnApplicationQuit()
  53.     {
  54.         listener.Close();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement