Exemplo n.º 1
0
        private int GetParamSize(ExternalEventType exttype)
        {
            switch (exttype)
            {
            case ExternalEventType.EXTERNAL_TAP_POWER:
                return(1);

            default:
                return(0);
            }
        }
Exemplo n.º 2
0
        private void execButton_Click(object sender, EventArgs e)
        {
            if (portStateButton.Text == "Connect")
            {
                return;
            }

            int cmd = commandListBox.SelectedIndex;

            if (cmd >= 0)
            {
                ExternalEventType exttype = (ExternalEventType)cmd;
                int    paramSize          = GetParamSize(exttype);
                int    size = paramSize + 9;
                byte[] data = new byte[size];

                data[0] = 0xEE;
                data[1] = 0x00;
                data[2] = (byte)cmd;

                switch (exttype)
                {
                case ExternalEventType.EXTERNAL_TAP_POWER:
                {
                    int param = paramListBox.SelectedIndex;
                    if (param >= 0)
                    {
                        data[3] = (byte)param;
                    }
                }
                break;
                }

                byte[] content = new byte[2 + paramSize];
                Array.Copy(data, 1, content, 0, 2 + paramSize);

                ushort crc = Crc16(content);
                crc = SwapBytes(crc);

                byte[] crc16 = BitConverter.GetBytes(crc);
                Array.Copy(crc16, 0, data, size - 6, 2);

                data[size - 4] = 0xFF;
                data[size - 3] = 0xFC;
                data[size - 2] = 0xFF;
                data[size - 1] = 0xFF;

                comPort.Write(data, 0, data.Length);
            }
        }
Exemplo n.º 3
0
        private void ProcessData(byte[] data)
        {
            if (bufSize > 0)
            {
                int size = data.Length;

                if (size > maxBufSize - bufSize)
                {
                    size = maxBufSize - bufSize;
                }

                Array.Copy(data, 0, buffer, bufSize, size);
                bufSize += size;
            }
            else
            {
                for (int i = 0; i < data.Length; ++i)
                {
                    if (data[i] == 0xEE)
                    {
                        int size = data.Length - i;

                        if (size > maxBufSize - bufSize)
                        {
                            size = maxBufSize - bufSize;
                        }

                        Array.Copy(data, i, buffer, bufSize, size);
                        bufSize += size;

                        break;
                    }
                }
            }

            if (bufSize >= minDataLen && buffer[0] == 0xEE)
            {
                ushort cmd = BitConverter.ToUInt16(buffer, 1);

                cmd = SwapBytes(cmd);

                if (cmd >= 0)
                {
                    ExternalEventType exttype = (ExternalEventType)cmd;

                    byte[] content = new byte[3];
                    Array.Copy(buffer, 1, content, 0, 3);
                    ushort crc = Crc16(content);

                    ushort crc16 = BitConverter.ToUInt16(buffer, 4);
                    crc16 = SwapBytes(crc16);

                    if (crc == crc16)
                    {
                        int param = (int)buffer[3];

                        this.recvTextBox.Text += commandListBox.Items[cmd].ToString() + " ";
                        switch (exttype)
                        {
                        case ExternalEventType.EXTERNAL_TAP_POWER:
                        case ExternalEventType.EXTERNAL_TAP_AUTO:
                        case ExternalEventType.EXTERNAL_TAP_COOL:
                        case ExternalEventType.EXTERNAL_TAP_DRY:
                        case ExternalEventType.EXTERNAL_TAP_FAN:
                        case ExternalEventType.EXTERNAL_TAP_HEAT:
                        case ExternalEventType.EXTERNAL_TAP_WIND_HIGH:
                        case ExternalEventType.EXTERNAL_TAP_WIND_MID:
                        case ExternalEventType.EXTERNAL_TAP_WIND_LOW:
                        case ExternalEventType.EXTERNAL_TAP_WIND_AUTO:
                        case ExternalEventType.EXTERNAL_TAP_AIRCONDITIONER:
                        case ExternalEventType.EXTERNAL_TAP_MULTIMEDIA:
                        {
                            this.recvTextBox.Text += paramListBox.Items[param].ToString() + "\r\n";
                        }
                        break;

                        case ExternalEventType.EXTERNAL_DRAG_TEMP_UP:
                        case ExternalEventType.EXTERNAL_DRAG_TEMP_DOWN:
                        case ExternalEventType.EXTERNAL_TAP_HOME_PAGE_UP:
                        case ExternalEventType.EXTERNAL_TAP_HOME_PAGE_DOWN:
                        {
                            this.recvTextBox.Text += param.ToString() + "\r\n";
                        }
                        break;
                        }
                        this.recvTextBox.SelectionStart = this.recvTextBox.TextLength;
                        this.recvTextBox.ScrollToCaret();
                    }
                }
                bufSize = 0;
            }
            //this.recvTextBox.Text += System.Text.Encoding.UTF8.GetString(data);
        }