void ReceiveLoop(IAsyncResult ar) { //Packet from remote host received Byte[] receiveBytes = UDP_Client.EndReceive(ar, ref LocalEndPoint); /* Received packets parsing */ //node data packet if (receiveBytes[0] == UDP_NODE_DATA) { //Get X coord from packet byte[] dataX = new byte[4]; Array.Copy(receiveBytes, 1, dataX, 0, 4); Array.Reverse(dataX); //Get Y coord from packet byte[] dataY = new byte[4]; Array.Copy(receiveBytes, 5, dataY, 0, 4); Array.Reverse(dataY); dojoCoords actCoords = new dojoCoords(BitConverter.ToUInt32(dataX, 0), BitConverter.ToUInt32(dataY, 0)); dojoData actData; //get act from table if (ActTable.TryGetValue(actCoords, out actData)) { //update act Value with Threshold actData.Value += actData.Threshold; //save new value in act table ActTable[actCoords] = actData; } } //Start receive next one UDP_Client.BeginReceive(new AsyncCallback(ReceiveLoop), null); }
public MainWindow() { InitializeComponent(); Client = new dojoClient(IPAddress.Parse("192.168.56.101")); UpdateProcess = new Thread(UpdateValues); UpdateProcess.IsBackground = true; UpdateProcess.Start(); Sensor1 = new dojoCoords(0, 0); Sensor2 = new dojoCoords(1, 0); Sensor3 = new dojoCoords(2, 0); Sensor4 = new dojoCoords(3, 0); UpAct = new dojoCoords(0, 1); DownAct = new dojoCoords(1, 1); LeftAct = new dojoCoords(2, 1); RightAct = new dojoCoords(3, 1); Client.RegSensor(Sensor1, 1); Client.RegSensor(Sensor2, 1); Client.RegSensor(Sensor3, 1); Client.RegSensor(Sensor4, 1); Client.RegAct(UpAct, 1); Client.RegAct(DownAct, 1); Client.RegAct(LeftAct, 1); Client.RegAct(RightAct, 1); }