Exemple #1
0
        private static NetworkInfo GetNetworkInfo()
        {
            NetworkInfo networkInfo = new NetworkInfo();

            if (NetworkInterface.GetIsNetworkAvailable() == true)
            {
                NetworkInterface NetworkInterface = null;
                foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet &&
                        ni.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
                    {
                        continue;
                    }

                    if (ni.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }

                    NetworkInterface = ni;
                    break;
                }

                string Ssid = GetWifiSsid();

                IPInterfaceProperties IPInterfaceProperties = NetworkInterface.GetIPProperties();

                string IPAddress  = string.Empty;
                string SubnetMask = string.Empty;
                foreach (UnicastIPAddressInformation ip in IPInterfaceProperties.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        IPAddress  = ip.Address.ToString();
                        SubnetMask = ip.IPv4Mask.ToString();
                    }
                }

                string GatewayAddress = string.Empty;
                foreach (GatewayIPAddressInformation gateway in IPInterfaceProperties.GatewayAddresses)
                {
                    if (gateway.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        GatewayAddress = gateway.Address.ToString();
                    }
                }

                string MacAddress = string.Empty;
                foreach (byte mac in NetworkInterface.GetPhysicalAddress().GetAddressBytes())
                {
                    if (string.IsNullOrEmpty(MacAddress) == false)
                    {
                        MacAddress += ":";
                    }
                    MacAddress += mac.ToString("X2");
                }

                networkInfo.Ssid       = Ssid;
                networkInfo.IpAddress  = IPAddress;
                networkInfo.MacAddress = MacAddress;
                networkInfo.Gateway    = GatewayAddress;
                networkInfo.Submask    = SubnetMask;
            }

            return(networkInfo);
        }
Exemple #2
0
        private static void ProcessDataReceived(char DataCharIn)
        {
            string Data;
            bool   OkToProcessCommand = false;
            string ScreenChar         = string.Empty;
            string DataChar           = string.Empty;
            string CommandChar        = string.Empty;

            //Data = mSerialPort.ReadExisting();
            Data = DataCharIn.ToString();

            //Console.Write(((int)Data[0]).ToString() + " ");
            //return;

            if (_TcpClientHandler.Connected == true)
            {
                try
                {
                    _TcpClientHandler.SendData(Data);
                }
                catch
                {
                    _TcpClientHandler.Close();
                }
                return;
            }

            switch (Data)
            {
            case "\r":     // Return
                OkToProcessCommand = true;
                ScreenChar         = Environment.NewLine;
                //DataChar = Environment.NewLine;
                CommandChar = string.Empty;

                if (_ShowWelcomeMessage == true)
                {
                    _CommandString      = string.Empty;
                    OkToProcessCommand  = false;
                    _ShowWelcomeMessage = false;

                    NetworkInfo networkInfo = GetNetworkInfo();

                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("NULLMODEMSERVER BY VBGUYNY" + Environment.NewLine);
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("CONNECTING TO SSID " + networkInfo.Ssid + Environment.NewLine);
                    mSerialPort.Write("CONNECTED TO " + networkInfo.Ssid + Environment.NewLine);
                    mSerialPort.Write("IP ADDRESSS: " + networkInfo.IpAddress + Environment.NewLine);
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    mSerialPort.Write(Environment.NewLine);
                }

                if (_EchoMode == false)
                {
                    //mSerialPort.Write(Environment.NewLine);
                }

                break;

            case "\u0014":     // Backspace
                ScreenChar  = "\b \b";
                DataChar    = Data;
                CommandChar = string.Empty;

                if (string.IsNullOrEmpty(_CommandString) == false)
                {
                    _CommandString = _CommandString.Remove(_CommandString.Length - 1, 1);
                }
                break;

            default:
                ScreenChar  = Data;
                DataChar    = Data;
                CommandChar = Data;
                break;
            }

            Console.Write(ScreenChar);


            if (string.IsNullOrEmpty(DataChar) == false)
            {
                //if (_EchoMode == true)
                if (_EchoMode == true && _ShowWelcomeMessage == false)
                {
                    mSerialPort.Write(DataChar);
                }
            }

            if (OkToProcessCommand == true)
            {
                if (string.IsNullOrWhiteSpace(_CommandString) == true)
                {
                    return;
                }

                mSerialPort.Write(Environment.NewLine);

                switch (_CommandString)
                {
                case "AT?":
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("NULLMODEMSERVER BY VBGUYNY" + Environment.NewLine);
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("AT COMMAND SUMMARY:" + Environment.NewLine);
                    //mSerialPort.Write("LOAD VRAM....:  AT&V" + Environment.NewLine);
                    mSerialPort.Write("DOWNLOAD URL.:  ATGET URL" + Environment.NewLine);
                    mSerialPort.Write("QUERY MOST COMMANDS FOLLOWED BY '?'" + Environment.NewLine);
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    break;

                case "ATI":

                    if (NetworkInterface.GetIsNetworkAvailable() == true)
                    {
                        //NetworkInterface NetworkInterface = null;
                        //foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
                        //{
                        //    if (ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet
                        //        && ni.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
                        //    {
                        //        continue;
                        //    }

                        //    if (ni.OperationalStatus != OperationalStatus.Up)
                        //    {
                        //        continue;
                        //    }

                        //    NetworkInterface = ni;
                        //    break;
                        //}

                        //string Ssid = GetWifiSsid();

                        //IPInterfaceProperties IPInterfaceProperties = NetworkInterface.GetIPProperties();

                        //string IPAddress = string.Empty;
                        //string SubnetMask = string.Empty;
                        //foreach (UnicastIPAddressInformation ip in IPInterfaceProperties.UnicastAddresses)
                        //{
                        //    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        //    {
                        //        IPAddress = ip.Address.ToString();
                        //        SubnetMask = ip.IPv4Mask.ToString();
                        //    }
                        //}

                        //string GatewayAddress = string.Empty;
                        //foreach (GatewayIPAddressInformation gateway in IPInterfaceProperties.GatewayAddresses)
                        //{
                        //    if (gateway.Address.AddressFamily == AddressFamily.InterNetwork)
                        //    {
                        //        GatewayAddress = gateway.Address.ToString();
                        //    }
                        //}

                        //string MacAddress = string.Empty;
                        //foreach (byte mac in NetworkInterface.GetPhysicalAddress().GetAddressBytes())
                        //{
                        //    if (string.IsNullOrEmpty(MacAddress) == false)
                        //    {
                        //        MacAddress += ":";
                        //    }
                        //    MacAddress += mac.ToString("X2");
                        //}

                        NetworkInfo ni = GetNetworkInfo();

                        mSerialPort.Write("WIFI STATUS: CONNECTED" + Environment.NewLine);
                        mSerialPort.Write("SSID.......: " + ni.Ssid + Environment.NewLine);
                        mSerialPort.Write("MAC ADDRESS: " + ni.MacAddress + Environment.NewLine);
                        mSerialPort.Write("IP ADDRESS.: " + ni.IpAddress + Environment.NewLine);
                        mSerialPort.Write("GATEWAY....: " + ni.Gateway + Environment.NewLine);
                        mSerialPort.Write("SUBNET MASK: " + ni.Submask + Environment.NewLine);
                        mSerialPort.Write("SERVER PORT: N/A" + Environment.NewLine);
                        mSerialPort.Write("WEB CONFIG.: N/A" + Environment.NewLine);
                        mSerialPort.Write("CALL STATUS: NOT CONNECTED" + Environment.NewLine);
                    }
                    else
                    {
                        mSerialPort.Write("WIFI STATUS: OFFLINE" + Environment.NewLine);
                        mSerialPort.Write("SSID.......: " + Environment.NewLine);
                        mSerialPort.Write("MAC ADDRESS: " + Environment.NewLine);
                        mSerialPort.Write("IP ADDRESS.: " + Environment.NewLine);
                        mSerialPort.Write("GATEWAY....: " + Environment.NewLine);
                        mSerialPort.Write("SUBNET MASK: " + Environment.NewLine);
                        mSerialPort.Write("SERVER PORT: N/A" + Environment.NewLine);
                        mSerialPort.Write("WEB CONFIG.: N/A" + Environment.NewLine);
                        mSerialPort.Write("CALL STATUS: NOT CONNECTED" + Environment.NewLine);
                    }

                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    break;

                case "AT":
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    break;

                case "ATE":
                case "ATE0":
                    _EchoMode = false;
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    break;

                case "ATE1":
                    _EchoMode = true;
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write("OK");
                    break;

                case "ATE?":
                    mSerialPort.Write(Environment.NewLine);
                    mSerialPort.Write((_EchoMode == true ? "1" : "0"));
                    break;

                default:
                    if (_CommandString.StartsWith("ATGET") == true)
                    {
                        mSerialPort.Write(Environment.NewLine);

                        DateTime dtStart = DateTime.Now;

                        string WebAddress = _CommandString.Substring(5).Replace(" ", string.Empty);
                        string WebContent = string.Empty;

                        try
                        {
                            //WebContent = mWebClient.DownloadString(WebAddress);

                            string[] WebAddressParts    = WebAddress.Split('/');
                            string   HostName           = WebAddressParts[2];
                            string   RelativeWebAddress = string.Join("/", WebAddressParts, 3, WebAddressParts.Length - 3);

                            TcpClient tcp = new TcpClient();
                            tcp.Connect(HostName, 80);

                            using (NetworkStream ns = tcp.GetStream())
                            {
                                using (StreamWriter sw = new StreamWriter(tcp.GetStream()))
                                {
                                    sw.WriteLine("GET /" + RelativeWebAddress + " HTTP/1.1");
                                    sw.WriteLine("Host: " + HostName);
                                    sw.WriteLine("Connection: close");
                                    sw.WriteLine();
                                    sw.Flush();

                                    using (StreamReader sr = new StreamReader(tcp.GetStream()))
                                    {
                                        WebContent = sr.ReadToEnd();
                                    }
                                }
                            }
                            tcp.Close();

                            Console.WriteLine(WebContent);

                            mSerialPort.Write("CONNECT " + BaudRate.ToString() + Environment.NewLine);
                            mSerialPort.Write(WebContent);
                            mSerialPort.Write(Environment.NewLine);
                        }
                        catch { }

                        DateTime dtEnd = DateTime.Now;

                        mSerialPort.Write("NO CARRIER (" + (dtEnd - dtStart).ToString(@"hh\:mm\:ss") + ")");
                    }
                    else if (_CommandString.StartsWith("ATDT") == true)
                    {
                        string WebAddress = _CommandString.Substring(4).Replace(" ", string.Empty);
                        string WebContent = string.Empty;

                        string[] WebAddressParts;
                        string   HostName   = string.Empty;
                        int      PortNumber = 23;

                        try
                        {
                            WebAddressParts = WebAddress.Split(':');
                            HostName        = WebAddressParts[0];
                            PortNumber      = Convert.ToInt32(WebAddressParts[1]);
                        }
                        catch { }

                        mSerialPort.Write("DIALING " + HostName + ":" + PortNumber.ToString());
                        mSerialPort.Write(Environment.NewLine);
                        mSerialPort.Write(Environment.NewLine);

                        if (string.IsNullOrEmpty(HostName) == true)
                        {
                            mSerialPort.Write("NO ANSWER");
                        }
                        else
                        {
                            try
                            {
                                _TcpClientHandler.Connect(HostName, PortNumber);

                                //NetworkStream ns = _TcpClient.GetStream();
                                //TcpClientHandler tch = new TcpClientHandler(_TcpClient, mSerialPort);

                                mSerialPort.Write("CONNECT " + BaudRate.ToString());
                                //mSerialPort.Write(WebContent);
                                //mSerialPort.Write(Environment.NewLine);
                            }
                            catch
                            {
                                mSerialPort.Write("NO ANSWER");
                            }
                        }
                    }
                    else if (_CommandString.StartsWith("AT&") == true)
                    {
                        mSerialPort.Write(Environment.NewLine);
                        mSerialPort.Write("OK");
                    }
                    else
                    {
                        mSerialPort.Write(Environment.NewLine);
                        mSerialPort.Write("ERROR");
                    }
                    break;
                }

                mSerialPort.Write(Environment.NewLine);

                _CommandString = string.Empty;
            }
            else
            {
                if (CommandChar != " ")
                {
                    _CommandString += CommandChar;
                }
            }

            //foreach (char c in WebAddress)
            //{
            //    Console.Write((int)c);
            //}

            return;
        }