public static PETSingleEventsPacket[] PackEventsToList(PETSingleEvent[] events, int EventsPerPacket) { var res = new List <PETSingleEventsPacket>(); int num = events.Length; int i = 0; while (i < num) { // сколько совпадений в этом пакете (не более EventsPerPacket, но в конце меньше) int cnt = Math.Min(num - i, EventsPerPacket); // начинаем новый пакет var p = new PETSingleEventsPacket(); p.ItemCount = (byte)cnt; p.Flags = 0; p.Events = new PETSingleEvent[cnt]; for (int j = 0; j < cnt; j++) { p.Events[j] = events[i + j]; } i += EventsPerPacket; res.Add(p); } return(res.ToArray()); }
public static PETSingleEventsPacket[] CreateRandomPackets(int NumPackets) { var res = new PETSingleEventsPacket[NumPackets]; var rnd = new System.Random(); // Generate random single events and pack the to packets for (int i = 0; i < NumPackets; i++) { res[i] = PETSingleEventsPacket.RandomPacket(rnd); } return(res); }
public bool CompareTo(PETSingleEventsPacket p) { if ((p.ItemCount != ItemCount) || (p.Flags != Flags)) { return(false); } for (int i = 0; i < ItemCount; i++) { if (!this.Events[i].CompareTo(p.Events[i])) { return(false); } } return(true); }
// static int ItemSize = PETSingleEvent.Size; // Marshal.SizeOf(typeof(PETSingleEvent)); public static PETSingleEventsPacket RandomPacket(System.Random rnd = null) { var res = new PETSingleEventsPacket(); var r = (rnd == null) ? new System.Random() : rnd; res.Flags = (byte)r.Next(); res.ItemCount = (byte)r.Next(1, 10); res.Events = new PETSingleEvent[res.ItemCount]; for (int i = 0; i < res.ItemCount; i++) { res.Events[i] = PETSingleEvent.RandomEvent(rnd); } return(res); }
// один пакет с одним ивентом на детекторе 1 public static PETSingleEventsPacket[] CreateUnitEventPacket() { var evt = new PETSingleEvent(); evt.Flags = 1; evt.Position = 2; evt.XMinus = 3; evt.XPlus = 4; evt.YMinus = 5; evt.YPlus = 6; evt.Timestamp = 255; var packet = new PETSingleEventsPacket(); packet.Flags = 63; packet.ItemCount = 1; packet.Events = new PETSingleEvent[1]; packet.Events[0] = evt; return(new PETSingleEventsPacket[] { packet }); }
/// <summary> /// Decode byte[] array to a list of SingleEvent[] /// </summary> public static PETSingleEventsPacket[] DecodeArray(int NumPackets, int Offset, byte[] Src, bool UseSizeInfo = false) { int Ofs = 0; var res = new PETSingleEventsPacket[NumPackets]; for (int i = 0; i < NumPackets; i++) { if (UseSizeInfo) { // skip two bytes Ofs += 2; } var pack = new PETSingleEventsPacket(); pack.FromBytes(Offset + Ofs, Src); res[i] = pack; Ofs += pack.GetSize(); } return(res); }