Exemplo n.º 1
0
        public List<EnvParameters> EnvQuery()
        {
            List<EnvParameters> parameters = new List<EnvParameters>();
            try
            {
                byte[] input = new byte[1024];
                byte[] recv = new byte[1024];
                byte[] data = new byte[1024];
                int len_recv;

                //string stringRecv;
                IPAddress ip = IPAddress.Parse("127.0.0.1");  //定义主机的IP及端口
                IPEndPoint ipEnd = new IPEndPoint(ip, port);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    socket.Connect(ipEnd);

                    input = commands["Stop"];
                    socket.Send(input, input.Length, SocketFlags.None);

                    len_recv = socket.Receive(recv);
                    data[0] = recv[2];  //add
                    data[1] = recv[3];  //c
                    data[2] = recv[4];  //l
                    data[3] = recv[5];  //data
                    data[4] = recv[5];  //data
                    data[5] = recv[5];  //data
                    data[6] = recv[5];  //data
                    Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(recv, 0, len_recv));

                    // Release the socket.
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();

                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }
                if (data[3] != CheckSum(data)) //cs
                { throw new Exception(); }

                string windDirection = System.Text.Encoding.ASCII.GetString(new[] { data[4] });
                EnvParameters parameter = new EnvParameters((double)data[3],
                    (string)windDirection, (double)data[5], (double)data[6]);
                parameters.Add(parameter);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return parameters;
        }
Exemplo n.º 2
0
        public List<EnvParameters> EnvQuery()
        {
            Write(commands["Query"]);
            byte[] data = new byte[4];
            List<EnvParameters> parameters = new List<EnvParameters>();
            try
            {
                data = ReadData();
                string windDirection = System.Text.Encoding.ASCII.GetString(new[] { data[1] });
                EnvParameters parameter = new EnvParameters((double)data[0],
                    (string)windDirection, (double)data[2], (double)data[3]);
                parameters.Add(parameter);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
                throw new Exception();

            }
            return parameters;
        }