private void thread() { UdpClient ucl = new UdpClient(6000); IAsyncResult iar = ucl.BeginReceive(null, null); while (!exit) { if (!iar.AsyncWaitHandle.WaitOne(1000)) { continue; } IPEndPoint ep = new IPEndPoint(0, 0); byte[] data = ucl.EndReceive(iar, ref ep); iar = ucl.BeginReceive(null, 0); using (MemoryStream ms = new MemoryStream(data)) { BinaryReader br = new BinaryReader(ms); while (ms.Position < ms.Length) { CanMessage m = CanMessage.DeserializeFrom(br); if (MessageReceived != null) { MessageReceived(this, m); } } } } ucl.Close(); }
public static CanMessage DeserializeFrom(BinaryReader br) { CanMessage msg = new CanMessage(); msg.COB = br.ReadUInt32(); msg.Time = br.ReadUInt16(); msg.Source = br.ReadByte(); br.ReadByte(); /* zero */ byte[] by = br.ReadBytes(8); msg.Data = new byte[br.ReadByte()]; Array.Copy(by, msg.Data, msg.Data.Length); br.ReadBytes(7); /* PAD */ UInt64 ticks = br.ReadUInt64(); msg.Sec = (UInt32)(long)(ticks / (1000 * 1000)); msg.Usec = (UInt32)((long)(ticks % (1000 * 1000))); return msg; }
public static CanMessage DeserializeFrom(BinaryReader br) { CanMessage msg = new CanMessage(); msg.COB = br.ReadUInt32(); msg.Time = br.ReadUInt16(); msg.Source = br.ReadByte(); br.ReadByte(); /* zero */ byte[] by = br.ReadBytes(8); msg.Data = new byte[br.ReadByte()]; Array.Copy(by, msg.Data, msg.Data.Length); br.ReadBytes(7); /* PAD */ UInt64 ticks = br.ReadUInt64(); msg.Sec = (UInt32)(long)(ticks / (1000 * 1000)); msg.Usec = (UInt32)((long)(ticks % (1000 * 1000))); return(msg); }