C# WPF上位机实现和下位机TCP通讯的方法

这篇文章主要介绍了C# WPF上位机实现和下位机TCP通讯的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

下位机使用北京大华程控电源DH1766-1,上位机使用WPF。实现了电压电流实时采集,曲线显示。上午在公司调试成功,手头没有程控电源,使用TCP服务端模拟。昨天写的TCP服务端正好排上用场。

界面如下:

服务端

服务端实在上篇基础上实现的。需要做如下更改:


while (true)
             {
               try
               {
                 byte[] bufferDate = new byte[1024];
                 int realLen = pSocket.Receive(bufferDate);
                 if (realLen <= 0)
                 {
                   this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "退出\r\n");
                   socketList.Remove(pSocket);
                   //客户端退出的时候会发送一个空字节
                   pSocket.Shutdown(SocketShutdown.Both);
                   pSocket.Close();
                   return;
                 }
                 string receiveStr = Encoding.Default.GetString(bufferDate, 0, realLen);
                 switch (receiveStr)
                 {
                   case "MEAS:VOLTage:ALL?\n":
                     proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
                     break;
                   case "MEAS:CURR:ALL?\n":
                     proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
                     break;
                   default:
                     break;
                 }
                 this.Invoke(addTextDelegate, receiveStr + "from" + pSocket.RemoteEndPoint.ToString() + "\r\n");
               }
               catch (Exception ex)
               {
                 this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "异常退出\r\n");
                 socketList.Remove(pSocket);
                 pSocket.Shutdown(SocketShutdown.Both);
                 pSocket.Close();
                 return;
               }
             }

在While循环中加入:


 switch (receiveStr)
{
  case "MEAS:VOLTage:ALL?\n":
  proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
  break;
  case "MEAS:CURR:ALL?\n":
  proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
  break;
  default:
  break;
}

模拟电源,当收到电压查询时,发送16~25中随机数,由于电源是三个通道的,因此发送三个随机数,用逗号隔开。同样收到电流查询,发送2~5之间的随机数。

完整的客户端源码:


public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      addTextDelegate = new AddTextDelegate(AddText);
    }
    private AddTextDelegate addTextDelegate;
    private List<Socket> socketList = new List<Socket>();

    public delegate void AddTextDelegate(string text);
    private void AddText(string text)
    {
      txtLog.Text += text;
    }

    Random r = new Random();

    private void btnStart_Click(object sender, EventArgs e)
    {
      //参数:寻址方式  传输数据方式 通信协议
      Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

      IPAddress iPAddress = IPAddress.Parse(txtIP.Text);

      //创建EndPoint
      IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, int.Parse(txtPort.Text));

      //绑定端口
      socket.Bind(iPEndPoint);

      //开启侦听
      socket.Listen(10);

      txtLog.Text += "服务启动开启侦听……\r\n";

      Thread thread = new Thread((s) =>
       {
         Socket serSocket = (Socket)s;
         while (true)//不断接收客户端连接
         {
           this.Invoke(addTextDelegate, "服务正在等待客户端连接……\r\n");

           //开始接收客户端的连接
           //阻塞当前线程,等待客户端连接
           //客户端连接上之后,服务端自动生成一个socket和连接的客端通信
           Socket proxSocket = serSocket.Accept();

           this.Invoke(addTextDelegate, "客户端连接成功!\r\n" + proxSocket.RemoteEndPoint.ToString());

           //proxSocket.Send(Encoding.Default.GetBytes("连接成功!"));

           socketList.Add(proxSocket);//当前通信的socket放到集合中

           new Thread(p =>
           {
             Socket pSocket = (Socket)p;
             while (true)
             {
               try
               {
                 byte[] bufferDate = new byte[1024];
                 int realLen = pSocket.Receive(bufferDate);

                 if (realLen <= 0)
                 {
                   this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "退出\r\n");

                   socketList.Remove(pSocket);
                   //客户端退出的时候会发送一个空字节
                   pSocket.Shutdown(SocketShutdown.Both);
                   pSocket.Close();

                   return;
                 }
                 string receiveStr = Encoding.Default.GetString(bufferDate, 0, realLen);
                 switch (receiveStr)
                 {
                   case "MEAS:VOLTage:ALL?\n":
                     proxSocket.Send(Encoding.Default.GetBytes(r.Next(16,25).ToString()+ ","+r.Next(16, 25).ToString()+","+ r.Next(16, 25).ToString()));
                     break;
                   case "MEAS:CURR:ALL?\n":
                     proxSocket.Send(Encoding.Default.GetBytes(r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString() + "," + r.Next(2, 5).ToString()));
                     break;
                   default:
                     break;
                 }
                 this.Invoke(addTextDelegate, receiveStr + "from" + pSocket.RemoteEndPoint.ToString() + "\r\n");
               }
               catch (Exception ex)
               {
                 this.Invoke(addTextDelegate, pSocket.RemoteEndPoint.ToString() + "异常退出\r\n");

                 socketList.Remove(pSocket);
                 pSocket.Shutdown(SocketShutdown.Both);
                 pSocket.Close();
                 return;
               }
             }
           })
           { IsBackground = true }.Start(proxSocket);
         }
       });
      thread.IsBackground = true;
      thread.Start(socket);
      
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
      string str = txtSend.Text;
      byte[] data = Encoding.Default.GetBytes(str);
      foreach (var socket in socketList)
      {
        if (socket != null && socket.Connected)
        {
          socket.Send(data);
        }
      }
    }
  }

上位机实现客户端功能。具体如下:

1、字段和属性


public readonly IPEndPoint TagetIPEP;

public bool IsConnected { get; set; } = false;

private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { /*ReceiveTimeout=1000,SendTimeout=1000*

本文标题为:C# WPF上位机实现和下位机TCP通讯的方法

基础教程推荐