Beispiel #1
0
        private byte[] GetBuffer(byte porg, int dataLength)
        {
            byte[] writeBuffer = new byte[BufferSize];

            writeBuffer[0] = 0x55;
            for (int i = 5; i < BufferSize; i++)
            {
                writeBuffer[i] = 0;
            }

            switch (porg)
            {
            case Main.porgRPS:
            case Main.porg1BS:
                headerRPS.CopyTo(writeBuffer, 1);
                break;

            case Main.porg4BS:
                header4BS.CopyTo(writeBuffer, 1);
                break;

            case Main.porgSYS_EX:
                headerSYS_EX.CopyTo(writeBuffer, 1);
                break;

            //case Main.porgReMan:
            //    headerReMan.CopyTo(writeBuffer, 1);
            //    break;
            default:
                headerCommand.CopyTo(writeBuffer, 1);
                if (optionalDataLength > 1)
                {
                    writeBuffer[2] = (byte)dataLength;
                }
                break;
            }
            writeBuffer[5] = CRC.Crc8(writeBuffer, 1, 4);
            return(writeBuffer);
        }
Beispiel #2
0
        public void TransmitReMan(object response, byte[] data)
        {
            int dataBufferLength = data.Length + remanOptionalLength;

            byte[] wBuffer = GetBuffer(Main.porgReMan, dataBufferLength);
            data.CopyTo(wBuffer, headerLength);

            // Optional data 0..3: Destination ID
            broadcast.CopyTo(wBuffer, headerLength + data.Length);

            // Optional data 4..7: Source ID
            // writeBuffer[4..7 + dataLength] = 0;
            wBuffer[headerLength + data.Length + 8] = 0xFF; // dBm
            wBuffer[headerLength + data.Length + 9] = 0;    // Send With Delay
            wBuffer[headerLength + data.Length + remanOptionalLength]
                = CRC.Crc8(wBuffer, headerLength, data.Length + remanOptionalLength);

            InterlockedTransmit(response, wBuffer,
                                headerLength + data.Length + remanOptionalLength + 1);

            return;
        }
Beispiel #3
0
        public void WirelessReceiveThread()
        {
            byte[]     readBuffer = new byte[maxTelegram];
            Boolean    gotHeader;
            Byte       porg = 0;
            int        dataLength;
            byte       optionalLength;
            PacketType packetType;
            byte       crc8h;
            byte       crc8d;

            while (true)
            {
                while (wirelessPort.BytesToRead == 0)
                {
                    Thread.Sleep(197);
                }
                do
                {
                    byte[] header = new byte[4];
                    gotHeader = false;
                    do
                    {
                        wirelessPort.Read(readBuffer, 0, 1);
                    }while (readBuffer[0] != SYNCBYTE);

                    wirelessPort.Read(header, 0, 4);

                    dataLength     = header[0] << 8 | header[1];
                    optionalLength = header[2];
                    packetType     = (PacketType)header[3];

                    wirelessPort.Read(readBuffer, 0, 1);
                    crc8h = readBuffer[0];

                    if (dataLength + optionalLength + 1 >= maxTelegram)
                    {
                        DebugPrint("Large data!\r\n");
                        continue;
                    }
                    gotHeader = crc8h == CRC.Crc8(header);
                }while (!gotHeader);

                //DebugPrint("Got Header...\r\n"); //?????????????????

                for (int i = 0; i < dataLength; i++)
                {
                    wirelessPort.Read(readBuffer, i, 1);
                }

                if (packetType == PacketType.Radio || packetType == PacketType.RadioSubTel)
                {
                    // Check CRC
                    if (optionalLength > 0)
                    {
                        wirelessPort.Read(readBuffer, dataLength, optionalLength);
                    }
                    wirelessPort.Read(readBuffer, dataLength + optionalLength, 1);
                    crc8d = readBuffer[dataLength + optionalLength];

                    if (crc8d != CRC.Crc8(readBuffer, dataLength + optionalLength))
                    {
                        DebugPrint("CRC error!\r\n");
                        return;
                    }

                    porg = readBuffer[0];
                    if (porg == porgSYS_EX)
                    {
                        ProcessSYSEX(readBuffer, dataLength);
                    }
                    else if (porg == porgRPS) // RPS or 1BS
                    {
                        //////////////////////////////////////////////////
                        // Only for DEBUG message
                        string from = Sx.XnString(ref readBuffer, 2, 4);
                        string to   = Sx.XnString(ref readBuffer, Tx.headerRPS[1] + 1, 4);
                        string stat = Sx.X2String(readBuffer[6]);
                        DebugPrint("RPS " + from + " > " + to + " " + stat + "\r\n");
                        //////////////////////////////////////////////////
                        if (CompareID(myID, readBuffer, Tx.headerRPS[1] + 1))
                        {
                            ProcessRPSTelegram(readBuffer, dataLength);
                        }
                        else
                        {
                            string my = Sx.XnString(ref myID, 0);
                            DebugPrint("CompareID error: " + my + "\r\n");
                        }
                    }
                    else if (porg == porg4BS)
                    {
                        // Process4BSTelegram(readBuffer, dataSize);
                        // DebugPrint("4BS Received...\r\n");
                    }
                    else
                    {
                        DebugPrint("Invalid porg: " + porg + "\r\n");
                        return;
                    }
                }
                else if (packetType == PacketType.Response)
                {
                    ProcessResponse(readBuffer, dataLength);
                }
            }
        }