void receiveName(DetectedPLC plc) { UdpClient udp = RecieveUDP(); IPEndPoint PLCEndPoint = new IPEndPoint(IPAddress.Any, thisEndPoint.Port); try { byte[] received = udp.Receive(ref PLCEndPoint); if (PacketBuilder.CheckData(received)) { int length = 0; for (length = 0; length < received.Length; length++) { if (received[length + 11] == 0x00) { break; } } plc.Name = System.Text.Encoding.UTF8.GetString(received, 11, length); } } catch { plc.Name = String.Empty; } }
public void changeIP(DetectedPLC plc, IPAddress IP) { byte[] ip = IP.GetAddressBytes(); UdpClient udp = NewBroadcastUDP(); byte[] data = { 0x15, plc.MAC[0], plc.MAC[1], plc.MAC[2], plc.MAC[3], plc.MAC[4], plc.MAC[5], 0x0c, 0x00, 0x10, 0x00, ip[0], ip[1], ip[2], ip[3] }; byte[] packet = PacketBuilder.BuildHeader(data); udp.Send(packet, packet.Length); udp.EnableBroadcast = false; udp.Close(); }
void requestIP(DetectedPLC plc) { UdpClient udp = NewBroadcastUDP(); byte[] data = new byte[11]; data[0] = 0x15; Array.Copy(plc.MAC, 0, data, 1, plc.MAC.Length); data[7] = 0x0b; data[9] = 0x10; byte[] packet = PacketBuilder.BuildHeader(data); udp.Send(packet, packet.Length); udp.Close(); }
void receiveIP(DetectedPLC plc) { UdpClient udp = RecieveUDP(); IPEndPoint PLCEndPoint = new IPEndPoint(IPAddress.Any, thisEndPoint.Port); try { byte[] received = udp.Receive(ref PLCEndPoint); if (PacketBuilder.CheckData(received)) { byte[] ip = new byte[4]; Array.Copy(received, 11, ip, 0, 4); plc.InternalIP = new IPAddress(ip); } } catch { plc.InternalIP = IPAddress.None; } }
public void getPLCInfo(DetectedPLC PLC = null) { if (PLC == null) { foreach (DetectedPLC plc in PLCList) { requestName(plc); receiveName(plc); requestIP(plc); receiveIP(plc); Console.WriteLine(plc.IP + ": " + BitConverter.ToString(plc.MAC)); Console.WriteLine("\t" + plc.Name + " " + plc.InternalIP); } } else { requestName(PLC); receiveName(PLC); requestIP(PLC); receiveIP(PLC); Console.WriteLine(PLC.IP + ": " + BitConverter.ToString(PLC.MAC)); Console.WriteLine("\t" + PLC.Name + " " + PLC.InternalIP); } }
void populatePLCList() { UdpClient udp = RecieveUDP(); IPEndPoint PLCEndPoint = new IPEndPoint(IPAddress.Any, thisEndPoint.Port); Stopwatch UDPReceiveTimeout = new Stopwatch(); bool timeout = false; while (!timeout) { if (!UDPReceiveTimeout.IsRunning) { UDPReceiveTimeout.Start(); } else { UDPReceiveTimeout.Stop(); UDPReceiveTimeout.Reset(); UDPReceiveTimeout.Start(); } bool dataready = false; while (UDPReceiveTimeout.ElapsedMilliseconds < maxmillis && !dataready) { if (udp.Available > 0) { dataready = true; } } if (dataready) { try { byte[] received = udp.Receive(ref PLCEndPoint); if (PacketBuilder.CheckData(received) && received[9] == 0x55 && received[10] == 0xaa) { DetectedPLC plc = new DetectedPLC(PLCEndPoint.Address); Array.Copy(received, 11, plc.MAC, 0, plc.MAC.Length); bool newplc = true; foreach (DetectedPLC old in PLCList) { if (plc.IP.ToString() == old.IP.ToString()) { newplc = false; } } if (newplc) { PLCbs.Add(plc); } } } catch { timeout = true; } } else { timeout = true; } } udp.Close(); }
DetectedPLC getNewPLC() { UdpClient udp = RecieveUDP(); IPEndPoint PLCEndPoint = new IPEndPoint(IPAddress.Any, thisEndPoint.Port); Stopwatch UDPReceiveTimeout = new Stopwatch(); bool timeout = false; UDPReceiveTimeout.Reset(); while (!timeout) { if (UDPReceiveTimeout.IsRunning) { UDPReceiveTimeout.Stop(); UDPReceiveTimeout.Reset(); UDPReceiveTimeout.Start(); } else { UDPReceiveTimeout.Start(); } bool dataready = false; while (UDPReceiveTimeout.ElapsedMilliseconds < maxmillis && !dataready) { if (udp.Available > 0) { dataready = true; } } if (dataready) { try { byte[] received = udp.Receive(ref PLCEndPoint); if (PacketBuilder.CheckData(received)) { if (received[9] == 0x55 && received[10] == 0xaa) { DetectedPLC plc = new DetectedPLC(PLCEndPoint.Address); bool newplc = true; foreach (DetectedPLC old in PLCList) { if (old.IP.ToString() == plc.IP.ToString()) { newplc = false; } } if (newplc) { Console.WriteLine("new plc detected! IP: " + plc.IP); Array.Copy(received, 11, plc.MAC, 0, plc.MAC.Length); getPLCInfo(plc); udp.Close(); return(plc); } } } } catch { timeout = true; Console.WriteLine("timeout"); } } else { timeout = true; } } udp.Close(); return(null); }