Ejemplo n.º 1
0
 //
 //摘要:
 //     获得从主机发送的数据
 private void GetMessageFromMain(NetworkStream networkStream, byte[] vs)
 {
     try
     {
         if ((networkStream.Read(vs, 0, vs.Length)) > 0)
         {
             DataIsAll dataIsAll = new DataIsAll(vs);
             if (dataIsAll.modeIorII == 0X00)//模式1
             {
                 this.Invoke(new Action(() =>
                 {
                     textBox1.AppendText($"{dataIsAll.carAddress}" + "号车-->");
                     textBox1.AppendText($"经度:{dataIsAll.longitude}" + " ");
                     textBox1.AppendText($"纬度{dataIsAll.latitude}" + " ");
                     textBox1.AppendText($"距离信标的距离{dataIsAll.destination1}" + "米");
                     textBox1.AppendText("\r\n");
                 }));
             }
             else if (dataIsAll.modeIorII == 0X01)
             {
                 this.Invoke(new Action(() =>
                 {
                     textBox1.AppendText($"{dataIsAll.carAddress}" + "号车-->");
                     textBox1.AppendText($"经度:{dataIsAll.longitude}" + " ");
                     textBox1.AppendText($"纬度{dataIsAll.latitude}" + "\r\n");
                     textBox1.AppendText($"距离信标1的距离{dataIsAll.destination1}" + "米" + "\r\n");
                     textBox1.AppendText($"距离信标2的距离{dataIsAll.destination2}" + "米" + "\r\n");
                     textBox1.AppendText($"距离信标3的距离{dataIsAll.destination3}" + "米" + "\r\n");
                     textBox1.AppendText($"距离信标4的距离{dataIsAll.destination4}" + "米" + "\r\n");
                 }));
             }
         }
     }
     catch
     {
         this.Invoke(new Action(() => { textBox1.AppendText("断开与服务器的连接"); }));
     }
 }
Ejemplo n.º 2
0
 async private void ConnectWithMain_Click(object sender, EventArgs e)
 {
     await Task.Run(() =>
     {
         try
         {
             tcpClient           = new TcpClient();
             string carText      = "";
             string IpNameText   = "";
             string PortNameText = "";
             Int16 car           = 0;
             this.Invoke(new Action(() =>
             {
                 try
                 {
                     IpNameText   = IpName.Text;
                     PortNameText = PortName.Text;
                     carText      = carName.Text;//得到车子的编号
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
             }));
             try
             {
                 car = Int16.Parse(carText);
                 IPAddress iPAddress = IPAddress.Parse(IpNameText);
                 int Port            = int.Parse(PortNameText);
                 this.Invoke(new Action(() => { textBox1.AppendText("连接中......\r\n"); ConnectWithMain.Enabled = false; }));
                 tcpClient.Connect(iPAddress, Port);
                 this.Invoke(new Action(() => { textBox1.AppendText("和主机连接成功\r\n"); BreakLink.Enabled = true; }));
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
                 this.Invoke(new Action(() => { textBox1.AppendText("连接失败!!!!!\r\n"); ConnectWithMain.Enabled = true; }));
             }
             if (tcpClient.Connected)
             {
                 networkStream = tcpClient.GetStream();
                 car.SendDataToMain(networkStream);
                 byte[] vs = new byte[100];
                 int size  = 0;
                 while (tcpClient.Connected)
                 {
                     if ((size = networkStream.Read(vs, 0, vs.Length)) > 0)
                     {
                         DataIsAll dataIsAll = new DataIsAll(vs);
                         this.Invoke(new Action(() =>
                         {
                             textBox1.AppendText($"{dataIsAll.carAddress}" + "号车-->");
                             textBox1.AppendText($"经度:{dataIsAll.longitude}" + " ");
                             textBox1.AppendText($"纬度{dataIsAll.latitude}" + " ");
                             textBox1.AppendText("\r\n");
                         }));
                     }
                 }
             }
         }
         catch
         {
             this.Invoke(new Action(() => { textBox1.AppendText("与服务器断开连接\r\n"); }));
         }
     });
 }