public void ReverseBitsTest() { Packet testPacket = new Packet(); byte res1 = testPacket.reverseBits(0xFF); // easy byte res2 = testPacket.reverseBits(0x0A); byte res3 = testPacket.reverseBits(0x5F); Assert.AreEqual(0xFF,res1); Assert.AreEqual(0x50,res2); Assert.AreEqual(0xFA, res3); }
public List<Block> ExtractFullBlocks(Packet pkt) { List<int> startPos = realStartPositions(findStartPatterninPacket(pkt)); List<Block> blocks = new List<Block>(); if (startPos.Count <= 1) return blocks; // if there were no start positions found give back an empty block for (int i = 0; i < startPos.Count - 1; i++) { byte[] bufTemp = new byte[26]; // 204 bits total to store the entire block. //blocks.Add(new Block( } return blocks; }
public void parseSingleChanTest() { UInt16 testVal = 1000; byte[] buf = new byte[Packet.MaxLength]; for (int i = 0; i < 14; i++) { buf[2*i] = (byte)(testVal & 0xFF); buf[2*i+1 ] = (byte)(testVal >> 8); } Packet testPacket = new Packet(buf); PacketHandler pkthandleTest = new PacketHandler(); List<int> results = pkthandleTest.parseSingleChan(testPacket); // Assert.AreEqual(2048-testVal, results[3]); }
public Packet( byte [] buffer) { byte info; this.rawBuffer = new byte [maxLength]; this.dataBuffer = new byte[infoIndex]; // reverses each byte in the packet contents so the bytes are in the right order. /* for (int i = 0; i < infoIndex; i++) { this.dataBuffer[i] = reverseBits(buffer[i]); } */ info = buffer[infoIndex]; this.isStimulating = ((info) & (0x80)) != 0; // if right most bit is set to 1 then its stimulating this.count = (byte)( 0x7F & info); // adcVals = ParsePacket(); lastPacket = this; }
public void ParseNewPacket(Packet receivedPacket) { List<int> parsedValues; if (lastPacket != null) { parsedValues = parseSixteenChan(receivedPacket); // Scope.CurrentScope.AddRawADCtoQueue(parsedValues); } else { parsedValues = new List<int>(); } lastPacket = receivedPacket; if (processingDone != null) { processingDone(this, new ProcessingDoneEventArgs(parsedValues.ToArray(), lastPacket.IsStimulating)); } }
public int calculateMissedPacketsBetween(Packet LastPacket) { return calculateMissedPacketsBetween(LastPacket.Count); }
private void UpdateWirelessStats(Packet receivedPacket) { // missedPackets = receivedPacket.calculateMissedPacketsBetween(lastPacket); missedPackets = receivedPacket.Count - lastPacket.Count - 1; if (receivedPacket.Count <= lastPacket.Count) // check for overflow since count only goes to 128; { if (missedPackets == 0) { // throw new InvalidOperationException(); } missedPackets += 128; } if (missedPackets == 0) { ++WirelessStats.Instance.NumSuccessRxPackets; } else { WirelessStats.Instance.NumDroppedPackets += missedPackets; ++WirelessStats.Instance.NumSuccessRxPackets; // we still need to count the last packet we received as a successful packet } }
public void processQueue() { while (!bcWorkQueue.IsCompleted || disposed) { byte[] buffer = null; try { buffer = bcWorkQueue.Take(); } catch (InvalidOperationException) { } if (buffer != null) { Packet rxPacket = new Packet(buffer); ParseNewPacket(rxPacket); } } }
public List<int> parseSixteenChan(Packet receivedPacket) { string lineToLog = ""; List<int> parsedValues = new List<int>(); // List<Block> validBlocks = SixteenChanParser.ParseNewPacket(receivedPacket); // logger.LogLine(receivedPacket.Count); UpdateWirelessStats(receivedPacket); /* foreach (Block block in validBlocks) { List<int> values = block.Parse(); parsedValues.Add(values[currentChannel]); lineToLog = ""; foreach (int val in values) { lineToLog += " " + Convert.ToString(val); } lineToLog += " " + Convert.ToString(receivedPacket.Count); // logger.LogLine(lineToLog); } */ return parsedValues; }
public List<int> parseSingleChan(Packet receivedPacket) { List<int> parsedValues = new List<int>(); int tempValue; for (int i = 0; i < parsedValues.Count; i++) { tempValue = receivedPacket.DataBuffer[2 * i + 1] + (receivedPacket.DataBuffer[2 * i] << 8); /* if (tempValue >= 512) { tempValue = -tempValue; } * */ parsedValues[i] = 2048-tempValue; // This is because a value of 2048 is 0 in differential recording mode logger.LogLine(parsedValues[i]); } return parsedValues; }
public List<int> findStartPatterninPacket(Packet rxPacket) { return findStartPatterninPacket(rxPacket.DataBuffer); }
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 } } }
public Block(Packet rxPacket) : this((new List<byte>( rxPacket.DataBuffer)), 0) { }
public void parseThreePackets() { byte[] buf1 = new byte[32]; byte[] buf2 = new byte[32]; byte[] buf3 = new byte[32]; // set counts so they're in order buf1[30]=13; buf2[30]=14; buf3[30]=15; buf1[8]=0x50; buf1[9]=0x55; buf1[15] = 0x21; buf1[16] = 0x15; buf1[17] = 0x12; // 204 bits in between buf2[4]=0x55; buf2[5]=0x05; buf2[29] = 0x50; buf3[0] = 0x55; buf3[25] = 0x55; buf3[26] = 0x05; Packet pkt1 = new Packet(buf1); Packet pkt2 = new Packet(buf2); Packet pkt3 = new Packet(buf3); Parser testParser = new Parser(); List<Block> blocks1 = testParser.ParseNewPacket(pkt1); List<Block> blocks2 = testParser.ParseNewPacket(pkt2); List<Block> blocks3 = testParser.ParseNewPacket(pkt3); Assert.AreEqual(0, blocks1.Count); Assert.AreEqual(1, blocks2.Count); Assert.AreEqual(2, blocks3.Count); Assert.AreEqual(0x0A, blocks2[0].Buffer[0]); }