اطلاعیه

Collapse
No announcement yet.

ارتباط به وسیله پروتکل TCP/IP با میکرو

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    ارتباط به وسیله پروتکل TCP/IP با میکرو

    سلام دوستان
    من پروژه ای بستم که در اون از طریق یک کارت شبکه wireless به یک wireless access point متصل می شوم و از طریق access point هم با یک قطعه واسط (TCP EZL) به یک میکرو متصل هستم که کار این قطعه تبدیل پروتکل TCP/IP به serial هست. برنامه هم با visual C# در کامپیوتر نوشتم که به وسیله socket ها اطلاعاتم رو به کارت شبکه ، اکسس پینت و میکرو می فرستم و در میکرو هم از طریق ارتباط USART اطلاعات می گیرم ، برای Command ها و تعریف هایی که خودم نوشتم مشکلی ندارم و دستگاههایی رو که می خواهم کنترل می کنم فقط زمانی که می خواهم یک text رو روی LCD عادی که به PORT A میکرو متصل کردم نمایش بدم ، اون چیزی رو که میفرستم نشان نمیده و علائم نامربوطی نشان می دهد. قابل توجه دوستان که اطلاعات باید به صورت Byte فرستاده شود. یعنی من text را به یک CharArray تبدیل می کنم وبه هر charا یک Byte اختصاص می دم بعدا مثلا در یک SendingTextBuffer[] می فرستم و در میکرو با دستور gets(TextArray,8) اطلاعات رو می گیرم و با دستور lcd_puts(TextArray) میخواهم اون را نمایش بدم . اما تنیجه نمی گیرم ، میشه دوستان راهنمایی کنند که چی کار کنم ؟ پیشا پیش ممنون !

    برنامه دقیق تر C#
    char[] chrOutput = strOutput.ToCharArray();
    TcpClient tcpClnt = new TcpClient();
    tcpClnt.Connect(IPEndPoibt, PortNum);
    Stream stm = tcpClnt.GetStream();

    byte[] SendingTextBuffer = Encoding.ASCII.GetBytes(chrOutput);

    stm.Write(SendingTextBuffer, 0, SendingTextBuffer.Length);

    #2
    پاسخ : ارتباط به وسیله پروتکل TCP/IP با میکرو

    سلام،

    اگر با TCP EZL در مد ATC کار می کنید باید برای هر پکتی که ارسال می کنید از سریال پاسخ بگیرید. در همین حین سوکت رو با یک اسنیفر (مثل Ethereal) مانیتور کنید ببینید چه خبره! :smile:
    There is nothing so practical as a good theory. — Kurt Lewin, 1951

    دیدگاه


      #3
      پاسخ : ارتباط به وسیله پروتکل TCP/IP با میکرو

      منون از جوابتون
      اما فکر نکنم که مشکل سخت افزاری یا packet lost داشته باشم چون در موارد دیگه مشکلی ندارم فکر کنم مشکلی در برنامه #C یا برنامه میکرو باشه
      اگه دوستان در اون زمینه راهنمایی کنن ممنون می شم

      دیدگاه


        #4
        پاسخ : ارتباط به وسیله پروتکل TCP/IP با میکرو

        سلام،

        به نظر من اول با Telnet تست کنید و بعد Application بنویسید. اگر هم می خواهید با C# کار کنید من این برنامه رو روی کنسول تست کردم شما هم امتحانش کنید.

        Client:

        کد:
        using System;
        using System.Net.Sockets;
        using System.Windows.Forms;
        using System.IO ;
        using System.ComponentModel ;
        using System.Drawing;
        public class Echoclient: Form
        {
         //Define the components...
         private Button b1;
         private TextBox t1,ta;
         TcpClient myclient;
         private NetworkStream networkStream ;
         private StreamReader streamReader ;
         private StreamWriter streamWriter ;
        	
         //constructor initialising... 
         public Echoclient()
         {
          InitializeComponent();
         }
         //main method... 
         public static void Main()
         {
          Echoclient df=new Echoclient();
          df.FormBorderStyle=FormBorderStyle.Fixed3D;
          Application.Run(df);
         }
         public void InitializeComponent()
         {
          b1=new Button();
          b1.Location = new System.Drawing.Point(170,20);
          b1.Size = new System.Drawing.Size (80,40);
          b1.Text="Click Here";
          b1.Click += new System.EventHandler(b1_Click);
         	b1.BackColor = System.Drawing.Color.Transparent ;
        	b1.ForeColor = System.Drawing.Color.Red ;
        	b1.BackgroundImage = Image.FromFile("back4.gif") ;
         	b1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
          b1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8f, System.Drawing.FontStyle.Bold);   
          
          t1=new TextBox();
          t1.Location = new System.Drawing.Point(20,20);
          t1.Size = new System.Drawing.Size (100,100);
          
          ta=new TextBox();
        	ta.Multiline=true;
        	ta.ScrollBars = ScrollBars.Vertical;
          ta.AcceptsReturn = true;
          ta.AcceptsTab = true;
          ta.WordWrap = true;
         	ta.Location = new System.Drawing.Point(20,80);
          this.SuspendLayout();
         	this.Text = "Socket Programming" ;
        	this.MaximizeBox = false;
        	this.BackgroundImage = Image.FromFile("back3.gif") ;
          this.Name = "Form1";
          this.Controls.Add(this.b1);
          this.Controls.Add(this.t1);
          this.Controls.Add(this.ta); 
         	this.Closing+= new CancelEventHandler(form1_closing) ;
         	//connect to the "localhost" at the give port
        	//if you have some other server name then you can use that instead of "localhost"
        	try
          		 {
            	 myclient = new TcpClient("localhost", 1234);
           	 }
          catch
           	 {
            	 Console.WriteLine("Failed to connect to server at {0}:999", "localhost");
            	 return;
           	 }
        		 //get a Network stream from the server
           	 networkStream = myclient.GetStream();
        		 streamReader = new StreamReader(networkStream);
             streamWriter = new StreamWriter(networkStream);
         }
         //User can enter a text in the textbox and on click of the button the message will be sent to the server..
         //then the Client waits and receives the response from the server which is displayed in the textarea..
         private void b1_Click(object sender, EventArgs e)
         {
        	ta.Text="" ;
         	if(t1.Text=="")
        		{
        			MessageBox.Show("Please enter something in the textbox");
        			t1.Focus();
        			return ;
        		}
        	  try
        		 	 {
             		string s;
            	 	streamWriter.WriteLine(t1.Text);
        				Console.WriteLine("Sending Message");
              		streamWriter.Flush();
        				s= streamReader.ReadLine();
        				Console.WriteLine("Reading Message") ;
        				Console.WriteLine(s) ;
        				ta.Text=s;
        				
             	 }
           	 catch(Exception ee)
           	 	 {
            		Console.WriteLine("Exception reading from Server:"+ee	.ToString());
           		 }	
        
         	}
         	public void form1_closing(object o , CancelEventArgs ec)
         	{
           			//close all streams...
        				streamReader.Close() ;
         				streamWriter.Close() ;
           			networkStream.Close();
         	}
        }


        Server:

        کد:
        using System;
        using System.Net.Sockets;
        using System.IO ;
        
        public class Echoserver
        {
        	//entry point of main method....
        	public static void Main()
        	{
        		//TcpListener is listening on the given port...							{
        		TcpListener tcpListener = new TcpListener(1234);
          	tcpListener.Start();   
        		Console.WriteLine("Server Started") ;			
        		//Accepts a new connection...
        		Socket socketForClient = tcpListener.AcceptSocket();			
           	//StreamWriter and StreamReader Classes for reading and writing the data to and fro.
           	//The server reads the meassage sent by the Client ,converts it to upper case and sends it back to the client.
           	//Lastly close all the streams.
            try
            {
           		if (socketForClient.Connected)
          	 	{
            	 	while(true)
            	 	{
            	 	Console.WriteLine("Client connected");
        				NetworkStream networkStream = new NetworkStream(socketForClient);
              	StreamWriter streamWriter = new StreamWriter(networkStream);
         				StreamReader streamReader = new StreamReader(networkStream);
            	 	string line = streamReader.ReadLine();
          	 		Console.WriteLine("Read:" +line);
        				line=line.ToUpper()+ "!";
        				streamWriter.WriteLine(line);
        				Console.WriteLine("Wrote:"+line);
           			streamWriter.Flush() ;    	 		
        				 } 
          	 	}
        		    socketForClient.Close();    	
                Console.WriteLine("Exiting...");
            }
            catch(Exception e)
            {
            	Console.WriteLine(e.ToString()) ;
            }
        	}
        }
        There is nothing so practical as a good theory. — Kurt Lewin, 1951

        دیدگاه

        لطفا صبر کنید...
        X