예제 #1
0
        public void CombineBlockTest()
        {
            byte[] buf1 = new byte[26];

            buf1[0] = 0xAA;
            buf1[1] = 0xA1;
            buf1[2] = 0xF5;
            buf1[3] = 0x01;
            buf1[4] = 0x55;
            buf1[24] = 0x24;
            buf1[25] = 0x1A;

            byte[] buf2 = new byte[12];

            buf2[8] = 0x02;
            buf2[9] = 0xAA;
            buf2[0] = 0x53;
            buf2[2] = 0x25;
            buf2[11] = 0x15;

            Block testBlockIn = new Block(buf2.ToList(), 5);

            Block combinedBlock = Block.combineBlocks(testBlockIn, buf1);

            Assert.AreEqual(12 + 26, combinedBlock.Buffer.Count);
            Assert.AreEqual(5, combinedBlock.Offset);
            Assert.AreEqual(0x1a, combinedBlock.Buffer[37]);
        }
예제 #2
0
        public void Convert12bitTo10TestNegValues()
        {
            Block testBlock = new Block();

            int val1 = testBlock.Convert12bitToAdcVal(4092);
            int val2 = testBlock.Convert12bitToAdcVal(4094);

            Assert.AreEqual(-511, val1);
            Assert.AreEqual(-511, val2);
        }
예제 #3
0
        public static Block combineBlocks(Block block1, IList<byte> buffer)
        {
            var retBlock = new Block();
            retBlock.buffer.AddRange(block1.buffer);
            retBlock.buffer.AddRange(buffer);

            retBlock.Offset = block1.offset;

            return retBlock;
        }
예제 #4
0
        public void Convert12bitTo10Test()
        {
            Block testBlock = new Block();

            int val1 = testBlock.Convert12bitToAdcVal(4000);
            int val2 = testBlock.Convert12bitToAdcVal(2976);
            int val3 = testBlock.Convert12bitToAdcVal(3488);
            int val4 = testBlock.Convert12bitToAdcVal(3872);

            Assert.AreEqual(-488, val1);
            Assert.AreEqual(-232, val2);
            Assert.AreEqual(-360, val3);

            Assert.AreEqual(-456, val4);
        }
예제 #5
0
        public void ParseTestEasy()
        {
            byte[] buf1 = new byte[30];

            buf1[0]=0xAA;
            buf1[1]=0xA1;
            buf1[2] = 0xF5;
            buf1[3] = 0x01;
            buf1[4] = 0x55;
            buf1[24] = 0x24;
            buf1[25] = 0x1A;
            Block testBlock = new Block( buf1.ToList(), 0);
            List<int> result = testBlock.Parse();

            Assert.AreEqual(17, result.Count);
            Assert.AreEqual(testBlock.Convert12bitToAdcVal(0xAAA), result[0]);
            Assert.AreEqual(testBlock.Convert12bitToAdcVal(0x1f5), result[1]);
            Assert.AreEqual(testBlock.Convert12bitToAdcVal(0x015), result[2]);
            Assert.AreEqual(testBlock.Convert12bitToAdcVal(0x241), result[16]);
        }
예제 #6
0
        public void RemoveBitsTest()
        {
            byte []  buf1 = new byte[32];

            buf1[8]=0x02;
            buf1[9]=0xAA;
            buf1[15] = 0x53;
            buf1[16] = 0x25;
            buf1[17] = 0x15;

            Block testBlock = new Block(buf1.ToList<byte>(), 0);

            testBlock.removeBits(9*8+5);

            int mask = ((1 << 6) - 1);
            Assert.AreEqual(0xAA & mask, testBlock.Buffer[0] & mask);
            Assert.AreEqual(buf1[16], testBlock.Buffer[16 - 9]);
            Assert.AreEqual(5, testBlock.Offset);
        }
예제 #7
0
 public List<int> findStartPatterninBlock(Block toBeSearched)
 {
     return findStartPatterninPacket(toBeSearched.Buffer);
 }
예제 #8
0
        public List<Block> ParseNewPacket(Packet rxPacket)
        {
            lock (parseLock) // argghhh!!!!
            {
                List<Block> validBlocks = new List<Block>();
                Block blockforProcessing = new Block(rxPacket);

                if (rxPacket.calculateMissedPacketsBetween(lastPacketCount) == 0)
                {
                    // we need to combine two packets just in case the start pattern was on the boundary so the search routine can find it.
                    blockforProcessing = Block.combineBlocks(leftOverBlock, rxPacket.DataBuffer);
                }
                else
                {
                    // Discard left over block from the last packet if there are missing packets in between since they wont match up anyways
                    blockforProcessing = new Block(rxPacket);
                }

                lastPacketCount = rxPacket.Count;
                List<int> startPosFound = findStartPatterninBlock(blockforProcessing);

                if (startPosFound.Count == 0) // means no start bits were found. should rarely come here. Need to test this more
                {
                    leftOverBlock = new Block(); // set it to an empty block so we start from scratch next time
                    return validBlocks; // returns 0 blocks
                }

                List<int> realStartPosFound = realStartPositions(startPosFound);

                if (realStartPosFound.Count > 0) // means we found some legit start patterns
                {

                    validBlocks = GenerateBlocks(blockforProcessing, realStartPosFound);
                    leftOverBlock = new Block(blockforProcessing);

                    leftOverBlock.removeBits(realStartPosFound[realStartPosFound.Count - 1]); // discard the part of the packet we already processed
                    return validBlocks;
                }
                else // most likely reason for coming here is because it found startpatterns two bits apart and cant figure out the real block to process
                {
                    if (blockforProcessing.Buffer.Count > 30 * 30) // after 10 packets
                    {
                        // for some reason sometimes it never finds a valid start pattern so this tells it to just start over and discard everything from before.
                        leftOverBlock = new Block();

                    }
                    else
                    {
                        leftOverBlock = blockforProcessing; // keeps on adding packets to leftoverblock so we can gather data to eventually make a decision
                    }
                    return validBlocks; // returns 0 blocks
                }
            }
        }
예제 #9
0
        public List<Block> GenerateBlocks(Block blockforProcessing, List<int> realStartPosFound)
        {
            List<Block> Blocks = new List<Block>();

            for (int i = 0; i < realStartPosFound.Count-1; i++) // we ignore the last value on purpose since its not the beginning of a valid block
            {
                Blocks.Add(new Block(blockforProcessing.Buffer,realStartPosFound[i]));

            }

            return Blocks;
        }
예제 #10
0
 public static Block combineBlocks(Block block1, Block block2)
 {
     return combineBlocks(block1,block2.Buffer);
 }
예제 #11
0
 public Block(Block block1)
 {
     // copies values
     this.buffer = new List<byte>(block1.buffer);
     this.offset = block1.offset;
 }