public bool CanVerifyTcpConnection(EsMount mount) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " Start Verify TCP Connection"); //Check Wireless first if (!CommunicationsManager.IsConnectedToWirelessNetwork()) { return(false); } if (!CommunicationsManager.ConnectedSsid().Contains("PMC")) { return(false); } bool responseIsGood = false; try { string result = CommunicationsManager.SendTcpMessage(mount.ConnectionSettings.IpAddr, mount.ConnectionSettings.IpPort, TCP_QUERY); responseIsGood = result.Contains(TCP_RESPONSE); EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " Done Verify TCP Connection: " + responseIsGood); return(responseIsGood); } catch (Exception ex) { EsException e = new EsException("Error in :" + "CanVerifyTcpConnection", ex); //TODO: EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " Done Verify TCP Connection - Failure"); return(false); } }
public string SendUdpMessage(string ipAddr, Int32 comPort, string msg) { EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " Start Sending UDP Message: " + msg); UdpClient udpClient = new UdpClient(54372); udpClient.Client.ReceiveTimeout = 1500; string returnData = "No Data Received"; try { udpClient.Connect("192.168.47.1", 54372); // Sends a message to the host to which you have connected. Byte[] sendBytes = Encoding.ASCII.GetBytes(msg + Environment.NewLine); udpClient.Send(sendBytes, sendBytes.Length); // Sends a message to a different host using optional hostname and port parameters. UdpClient udpClientB = new UdpClient(); Thread.Sleep(1000); udpClientB.Send(sendBytes, sendBytes.Length, "192.168.47.1", 54372); //IPEndPoint object will allow us to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Thread.Sleep(1000); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); returnData = Encoding.ASCII.GetString(receiveBytes); // Uses the IPEndPoint object to determine which of these two hosts responded. Console.WriteLine("This is the message you received " + returnData.ToString()); Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()); udpClient.Close(); udpClientB.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); EsException e = new EsException("Error in :" + "SendUdpMessage", ex); throw e; } EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " UDP Message Received: " + returnData); return(returnData.ToString()); }
public string SendTcpMessageOLD(string ipAddr, Int32 comPort, string msg) { if (!ConnectedToPmcNetwork) { throw new EsException("Must be connected to PMC=8 network."); } string message; try { EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " Start Sending TCP Message: " + msg); // Create TCP client and connect using (var _client = new TcpClient(ipAddr, comPort)) using (var _netStream = _client.GetStream()) { //_netStream.ReadTimeout = 2000; // Write a message over the socket message = Environment.NewLine + msg + Environment.NewLine; byte[] dataToSend = System.Text.Encoding.ASCII.GetBytes(message); _client.Client.ReceiveTimeout = 1500; _netStream.Write(dataToSend, 0, dataToSend.Length); Thread.Sleep(700); // Read server response byte[] recvData = new byte[512]; int bytes = _netStream.Read(recvData, 0, recvData.Length); message = System.Text.Encoding.ASCII.GetString(recvData, 0, bytes); Console.WriteLine(string.Format("IP Server: {0}", message)); };// The client and stream will close as control exits the using block (Equivilent but safer than calling Close(); } catch (System.Net.Sockets.SocketException ex) { throw new ApplicationException("Can not read/write TCP command", ex); } catch (System.IO.IOException ex) { throw new ApplicationException("Can not read/write TCP command", ex); } catch (Exception ex) { throw new ApplicationException("Can not read/write TCP command", ex); } finally { } EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " TCP Response: " + message); return(message); }
private bool IsConnectedToPmcNetwork() { bool result = false; try { NetworkIdentifier ss = NativeWifi.EnumerateConnectedNetworkSsids().FirstOrDefault(); if (ss != null) { result = ss.ToString().Contains("PMC"); } } catch (Exception ex) { EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, "ERROR", EsMessagePriority.DebugInfo, "error in IsConnectedToPmcNetwork ", null, ex, null); } return(result); }
public EsMount ConnectMount(EsMount mount) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DetailedInfo, " Start Connecting to Mount"); EsMount result = mount; string ipaddress = mount.ConnectionSettings.IpAddr; int ipPort = mount.ConnectionSettings.IpPort; string serPort = mount.ConnectionSettings.SerPort; if (!String.IsNullOrWhiteSpace(serPort)) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Try to Verify - Serial Connection"); if (CanVerifySerialConnection(mount)) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Verified - Serial Connection"); result.ConnectionSettings.IsConnected = ConnectionEnum.Serial; return(result); } } if (!String.IsNullOrWhiteSpace(ipaddress)) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Try to Verifiy TCP Connection"); if (CanVerifyTcpConnection(mount)) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Verified - TCP Connection"); result.ConnectionSettings.IsConnected = ConnectionEnum.TCP; return(result); } EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Try to Verify UDP Connection"); if (CanVerifyUdpConnection(mount)) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.GeneralInfo, "Verified - UDP Connection"); result.ConnectionSettings.IsConnected = ConnectionEnum.UDP; return(result); } } EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DetailedInfo, "Failed to Verify Connection Method"); return(null); }
public bool CanVerifyUdpConnection(EsMount mount) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " Start Verify UDP Connection"); //Check Wireless first if (!CommunicationsManager.IsConnectedToWirelessNetwork()) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " End Verify UDP Connection - Failure. Not connected to a network"); return(false); } if (!CommunicationsManager.ConnectedSsid().Contains("PMC")) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " End Verify UDP Connection - Failure. Not connected to a PMC network"); return(false); } bool versionGood = false; try { string result = CommunicationsManager.SendUdpMessage(mount.ConnectionSettings.IpAddr, mount.ConnectionSettings.IpPort, UDP_QUERY); versionGood = result.Contains(UDP_RESPONSE); EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " End Verify UDP Connection: " + versionGood); return(versionGood); } catch (Exception ex) { EsException e = new EsException("Error in :" + "CanVerifyUdpConnection", ex); //TODO: //EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DetailedInfo, " End Verify UDP Connection - Failure. ERROR"); EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, "CanVerifyUdpConnection", EsMessagePriority.DetailedInfo, "", null, ex, null); return(false); } }
public bool CanVerifySerialConnection(EsMount mount) { EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " Start Very Serial Connection"); string[] ports = SerialPort.GetPortNames(); bool responseIsGood = false; if (ports.Rank > 0 && mount.ConnectionSettings.SerPort != null) { try { string result = CommunicationsManager.SendSerialMessage(mount.ConnectionSettings.SerPort, SERIAL_QUERY); responseIsGood = result.Contains(SERIAL_RESPONSE); } catch (Exception ex) { EsException e = new EsException("Error in :" + "CanVerifySerialConnection", ex); throw e; } } EsEventManager.PublishEsEvent(EsEventSenderEnum.EsMountManager, EsMessagePriority.DebugInfo, " End Very Serial Connection: " + responseIsGood); return(responseIsGood); }
public string SendSerialMessage(string serialPort, string msg) { EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " Start Sending Serial Message: " + msg); Mutex mutex = new Mutex(); mutex.WaitOne(); if (serialPort == String.Empty) { throw new EsException("Error in :" + "SendSerialMessage - Serial Port can not be empty"); } string newData; try { SerialPort Port = new SerialPort(serialPort, 115200, Parity.None, 8); Port.Open(); Port.WriteLine(msg); Thread.Sleep(500); newData = Port.ReadExisting().ToString(); Console.WriteLine("Received on serial: " + newData); Port.Close(); } catch (Exception ex) { EsException e = new EsException("Error in :" + "SendSerialMessage", ex); throw e; } finally { mutex.ReleaseMutex(); } EsEventManager.PublishEsEvent(EsEventSenderEnum.ComManager, EsMessagePriority.DetailedInfo, " Serial Returned: " + newData); return(newData); }