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); }
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 } } }