Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Net;
  12.  
  13.  
  14. namespace WindowsFormsApplication2
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         Timer timwer1 = new Timer();
  19.         TcpClient clientSocket = new TcpClient(); //serwer podstawowy
  20.         TcpClient clientSocket2 = new TcpClient(); //server czujnika odleglosci na porcie 5561
  21.         private static int odleglosc = 0;
  22.  
  23.         /*----Adresy portów i IP----*/
  24.         string IP = "192.168.0.14";
  25.         int Port = 5560; //port podstawowy
  26.         int Port2 = 5561; //port czujnika odleglosci
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.            
  32.         }
  33.  
  34.         private void timer1_Tick(object sender, EventArgs e)
  35.         {
  36.             CzujnikOdleglosci();
  37.            
  38.            
  39.         }
  40.  
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.            
  44.         }
  45.  
  46.         private void button1_Click(object sender, EventArgs e)
  47.         {
  48.             CzujnikOdleglosciPolaczenie();
  49.                        
  50.         }
  51.  
  52.         private void CzujnikOdleglosciTimer()
  53.         {
  54.             timwer1.Interval = 100;
  55.             timwer1.Tick += new EventHandler(timer1_Tick);            
  56.             timer1_Tick(null, null);
  57.             timwer1.Start();
  58.         }
  59.  
  60.         private void CzujnikOdleglosciPolaczenie()
  61.         {
  62.             clientSocket2.Connect(IP, Port2);
  63.             textBox1.Text = textBox1.Text + Environment.NewLine + "Połączono z czujnikiem odległości";
  64.             CzujnikOdleglosciTimer();
  65.             button1.Enabled = false;
  66.         }
  67.  
  68.         private void CzujnikOdleglosci()
  69.         {
  70.             NetworkStream serverStream2 = clientSocket2.GetStream();
  71.             byte[] outStream2 = System.Text.Encoding.UTF8.GetBytes("ddist");
  72.             serverStream2.Write(outStream2, 0, outStream2.Length);
  73.             serverStream2.Flush();
  74.  
  75.             byte[] inStream = new byte[2048];          
  76.             serverStream2.Read(inStream, 0, inStream.Length);
  77.             string returndata2 = Encoding.UTF8.GetString(inStream);
  78.             label1.Text = returndata2;
  79.             var a = Int32.Parse(label1.Text);
  80.            
  81.  
  82.         }
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement