示例#1
0
        protected override object ReceiveThreadProcess(object o)
        {
            Byte[] bytes     = new Byte[this.BufferSize];
            int    byteIndex = 0;

            while (this.ProgramRunning && this.Status == SocketStatus.SOCKET_STATUS_CONNECTED)
            {
                try
                {
                    byte b = ReceiveQueue.Dequeue();

                    // If find byte = CR
                    if (b == 13)
                    {
                        // check the next byte may also be 13 in which this one maybe the checksum
                        if (ReceiveQueue.Peek() == 13)
                        {
                            b = ReceiveQueue.Dequeue();
                            bytes[byteIndex] = b;
                            byteIndex++;
                        }

                        // Copy bytes to new array with length of packet and ignoring the CR.
                        Byte[] copiedBytes = new Byte[byteIndex];
                        Array.Copy(bytes, copiedBytes, byteIndex);

                        byteIndex = 0;

                        int chk = 0;

                        for (int i = 1; i < (copiedBytes.Length - 1); i++)
                        {
                            chk = chk ^ copiedBytes[i];
                        }
#if DEBUG
                        CrestronConsole.Print("NEC Rx: ");
                        Tools.PrintBytes(copiedBytes, copiedBytes.Length, false);
#endif
                        if (copiedBytes.Length > 0 && chk == (int)copiedBytes.Last())
                        {
                            if (this.ReceivedData != null)
                            {
                                this.ReceivedData(this, copiedBytes);
                            }
                        }
                        else if (copiedBytes.Length > 0)
                        {
                            ErrorLog.Warn("NEC Display Rx: \"{0}\"", Tools.GetBytesAsReadableString(copiedBytes, copiedBytes.Length, true));
                            ErrorLog.Warn("NEC Display Rx - Checksum Error, chk = 0x{0}, byteIndex = {1}, copiedBytes.Length = {2}",
                                          chk.ToString("X2"), byteIndex, copiedBytes.Length);
#if DEBUG
                            CrestronConsole.PrintLine("NEC Display Rx - Checksum Error, chk = 0x{0}, byteIndex = {1}, copiedBytes.Length = {2}",
                                                      chk.ToString("X2"), byteIndex, copiedBytes.Length);

                            CrestronConsole.PrintLine("rxQueue.Peek() = {0}", ReceiveQueue.Peek());
#endif
                        }

                        if (ReceiveQueue.IsEmpty)
                        {
                            break;
                        }
                    }
                    else
                    {
                        bytes[byteIndex] = b;
                        byteIndex++;
                    }

                    CrestronEnvironment.AllowOtherAppsToRun();
                    Thread.Sleep(0);
                }
                catch (Exception e)
                {
                    ErrorLog.Error("{0} - Error in ReceiveThreadProcess, {1}", this.GetType().Name, e.Message);
                    break;
                }
            }

            return(null);
        }