h8476532 2022-05-12 09:32 采纳率: 88.9%
浏览 91
已结题

机械臂数字挛生问题询问,请教vs和unity有关tcp socket send message的6个double变成一个byte array并传送到unity。

目前使用史陶比尔的c# soap当作server端,unity为client端,目前想要丢6个关节变数到unity,之后再分别丢到相对应的关节中,但本人机械专业,对程式不太了解,求指导。

下面是史陶比尔c# soap的程式码(server):

private void button55_Click_1(object sender, EventArgs e)
        {
            double c_px = 311.2;
            double c_py = 18.38;
            double c_pz = 137.44;
            double c_rx = 10;
            double c_ry = 179.93;
            double c_rz = 11.53; 

            //創建服務器
            Socket severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //ip地址和端口號綁定
            EndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
            //綁定服務器
            severSocket.Bind(endPoint);

            //發起監聽
            severSocket.Listen(1000);

            Console.WriteLine("server already opened!");
            Console.WriteLine("Waiting for Client connect!");
            Socket cilentSocket = severSocket.Accept();//這裡會阻塞,等待鏈接
            Console.WriteLine("client is connect.");
            while (true)
            {
                byte[] sendMsg = System.Text.Encoding.UTF8.GetBytes(string.Format(c_px.ToString())+',');
                byte[] sendMsg2 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_py.ToString())+',');
                byte[] sendMsg3 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_pz.ToString())+',');
                byte[] sendMsg4 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_rx.ToString())+',');
                byte[] sendMsg5 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_ry.ToString())+',');
                byte[] sendMsg6 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_rz.ToString()));
                int sendLength = cilentSocket.Send(sendMsg, SocketFlags.None);
                int sendLength2 = cilentSocket.Send(sendMsg2, SocketFlags.None);
                int sendLength3 = cilentSocket.Send(sendMsg3, SocketFlags.None);
                int sendLength4 = cilentSocket.Send(sendMsg4, SocketFlags.None);
                int sendLength5 = cilentSocket.Send(sendMsg5, SocketFlags.None);
                int sendLength6 = cilentSocket.Send(sendMsg6, SocketFlags.None);}
        }

有什么方法可以把上面的c_px, c_py, c_pz, c_rx, c_ry, c_rz的变成一个byte array并用tcp socket的方式传到unity? 我目前是一个一个传到unity。

感谢各位,顺便附上我的client程式码(我也是参考别的博主的)。

using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// 客戶端的控制腳本
/// </summary>
public class ClientSocketController : MonoBehaviour
{
    /// <summary>
    /// 鏈接對象
    /// </summary>
    public Socket clientSocket;
    /// <summary>
    /// ip地址
    /// </summary>
    public string ipAddress = "127.0.0.1";
    /// <summary>
    /// 端口號,這個是服務器開設的端口號
    /// </summary>
    public int portNumber = 10001;
    /// <summary>
    /// 鏈接間隔時間
    /// </summary>
    public float connectInterval = 1;
    /// <summary>
    /// 當前鏈接時間
    /// </summary>
    public float connectTime = 0;
    /// <summary>
    /// 鏈接次數
    /// </summary>
    public int connectCount = 0;
    /// <summary>
    /// 是否在連接中
    /// </summary>
    public bool isConnecting = false;
    void Start()
    {
        //調用開始連接
        ConnectedToServer();

    }
    /// <summary>
    /// 鏈接到服務器
    /// </summary>
    public void ConnectedToServer()
    {
        //鏈接次數增加
        connectCount++;
        isConnecting = true;
        Debug.Log("這是第" + connectCount + "次連接");
        //如果客戶端不為空
        if (clientSocket != null)
        {
            try
            {
                //斷開連接,釋放資源
                clientSocket.Disconnect(false);
                clientSocket.Close();
            }
            catch (System.Exception e)
            {
                Debug.Log(e.ToString());
            }

        }

        //創建新的鏈接(固定格式)
        clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        //設置端口號和ip地址
        EndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);

        //發起鏈接
        clientSocket.BeginConnect(endPoint, OnConnectCallBack, "");
    }
    /// <summary>
    /// 開始鏈接的回調
    /// </summary>
    /// <param name="ar"></param>
    public void OnConnectCallBack(IAsyncResult ar)
    {
        Debug.Log("連接完成!!!!");
        if (clientSocket.Connected)
        {
            //鏈接成功
            Debug.Log("連接成功");
            connectCount = 0;

            //開啟收消息
            ReceiveFormServer();
        }
        else
        {
            //鏈接失敗
            Debug.Log("連接失敗");
            //計時重置
            connectTime = 0;
        }
        isConnecting = false;
        //結束鏈接
        clientSocket.EndConnect(ar);
    }
    /// <summary>
    /// 向服務器發送字符串信息
    /// </summary>
    /// <param name="msg"></param>
    public void SendMessageToServer(string msg)
    {
        //將字符串轉成byte數組
        byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(msg);
        clientSocket.BeginSend(msgBytes, 0, msgBytes.Length, SocketFlags.None, SendMassageCallBack, 1);
    }
    /// <summary>
    /// 發送信息的回調
    /// </summary>
    /// <param name="ar"></param>
    public void SendMassageCallBack(IAsyncResult ar)
    {
        //關閉消息發送
        int length = clientSocket.EndSend(ar);
        Debug.Log("信息發送成功,發送的信息長度是:" + length);
    }
    /// <summary>
    /// 從服務器接收消息
    /// </summary>
    public void ReceiveFormServer()
    {
        //定義緩衝池
        byte[] buffer = new byte[512];
        clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveFormServerCallBack, buffer);
    }
    /// <summary>
    /// 信息接收方法的回調
    /// </summary>
    /// <param name="ar"></param>
    public void ReceiveFormServerCallBack(IAsyncResult ar)
    {
        //結束接收
        int length = clientSocket.EndReceive(ar);
        byte[] buffer = (byte[])ar.AsyncState;
        //將接收的東西轉為字符串
        string msg = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
        //Debug.Log(msg); //接收到的消息是 //"接收到的消息是"+

        //開啟下一次接收消息
        ReceiveFormServer();

        string robottarget = Convert.ToString(msg);
        robottarget = string.Join("", robottarget.Split());

        List<string> r_robottarget = robottarget.Split(',').ToList();
        //Debug.Log(robottarget);
        Debug.Log(r_robottarget);

    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            SendMessageToServer("哭阿!");
        }
        if (clientSocket != null && clientSocket.Connected == false)
        {
            //鏈接沒有成功
            //計時
            connectTime += Time.deltaTime;
            if (connectTime > connectInterval && isConnecting == false)//如果時間大於鏈接重置時間間隔且沒有鏈接
            {
                if (connectCount >= 7)
                {
                    Debug.Log("已經嘗試了7次,請檢查網絡連接");
                    clientSocket = null;
                }
                else
                {
                    //重連一次
                    ConnectedToServer();
                }

            }


        }
    }

    private void OnDestroy()
    {
        Debug.Log("物體被銷毀");
        //關閉客戶端
        clientSocket.Close();
    }
}
  • 写回答

3条回答 默认 最新

  • CoderZ1010 Unity领域优质创作者 2022-05-12 10:19
    关注

    服务端和客户端封装一个相同的数据结构JointData

    using System;
    
    /// <summary>
    /// 关节数据
    /// </summary>
    [Serializable]
    public class JointData
    {
        public double c_px;
        public double c_py;
        public double c_pz;
        public double c_rx;
        public double c_ry;
        public double c_rz;
    
        public JointData(double c_px, double c_py, double c_pz, double c_rx, double c_ry, double c_rz) 
        {
            this.c_px = c_px;
            this.c_py = c_py;
            this.c_pz = c_pz;
            this.c_rx = c_rx;
            this.c_ry = c_ry;
            this.c_rz = c_rz;
        }
    }
    

    使用一个序列化与反序列化工具,建议使用NewtonsoftJson,因为你这使用了双精度浮点数。
    服务端发送数据时New一个JointData类,使用构造函数将数据传入,然后将其序列化为字符串再发送

    JointData jointData = new JointData(311.2, 18.38, 137.44, 10, 179.93, 11.53);
    string data = JsonConvert.SerializeObject(jointData);
    

    客户端接收到data字符串数据后,将其反序列化为JointData类

    using UnityEngine;
    using Newtonsoft.Json;
    
    public class Example : MonoBehaviour
    {
        private void Start()
        {
            //接收到的数据
            string data = "";
            //反序列化
            JointData jointData = JsonConvert.DeserializeObject<JointData>(data);
        }
    }   
    

    反序列化后,jointData里包含各个关节数据,拿去赋值就可以了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月12日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么