Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        try
        {
            switch (Step)
            {
            case 0:
            {
                if (Timer.Match())
                {
                    // Create TCP client and asynchronously connect to server
                    Client      = new TcpClient();
                    ConnectTask = Client.ConnectAsync(IPAddress.Parse(address), port);
                    Debug.Log(string.Format("Connecting on {0} : {1}", address, port));
                    Step++;
                }
                break;
            }

            case 1:
            {
                if (ConnectTask.Status == TaskStatus.RanToCompletion)
                {
                    Debug.Log("Connected!");
                    Stream    = Client.GetStream();
                    LastColor = ColorPicker.Color;
                    Timer.Start(100);
                    Step++;
                }
                else if (ConnectTask.Exception != null)
                {
                    throw ConnectTask.Exception.InnerException;
                }
                break;
            }

            case 2:
            {
                if ((LastColor != ColorPicker.Color) && Timer.Match())
                {
                    LastColor = ColorPicker.Color;
                    Timer.Start(100);

                    int     startLed = 0;
                    int     stopLed  = 0;
                    Color32 color    = LastColor;

                    // Create packet
                    int n = 0;
                    Packet[n++] = (byte)Preample;
                    Packet[n++] = (byte)(Preample >> 8);
                    Packet[n++] = (byte)(Preample >> 16);
                    Packet[n++] = (byte)(Preample >> 24);

                    int packetLen = 8;
                    Packet[n++] = (byte)packetLen;
                    Packet[n++] = (byte)(packetLen >> 8);
                    int dataOffset = n;
                    Packet[n++] = (byte)Command_t.SET_LEDS;
                    Packet[n++] = (byte)startLed;
                    Packet[n++] = (byte)(startLed >> 8);
                    Packet[n++] = (byte)stopLed;
                    Packet[n++] = (byte)(stopLed >> 8);
                    Packet[n++] = color.r;
                    Packet[n++] = color.g;
                    Packet[n++] = color.b;

                    ushort crc = Crc.CRC16(Packet, dataOffset, n - dataOffset, 0);
                    Packet[n++] = (byte)crc;
                    Packet[n++] = (byte)(crc >> 8);

                    // Send packet
                    Debug.Log(string.Format("Sending color: {0}", color));
                    SendTask = Stream.WriteAsync(Packet, 0, n);
                    Step++;
                }

                break;
            }

            case 3:
            {
                if (SendTask.Status == TaskStatus.RanToCompletion)
                {
                    Debug.Log("Sended!");
                    Step--;
                }
                else if (SendTask.Exception != null)
                {
                    throw SendTask.Exception.InnerException;
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }
        catch (Exception ex)
        {
            // if something went wrong - close everything and try to connect again
            if (Stream != null)
            {
                Stream.Close();
                Stream = null;
            }

            if (Client != null)
            {
                Client.Close();
                Client = null;
            }
            Timer.Start(5000);
            Step = 0;
            Debug.Log(ex.Message);
        }
    }