public event UDPPortdHandler UDPPortEvent; //[6] DateTimeRecievedHandler is the delegate public void ReadFromUDPPort(int myPort) { try { UdpClient receivingUdpClient = new UdpClient(); IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, myPort); receivingUdpClient.ExclusiveAddressUse = false; receivingUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); receivingUdpClient.Client.Bind(remoteIpEndPoint); while (true) { Byte[] receiveBytes = receivingUdpClient.Receive(ref remoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); if (returnData.Length > 0) { SendUDPPortArgs args = new SendUDPPortArgs() { gpsData = returnData, ThreadEnd = true }; UDPPortEvent.Invoke(null, args); } } } catch { SendUDPPortArgs args = new SendUDPPortArgs() { ThreadEnd = true }; //thread has closed because of error UDPPortEvent.Invoke(null, args); } }
//Deal with GPS data from UDP private void UDPPortTaskActive(object sender, SendUDPPortArgs args) { if (args.ThreadEnd != false) //Serial Port Task has not ended .. it shouldnt { if (args.gpsData != null) //dont process string if its null... will it ever be null? { GPSDataProcessing gPSDataString = new GPSDataProcessing(); //create instance of GPS claas gPSDataString.Process(args.gpsData); //pass data to class } NewGPSDataRecieved(args.gpsData); } else { UnexpectedThreadEnd(2); } }