继电器

作者: Moment__格调 | 来源:发表于2019-11-25 15:28 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using UnityEngine.UI;
    using System.Text;
    using System.Threading;
    
    public class ClientController : MonoBehaviour
    {
        private static ClientController _singleton;
        public static ClientController Singleton
        {
            get
            {
                if (_singleton == null)
                {
                    _singleton = FindObjectOfType<ClientController>();
                }
                return _singleton;
            }
        }
        private const int BUFFER_SIZE = 1024;
        private byte[] buffer;
        public string host ;
        public int port ;
    
        private Socket socket;
       // public Text text;
        public bool isShow = true;
        //public string revText;
        //public Text ipText_Client;
        //public InputField inputField;
        private void Awake()
        {
            //text.gameObject.SetActive(false);
            //text.gameObject.GetComponent<RectTransform>().localScale = Vector3.zero;
        }
        // Use this for initialization
        void Start()
        {
            //socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Connect();
            // ipText_Client.text = host;
        }
        //private void Connect()
        //{
        //    try
        //    {
        //        Debug.Log(0000);
        //        socket.Connect(host, port);
        //    }
        //    catch (Exception e)
        //    {
        //        //积极拒绝
        //        print(e.Message);
        //        if (isShow)
        //        {
        //            text.gameObject.SetActive(true);
        //            text.text = e.Message;
        //            text.gameObject.GetComponent<RectTransform>().localScale = Vector3.one;
        //        }
        //        else
        //        {
        //            return;
        //        }
        //    }
    
        //    if (socket.Connected)
        //    {
        //        text.gameObject.SetActive(false);
        //        text.gameObject.GetComponent<RectTransform>().localScale = Vector2.zero;
        //        print("Connected");
        //        //  Receive();
        //    }
        //    else
        //    {
        //        print("Connect fail");
        //        Invoke("Connect", 3);
        //    }
        //}
        ////异步接收
        //private void Receive()
        //{
        //    if (!socket.Connected)
        //        return;
    
        //    buffer = new byte[BUFFER_SIZE];
    
        //    try
        //    {
        //        socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, new AsyncCallback(Receive_Callback), socket);
        //    }
        //    catch (Exception e)
        //    {
        //        print(e.Message);
        //    }
        //}
    
        //private void Receive_Callback(IAsyncResult ar)
        //{
        //    if (!socket.Connected)
        //    {
        //        return;
        //    }
    
        //    int read = socket.EndReceive(ar);
    
        //    if (read > 0)
        //    {
        //        print(Encoding.ASCII.GetString(buffer));
        //        revText = Encoding.ASCII.GetString(buffer);
        //        Debug.Log("接收到服务器端信息");
        //        //  text.text = Encoding.UTF8.GetString(buffer);
        //        Receive();
        //    }
        //}
        public void Update()
        {
            //if (revText != null && revText != "")
            //{
            //    text.text = revText;
            //    //重置视频进度
            //   // VideoClient.instance.SetTime();
            //    revText = string.Empty;
            //}
            //else
            //{
            //    return;
            //}
        }
    
        /// <summary>
        /// 短连接,最后调用Close释放资源
        /// </summary>
        /// <param name="input"></param>
        //public static void shotlink(string input)
        //{
        //    //设定服务器IP地址  
        //    IPAddress ip = IPAddress.Parse("127.0.0.1");
        //    Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //    try
        //    {
        //        clientSocket.Connect(new IPEndPoint(ip, 2000)); //配置服务器IP与端口  
        //        Console.WriteLine("连接服务器成功");
        //    }
        //    catch
        //    {
        //        Console.WriteLine("连接服务器失败,请按回车键退出!");
        //        Console.ReadLine();
        //        return;
        //    }
    
        //    string sendMessage = "你好";//发送到服务端的内容
        //    clientSocket.Send(Encoding.UTF8.GetBytes(sendMessage));//向服务器发送数据,需要发送中文则需要使用Encoding.UTF8.GetBytes(),否则会乱码
        //    Console.WriteLine("向服务器发送消息:" + sendMessage);
    
        //    ////接受从服务器返回的信息
        //    //string recvStr = "";
        //    //byte[] recvBytes = new byte[1024];
        //    //int bytes;
        //    //bytes = clientSocket.Receive(recvBytes, recvBytes.Length, 0);    //从服务器端接受返回信息 
        //    //recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
        //    //Console.WriteLine("服务端发来消息:{0}", recvStr);//回显服务器的返回信息
    
        //    //每次完成通信后,关闭连接并释放资源
        //    clientSocket.Close();
        //    Console.ReadLine();
        //}
    
    
        //public void Send(string message)
        //{
        //    if (!socket.Connected)
        //        return;
        //    //Debug.Log(inputField.text);
        //    //message = inputField.text;
        //    byte[] msg = Encoding.ASCII.GetBytes(message);
        //    socket.Send(msg);
        //}
    
       
        //public void SetText()
        //{
        //    isShow = false;
        //    if (text.gameObject.GetComponent<RectTransform>().localScale == Vector3.one)
        //    {
        //        text.gameObject.GetComponent<RectTransform>().localScale = Vector3.zero;
        //    }
        //    else
        //    {
        //        return;
        //    }
        //}
    
        public void ShotLine(string command)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                // clientSocket.Connect(new IPEndPoint(host, port)); //配置服务器IP与端口  
                socket.Connect(host, port);
                Console.WriteLine("连接服务器成功");
            }
            catch
            {
                Console.WriteLine("连接服务器失败,请按回车键退出!");
                Console.ReadLine();
                return;
            }
    
            if (!socket.Connected)
                return;
    
            string stramin16 = "61 64 6D 69 6E 0D 0A";
            string[] strsplitadmin = stramin16.Split(' ');
            byte[] data = new byte[strsplitadmin.Length];
            for (int i = 0; i < strsplitadmin.Length; i++)
            {
                data[i] = (byte)Convert.ToInt32(strsplitadmin[i], 16);
            }
    
    
            socket.Send(data);
    
            //  Thread.Sleep(3000);
            // stramin16 = "55 AA 00 03 00 01 01 06";
            string stramin16cmd = command;
            string[] strsplitadmincmd = stramin16cmd.Split(' ');
            byte[] datacmd = new byte[strsplitadmincmd.Length];
            for (int i = 0; i < strsplitadmincmd.Length; i++)
            {
                datacmd[i] = (byte)Convert.ToInt32(strsplitadmincmd[i], 16);
            }
    
            Thread.Sleep(100);
    
            socket.Send(datacmd);
            // socket.EndSend(data);
            socket.Close();
    
        }
    
    
        //public byte[] ConvertHexStrToByteArray(string hexStr)
        //{
        //    string[] array = new string[hexStr.Length / 2];
        //    byte[] bytes = new byte[array.Length];
        //    for (int i = 0; i < hexStr.Length; i++)
        //    {
        //        array[i / 2] += hexStr[i];
        //    }
        //    for (int i = 0; i < array.Length; i++)
        //    {
        //        bytes[i] = Convert.ToByte(strArray[i], 16);
        //    }
        //    return bytes;
    
        //}
        //private static byte[] HexStrTobyte(string hexString)
        //{
        //    hexString = hexString.Replace(" ", "");
        //    if ((hexString.Length % 2) != 0)
        //        hexString += " ";
        //    byte[] returnBytes = new byte[hexString.Length / 2];
        //    for (int i = 0; i < returnBytes.Length; i++)
        //        returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
        //    return returnBytes;
        //}
    
        //private void OnDisable()
        //{
        //    if (socket.Connected)
        //    {
        //        socket.Shutdown(SocketShutdown.Both);
        //        socket.Close();
        //    }
        //}
    }```

    相关文章

      网友评论

          本文标题:继电器

          本文链接:https://www.haomeiwen.com/subject/sbcjwctx.html