public static string Dump(DataPacketHeader hdrDPK, bool /*labelled?*/ isLbl = false) { string /*text (returned)*/ txt = (isLbl ? "DataPacketHeader" : string.Empty) + "{"; txt += (isLbl ? "LineID " : string.Empty) + hdrDPK.LineID.ToString(); txt += ", " + (isLbl? "Start/EndNumOfDetectors" : string.Empty) + "[" + hdrDPK.StartNumOfDetectors.ToString() + "," + hdrDPK.EndNumOfDetectors.ToString() + "]"; txt += "," + XRayInfoIDStruct.Dump(hdrDPK.EnergyAndPulsewidth, isLbl); txt += "," + (isLbl ? "NumBytesPerPixel " : string.Empty) + hdrDPK.NumBytesPerPixel.ToString(); txt += "}"; return(txt); }
public DataPacketHeader Deserialize(byte[] buffer) { byte LineIDbyte3 = buffer[3]; byte LineIDbyte2 = buffer[2]; byte LineIDbyte1 = buffer[1]; byte LineIDbyte0 = buffer[0]; //swap LineID bytes network to host buffer[0] = LineIDbyte3; buffer[1] = LineIDbyte2; buffer[2] = LineIDbyte1; buffer[3] = LineIDbyte0; //byte swap network to host //only handle start and end of detector fields. for (int i = 4; i < 8; i += 2) { byte byte1 = buffer[i]; byte byte2 = buffer[i + 1]; buffer[i] = byte2; buffer[i + 1] = byte1; } //chData is 3bytes wide per channel, swap byte 1 and 3 for (int i = 20; i < buffer.Length; i += 3) { byte byte1 = buffer[i]; byte byte3 = buffer[i + 2]; buffer[i] = byte3; buffer[i + 2] = byte1; } GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); DataPacketHeader header = (DataPacketHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DataPacketHeader)); handle.Free(); return(header); }
public static string Dump(DataPacketHeader hdrDPK, bool /*labelled?*/ isLbl = false) { string /*text (returned)*/ txt = (isLbl ? "DataPacketHeader" : string.Empty) + "{"; txt += (isLbl ? "LineID " : string.Empty) + hdrDPK.LineID.ToString(); txt += ", " + (isLbl? "Start/EndNumOfDetectors" : string.Empty) + "[" + hdrDPK.StartNumOfDetectors.ToString() + "," + hdrDPK.EndNumOfDetectors.ToString() + "]"; txt += "," + XRayInfoIDStruct.Dump(hdrDPK.EnergyAndPulsewidth, isLbl); txt += "," + (isLbl ? "NumBytesPerPixel " : string.Empty) + hdrDPK.NumBytesPerPixel.ToString(); txt += "}"; return txt; }
private void DataHarvest(byte[] rawData, ref Dictionary<uint, AllLineData> stagedLines, int ncbIndex) { #if false ushort StartNumOfDetectors = BitConverter.ToUInt16(rawData, 4); if (ncbIndex == 0 && StartNumOfDetectors == 0) { Array.Reverse(rawData, 0, 4); CurrentLineId = BitConverter.ToUInt32(rawData, 0); } #endif // Decode the data into a header. DataPacketHeader packetHeader = new DataPacketHeader(); packetHeader = packetHeader.Deserialize(rawData); //bUInt32 currLineId = packetHeader.LineID; if (ncbIndex == 0 && packetHeader.StartNumOfDetectors == 0) CurrentLineId = packetHeader.LineID; /*currLineId*/ //Logger.LogInfo(String.Format("NCB Index = {0}, Start Num = {1}, End Num = {2}", (int)ncbIndex, packetHeader.StartNumOfDetectors, packetHeader.EndNumOfDetectors)); if (SourcesSynchronized) { if (!stagedLines.ContainsKey(packetHeader.LineID)) { DataInfo[] lineArr = new DataInfo[NCBCount]; AllLineData allData = new AllLineData(); allData.LineID = packetHeader.LineID; allData.LineTimeStamp = DateTime.UtcNow.Ticks; allData.DataComplete = new bool[NCBCount]; allData.BytesRecieved = new int[NCBCount]; allData.NCBData = lineArr; for (int ix = 0; ix < NCBCount; ix++) { lineArr[ix] = new DataInfo(_ncbChannelCount[ix], packetHeader.EnergyAndPulsewidth, (byte)ImageBytesPerPixel); allData.DataComplete[ix] = false; } stagedLines.Add(packetHeader.LineID, allData); } DataInfo currentNCBDataLine = stagedLines[packetHeader.LineID].NCBData[ncbIndex]; int size = (int)((packetHeader.EndNumOfDetectors - packetHeader.StartNumOfDetectors + 1) * packetHeader.NumBytesPerPixel); int linePixelIndex = packetHeader.StartNumOfDetectors; //if(linePixelIndex == 2336) // Logger.LogInfo(String.Format("NCB Index = {0}, Line Id = {1}, LinePixelIndex = {2}, Start Num = {3}, End Num = {4}", (int)ncbIndex, CurrentLineId, linePixelIndex, packetHeader.StartNumOfDetectors, packetHeader.EndNumOfDetectors)); for (int index = 0; index < size; index += packetHeader.NumBytesPerPixel) { if (currentNCBDataLine.LineData[linePixelIndex] == null) { currentNCBDataLine.TotalBytesReceived += packetHeader.NumBytesPerPixel; currentNCBDataLine.LineData[linePixelIndex] = PixelConverter.BytesToPixel(packetHeader.ChData, index, _bytesPerPixel); int offset = 8 * (_bytesPerPixel - (int)_imageBytesPerPixel); currentNCBDataLine.LineData[linePixelIndex].Value = currentNCBDataLine.LineData[linePixelIndex++].Value >> offset; } } stagedLines[packetHeader.LineID].BytesRecieved[ncbIndex] = currentNCBDataLine.TotalBytesReceived; //if (linePixelIndex == 2336) // Logger.LogInfo(String.Format("Data Complete for Line Id = {0}", CurrentLineId)); if ((currentNCBDataLine.TotalBytesReceived / packetHeader.NumBytesPerPixel) >= (_ncbChannelCount[ncbIndex])) { stagedLines[packetHeader.LineID].DataComplete[ncbIndex] = true; bool IsLineComplete = true; for (int i = 0; i < NCBCount; i++) { IsLineComplete &= stagedLines[packetHeader.LineID].DataComplete[i]; } if (IsLineComplete) { DataInfo currentDataLine = new DataInfo(PixelsPerColumn, currentNCBDataLine.XRayInfo, currentNCBDataLine.NumberOfBytesPerPixel); linePixelIndex = 0; for (int i = 0; i < NCBCount; i++) { for (int index = 0; index < stagedLines[packetHeader.LineID].NCBData[i].LineData.Length; index++) { if (i == 0) { if (index == ReferenceSensorCount) index += ReferenceSensorCount; } else { if (index == 0) index += ReferenceSensorCount; } currentDataLine.LineData[linePixelIndex] = stagedLines[packetHeader.LineID].NCBData[i].LineData[index]; linePixelIndex++; } stagedLines[packetHeader.LineID].NCBData[i].Dispose(); } //add this line to the data collection _rawDataCollection.TryAdd(currentDataLine, 500); stagedLines.Remove(packetHeader.LineID); DataGatherTrace--; } List<uint> keysToRemove = new List<uint>(); foreach (KeyValuePair<uint, AllLineData> aLine in stagedLines) { //Check if the line is not complete in 200,000 (20 ms - unit is 100 ns) if (DateTime.UtcNow.Ticks > aLine.Value.LineTimeStamp + 500000) { keysToRemove.Add(aLine.Key); } } // foreach (KeyValuePair<uint, AllLineData> aLine in stagedLines) String removedLineIndices = "Removing old line index: "; foreach (uint key in keysToRemove) { //Logger.LogInfo(String.Format("Removing old line index: {0}, Bytes Received = {1},{2}", identity, // stagedLines[identity].NCBData[0].TotalBytesReceived, // stagedLines[identity].NCBData[1].TotalBytesReceived)); removedLineIndices += String.Format("{0}, ", key); for (int i = 0; i < NCBCount; i++) { stagedLines[key].NCBData[i].Dispose(); } stagedLines.Remove(key); } if (keysToRemove.Count > 0) { //bLogger.LogInfo(removedLineIndices); keysToRemove.Clear(); } } } }