一种Labview多通道数据采集通过TCP将数据传到UNITY 3D的实现方法

准备:Labview2018,unity 3D,数据采集卡

实现方法:

labview后面板:

 

 

 unity 3D代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;



public class tcp : MonoBehaviour
{
    private string inputIp = "192.168.244.202";   //输入ip地址
    private string inputPort = "111";           //输入端口号          
    private string inputPort2 = "222";
    private string inputPort3 = "333";
    private string inputPort4 = "444";
    private string inputPort5 = "555";
    private string inputPort6 = "666";



    public string recMes = "NULL";                                          
    

    public static double ax;      
    public static double ay;              
    public static double az;
    public static double T;
    public static double P;
    public static double dB;

    private Socket socketSend1;                   //客户端套接字,用来链接远端服务器                                    
    private Socket socketSend2;
    private Socket socketSend3;
    private Socket socketSend4;
    private Socket socketSend5;
    private Socket socketSend6;

    public static string s="False";
    

    

    //建立链接
    private void ClickConnect()
    {
        try
        {    
//端口1 
            int _port = Convert.ToInt32(inputPort);             //获取端口号
            string _ip = inputIp;                               //获取ip地址

            //创建客户端Socket,获得远程ip和端口号
            socketSend1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(_ip);
            IPEndPoint point = new IPEndPoint(ip, _port);

            socketSend1.Connect(point);
            Debug.Log("连接成功 , " + " ip = " + ip + " port = " + _port);
            
            Thread r_thread1 = new Thread(Received1);             //开启新的线程,不停的接收服务器发来的消息
            r_thread1.IsBackground = true;
            r_thread1.Start();

//端口2            
            int _port2 = Convert.ToInt32(inputPort2);            
            string _ip2 = inputIp;                               

           
            socketSend2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip2 = IPAddress.Parse(_ip2);
            IPEndPoint point2 = new IPEndPoint(ip2, _port2);

            socketSend2.Connect(point2);
            Debug.Log("连接成功 , " + " ip2 = " + ip2 + " port = " + _port2);

            Thread r_thread2 = new Thread(Received2);             
            r_thread2.IsBackground = true;
            r_thread2.Start();

//端口3
            int _port3 = Convert.ToInt32(inputPort3);            
            string _ip3 = inputIp;                               

           
            socketSend3 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip3 = IPAddress.Parse(_ip3);
            IPEndPoint point3 = new IPEndPoint(ip3, _port3);

            socketSend3.Connect(point3);
            Debug.Log("连接成功 , " + " ip3 = " + ip3 + " port = " + _port3);

            Thread r_thread3 = new Thread(Received3);             
            r_thread3.IsBackground = true;
            r_thread3.Start();

//端口4
            int _port4 = Convert.ToInt32(inputPort4);            
            string _ip4 = inputIp;                               

           
            socketSend4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip4 = IPAddress.Parse(_ip4);
            IPEndPoint point4 = new IPEndPoint(ip4, _port4);

            socketSend4.Connect(point4);
            Debug.Log("连接成功 , " + " ip4 = " + ip4 + " port = " + _port4);

            Thread r_thread4 = new Thread(Received4);             
            r_thread4.IsBackground = true;
            r_thread4.Start();
//端口5
            int _port5 = Convert.ToInt32(inputPort5);            
            string _ip5 = inputIp;                               

           
            socketSend5 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip5 = IPAddress.Parse(_ip5);
            IPEndPoint point5 = new IPEndPoint(ip5, _port5);

            socketSend5.Connect(point5);
            Debug.Log("连接成功 , " + " ip5 = " + ip5 + " port = " + _port5);

            Thread r_thread5 = new Thread(Received5);             
            r_thread5.IsBackground = true;
            r_thread5.Start();
//端口6
            int _port6 = Convert.ToInt32(inputPort6);            
            string _ip6 = inputIp;                               

           
            socketSend6 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip6 = IPAddress.Parse(_ip6);
            IPEndPoint point6 = new IPEndPoint(ip6, _port6);

            socketSend6.Connect(point6);
            Debug.Log("连接成功 , " + " ip6 = " + ip6 + " port = " + _port6);

            Thread r_thread6 = new Thread(Received6);             
            r_thread6.IsBackground = true;
            r_thread6.Start();


        }
        catch (Exception)
        {
            Debug.Log("IP或者端口号错误......");
        }
    }

    /// <summary>
    /// 接收服务端返回的消息
    /// </summary>
    void Received1()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
                //实际接收到的有效字节数

                int len=socketSend1.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                ax = double.Parse(recMes);
                Debug.Log("客户端接收到的数据ax: " + ax);
             
            }
            catch { }
        }
    }
 
    void Received2()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
               
                int len=socketSend2.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                ay = double.Parse(recMes);
                Debug.Log("客户端接收到的数据ay: " + ay);
             
            }
            catch { }
        }
    }

    void Received3()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
               
                int len=socketSend3.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                az = double.Parse(recMes);
                Debug.Log("客户端接收到的数据az: " + az);
           }
            catch { }
        }
    }

      void Received4()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
               
                int len=socketSend4.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                T = double.Parse(recMes);
                Debug.Log("客户端接收到的数据T: " + T);
            }
            catch { }
        }
    }    
      void Received5()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
               
                int len=socketSend5.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                P = double.Parse(recMes);
                Debug.Log("客户端接收到的数据P: " + P);
            }
            catch { }
        }
    }    
      void Received6()
    {
        while (true)
        {
            try
            {
                byte[] buffer = new byte[1024];
               
                int len=socketSend6.Receive(buffer, buffer.Length,SocketFlags.None);         
                if (len == 0)
                {
                    break;
                }
                
                string recMes = System.Text.Encoding.ASCII.GetString(buffer, 0, len);
                dB = double.Parse(recMes);
                Debug.Log("客户端接收到的数据dB: " + dB);
            }
            catch { }
        }
    }    
    void OnDisable()
    {
        Debug.Log("begin OnDisable()");

        if (socketSend1.Connected)
        {
            try
            {
                socketSend1.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend1.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
        if (socketSend2.Connected)
        {
            try
            {
                socketSend2.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend2.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
 
        if (socketSend3.Connected)
        {
            try
            {
                socketSend3.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend3.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
          if (socketSend4.Connected)
        {
            try
            {
                socketSend4.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend4.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
          if (socketSend5.Connected)
        {
            try
            {
                socketSend5.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend5.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
             
          if (socketSend6.Connected)
        {
            try
            {
                socketSend6.Shutdown(SocketShutdown.Both);    //禁用Socket的发送和接收功能
                socketSend6.Close();                          //关闭Socket连接并释放所有相关资源
            }
            catch (Exception e)
            {
                print(e.Message);
            }
        }
       

        Debug.Log("end OnDisable()");
    }

    //用户界面
    void OnGUI()
    {
        GUI.color = Color.black;

        GUI.Label(new Rect(65, 70, 50, 20), "服务器ip地址");

        inputIp = GUI.TextField(new Rect(125, 70, 100, 20), inputIp, 20);

        GUI.Label(new Rect(65, 110, 50, 20), "服务器端口1");

        inputPort = GUI.TextField(new Rect(125, 110, 100, 20), inputPort, 20);
        
        GUI.Label(new Rect(65, 150, 50, 20), "服务器端口2");

        inputPort2 = GUI.TextField(new Rect(125, 150, 100, 20), inputPort2, 20);

        GUI.Label(new Rect(65, 190, 50, 20), "服务器端口3");

        inputPort3 = GUI.TextField(new Rect(125, 190, 100, 20), inputPort3, 20);

        GUI.Label(new Rect(65, 230, 50, 20), "服务器端口4");

        inputPort4 = GUI.TextField(new Rect(125, 230, 100, 20), inputPort4, 20);

        GUI.Label(new Rect(65, 270, 50, 20), "服务器端口5");

        inputPort5 = GUI.TextField(new Rect(125, 270, 100, 20), inputPort5, 20);

        GUI.Label(new Rect(65, 310, 50, 20), "服务器端口6");

        inputPort6 = GUI.TextField(new Rect(125, 310, 100, 20), inputPort6, 20);
  
        if (GUI.Button(new Rect(65,350, 60, 20), "开始连接"))
        {
            ClickConnect();
            s="True";
        }
    }
}

效果:

unity 3D能接收到采集卡采集的数据

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值