当前位置:编程学习 > C#/ASP.NET >>

C#编程问题



我在实习中需要编写个调用其他软件中的数据,数据为tagid,x,y,z,time.然后把这个坐标利用程序meaage部分的已知量theta,a,b,c换成GPS坐标输出,但是写来写去总是不对,我已经快好几天没睡觉了,感觉自己水平很烂,对自己失去了信心,所以真心请教前辈。请那位大大给我看看程序,纠正一下,小弟跪拜!!
程序分为server,client和message。


Server为:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

using Star.UBase;
using Star.ULocation;
using Star.ULocation.CellData;


namespace UAtlas
{
    class UAtlasServer
    {
        static UbiBuffer buffer = null;

        static void Main(string[] args)
        {
            TcpListener server = null;
            TcpClient client = null;

            // create multicell object and load all Star cells
            MultiCell multicell = new MultiCell();
            SortedDictionary<string, Cell> cells = multicell.GetAvailableCells();
            foreach (Cell _cell in cells.Values)
                multicell.LoadCell(_cell, true);


    using (Star.ULocation.CellData.ReadTransaction xact = multicell.Schema.ReadTransaction())
    {
        foreach (Star.ULocation.CellData.Location.RowType row in Star.ULocation.CellData.Location.object_(xact))
        {
             Star.UBase.UObject obj = row.object_;

            // If the object is a tag, return the tag id as the name.
            Star.ULocationIntegration.Tag tag = new Star.ULocationIntegration.Tag();
            tag.Narrow(obj);
            
        }
    }




            Star.ULocation.CellData.Location.AddUpdateHandler(multicell.Schema, CellData_Update);
            //Star.ULocation.CellData.Location.AddUpdateHandler(multicell.Schema, TestCellData_Update);

            try
            {
                IPHostEntry myIpHostEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress ipAdress = myIpHostEntry.AddressList[0];
                string portStr = args[0];
                int port = Int32.Parse(portStr);
                IPEndPoint serverAddr = new IPEndPoint(ipAdress, port);
                Console.WriteLine("Listen at {0}", serverAddr);


                // TcpListener server = new TcpListener(port);
                server = new TcpListener(serverAddr);

                // Start listening for client requests.
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[1024];

                // Enter the listening loop.
                while (true)
                {
                    Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests.
                    
                    client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    try
                    {

                        buffer = new UbiBuffer(100);

                        Console.WriteLine("Start loop...");

                        while (true)
                        {
                            if (buffer.Count == 0)
                            {
                                System.Threading.Thread.Sleep(25);
                                continue;
                            }
                            UAtlasMessage message = (UAtlasMessage)buffer.Next();

                            byte[] msg = System.Text.Encoding.ASCII.GetBytes(message.MessageString());

                            //Console.WriteLine(message);
                            Console.Write(".");

                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        buffer = null;
                        // Shutdown and end connection
                        client.Close();
                    }
                    Console.WriteLine("...end of loop");
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }
        }

        static void CellData_Update(Location.RowType old_row, Location.RowType new_row)
        {
            //Console.WriteLine("Got an update for {0}", new_row.object_.Id);
            if (buffer == null)
                return;

            Star.UBase.UObject obj = new_row.object_;

            // If the object is a tag, return the tag id as the name.
            Star.ULocationIntegration.Tag tag = new Star.ULocationIntegration.Tag();
            tag.Narrow(obj);
            if (!tag.Nil())
            {
                string id = Star.ULocationIntegration.Tag.ConvertIdToString(tag.PhysicalId, '-');
                Vector3D P = new_row.position_.P;
                UAtlasMessage message = new UAtlasMessage(id, P.X, P.Y, P.Z, new_row.time_);
                try
                {
                    buffer.Add(message);
                }
                catch
                {
                }
            }
        }


        static void TestCellData_Update(Location.RowType old_row, Location.RowType new_row)
        {
            if (buffer == null)
                return;

            Star.UBase.UObject obj = new_row.object_;

            string id = obj.Id.ToString();
            Vector3D P = new_row.position_.P;
            UAtlasMessage message = new UAtlasMessage(id, P.X, P.Y, P.Z, new_row.time_);
            try
            {
                buffer.Add(message);
            }
            catch
            {
            }
        }
    }
}



--------------------编程问答--------------------
Client部分

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;


namespace UAtlas
{
    class UAtlasClient
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: UAtlasClient <Server-IP>");
                return;
            }

            try
            {
                IPAddress serverIP = IPAddress.Parse(args[0]);
                IPHostEntry myIpHostEntry = Dns.GetHostEntry(serverIP);
                IPEndPoint server = new IPEndPoint(myIpHostEntry.AddressList[0], Int32.Parse(args[1]));
               // Console.WriteLine("Trying to connect to {0}",server);
                
                // Create a TcpClient.
                TcpClient client = new TcpClient();
                client.Connect(server);

                // Get a client stream for reading and writing.
                NetworkStream stream = client.GetStream();

                // Buffer to store the response bytes.
                Byte[] data = new Byte[1024];

                // String to store the response ASCII representation.
                string responseData = string.Empty;

                try
                {
                    while (true)
                    {
                        Int32 bytes = stream.Read(data, 0, data.Length);
                        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                        UAtlasMessage message = new UAtlasMessage(responseData);

                        //Console.WriteLine("{0}", message);
                       // Console.WriteLine("Received: {0}", message);
                       Console.WriteLine("{0}", message);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                //    //Console.WriteLine();
                //    //Console.WriteLine(ex.StackTrace);
                }

                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
        }
    }


}




message部分

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

namespace UAtlas
{
    public class UAtlasMessage
    {
        static NumberFormatInfo fi = new NumberFormatInfo();
        public char Header = 'R';
        public string TagID;
        public double X;
        public double Y;
        public double Z;
        public int BatteryStatus = 13;
        public int Timestamp;
        public int Unit = 1;

        //public int diff;
        


        public UAtlasMessage()
        {
        }

        public UAtlasMessage(string tagid, double x, double y, double z, int diff, DateTime time)
        {
            TagID = tagid;
            double theta = 1.8;
            double a = 398169.59;
            double b = 5707122;
            double c = 131;


            double x0 = x;
            double y0 = y;
            double z0 = z;
            X = (Math.Cos(theta))*x0-Math.Sin(theta)*y0+a;
            Y = (Math.Sin(theta))*x0+Math.Cos(theta)*y0+b;
            Z = z0 + c;
            //Timestamp = ConvertDateTime(time);
            Timestamp = diff;
        }

        public UAtlasMessage(string message)
        {
            string[] components = message.Split(new char [] {','});
            TagID = components[0];
            X = Convert.ToDouble(components[1], fi);
            Y = Convert.ToDouble(components[2], fi);
            Z = Convert.ToDouble(components[3], fi);
            Timestamp = Convert.ToInt32(components[4],fi);

          
        }

        private static int ConvertDateTime(DateTime time)
        {
            DateTime baseTime = DateTime.Parse("01.11.2010 00:00:00");

           
            TimeSpan diff = time.Subtract(baseTime);
            return (int)diff.TotalMilliseconds;
        }
        /*
        private static DateTime ConvertUTCTime(int time)
        {
            DateTime baseTime = DateTime.Parse("1/11/2010 0:0:0");
            TimeSpan diff = new TimeSpan(0, 0, time);
            return baseTime + diff;
        }
        */
        public override string ToString()
        {
           return String.Format("{0}", MessageString());
           // return String.Format("(\"{0}\")", MessageString());
           //return String.Format("TimeSyncCommand(\"{0}\")", MessageString());
        }

                
        public string MessageString()
        {
         
            return String.Format(fi, "{0},{1},{2:0.00},{3:0.00},{4}", TagID, X, Y, Z, Timestamp);
       
        
        }
        
    }
}
--------------------编程问答-------------------- --------------------编程问答-------------------- 没人会吗? --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 你说简单说一下,现在到那儿了,碰到什么问题了,还有你的思路可以讲完整一些。
--------------------编程问答--------------------
引用 6 楼 libinguest 的回复:
你说简单说一下,现在到那儿了,碰到什么问题了,还有你的思路可以讲完整一些。
  +1  贴这么一堆干嘛 --------------------编程问答-------------------- UAtlasMessage message = new UAtlasMessage(id, P.X, P.Y, P.Z, new_row.time_);
public UAtlasMessage(string tagid, double x, double y, double z, int diff, DateTime time)

参数个数对吗,我没眼花吧
--------------------编程问答-------------------- 太多了.看着很累.直接说问题吧. --------------------编程问答-------------------- 没看明白呀
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,