亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#WPF上位機實現和下位機TCP通訊的示例分析

發布時間:2021-06-24 14:25:58 來源:億速云 閱讀:214 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關C#WPF上位機實現和下位機TCP通訊的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

界面如下:

C#WPF上位機實現和下位機TCP通訊的示例分析

服務端

C#WPF上位機實現和下位機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*/};

private Thread recListenThread;

public string ReceiveStr { get; set; }

public byte[] ReceiveByte { get; set; }

TagetIPEP是服務器地址和端口。

IsConnected是連接的狀態,這個比較重要,在發送和接收時,都要更加IsConnected進行,并更新IsConnected。

Socket用于和客戶端通訊。

recListenThread是監聽客戶端消息的線程。

ReceiveStr和ReceiveByte用來存儲客戶端發來的消息。

2、方法函數連接方法:

public bool Connect()
    {
      try
      {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        {
          //ReceiveTimeout = 1000,
          //SendTimeout=1000
        };

        //IAsyncResult connResult = socket.BeginConnect(TagetIPEP.Address, TagetIPEP.Port, null, null);
        //connResult.AsyncWaitHandle.WaitOne(5000, true);
        //if (connResult.IsCompleted)
        //{
        socket.Connect(TagetIPEP.Address, TagetIPEP.Port);
        IsConnected = true;
          //開啟接收監聽

          recListenThread = new Thread(() =>
          {
            while (true)
            {
              try
              {
                ReceiveByte = new byte[1024];
                int realLen = socket.Receive(ReceiveByte);
                ReceiveStr = Encoding.Default.GetString(ReceiveByte, 0, realLen);
                ReceiveEvent();
                if (realLen <= 0)
                {
                  if (socket != null && socket.Connected)
                  {
                    //服務器退出
                    IsConnected = false;
                    Log.WriteLog("服務器退出!");
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                    MessageBox.Show("連接斷開!");
                  }
                  return;
                }
              }
              catch (Exception ex)
              {
                if (socket != null && socket.Connected)
                {
                  IsConnected = false;
                  Log.WriteLog("服務器異常退出!", ex);
                  socket.Shutdown(SocketShutdown.Both);
                  socket.Close();
                }
                return;
              }
            }
          })
          { IsBackground = true };
          recListenThread.Start();
          return true;
        //}

      }
      catch (Exception ex)
      {
        Log.WriteLog(TagetIPEP.Address + "連接失敗", ex);
      }
      return false;
    }

連接函數返回值為bool類型,根據返回值判斷連接是否成功連接。這里每次連接都實例化了一個socket,因為在執行socket.close()后,重新打開會失敗,而斷線重連會經常用到,沒有找到更好的方法,干脆重新實例化socket。連接成功后,開啟監聽服務端消息的線程。這里使用了一個ReceiveEvent()事件,在接收到消息時會觸發這個事件,刷新UI界面。

發送方法:

public bool Send(string msg)
    {
      byte[] sendMsg = Encoding.Default.GetBytes(msg);
      if (sendMsg.Length > 0&&IsConnected)
      {
        if (socket != null && socket.Connected)
        {
          try
          {
            socket.Send(sendMsg);
            return true;
          }
          catch (Exception ex)
          {
            IsConnected = false;
            Log.WriteLog("發送數據失敗,目標地址" + TagetIPEP.Address, ex);
          }
        }
      }

      return false;

    }

關閉方法:

public void Close()
    {
      if (socket != null && socket.Connected)
      {
        IsConnected = false;
        recListenThread.Abort();
        Log.WriteLog("關閉連接!");
        socket.Shutdown(SocketShutdown.Both);
        socket.Close();
      }
    }

在出現異常時調用

消息接收事件:

public event Action ReceiveEvent;

每次接收消息時觸發,獲取屬性ReceiveStr和ReceiveByte的值,刷新UI界面。

完整代碼:

public class TCPClient
  {
    public TCPClient(/*IPEndPoint localIPEP,*/IPEndPoint targetIPEP)
    {
      //socket.Bind(localIPEP);
      TagetIPEP = targetIPEP;
      
    }

    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*/};

    public bool Connect()
    {
      try
      {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        {
          //ReceiveTimeout = 1000,
          //SendTimeout=1000
        };

        //IAsyncResult connResult = socket.BeginConnect(TagetIPEP.Address, TagetIPEP.Port, null, null);
        //connResult.AsyncWaitHandle.WaitOne(5000, true);
        //if (connResult.IsCompleted)
        //{
        socket.Connect(TagetIPEP.Address, TagetIPEP.Port);
        IsConnected = true;
          //開啟接收監聽

          recListenThread = new Thread(() =>
          {
            while (true)
            {
              try
              {
                ReceiveByte = new byte[1024];
                int realLen = socket.Receive(ReceiveByte);
                ReceiveStr = Encoding.Default.GetString(ReceiveByte, 0, realLen);
                ReceiveEvent();
                if (realLen <= 0)
                {
                  if (socket != null && socket.Connected)
                  {
                    //服務器退出
                    IsConnected = false;
                    Log.WriteLog("服務器退出!");
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                    MessageBox.Show("連接斷開!");
                  }
                  return;
                }
              }
              catch (Exception ex)
              {
                if (socket != null && socket.Connected)
                {
                  IsConnected = false;
                  Log.WriteLog("服務器異常退出!", ex);
                  socket.Shutdown(SocketShutdown.Both);
                  socket.Close();
                }
                return;
              }
            }
          })
          { IsBackground = true };
          recListenThread.Start();
          return true;
        //}

      }
      catch (Exception ex)
      {
        Log.WriteLog(TagetIPEP.Address + "連接失敗", ex);
      }
      return false;
    }

    public bool Send(string msg)
    {
      byte[] sendMsg = Encoding.Default.GetBytes(msg);
      if (sendMsg.Length > 0&&IsConnected)
      {
        if (socket != null && socket.Connected)
        {
          try
          {
            socket.Send(sendMsg);
            return true;
          }
          catch (Exception ex)
          {
            IsConnected = false;
            Log.WriteLog("發送數據失敗,目標地址" + TagetIPEP.Address, ex);
          }
        }
      }

      return false;

    }

    public event Action ReceiveEvent;

    public string ReceiveStr { get; set; }

    public byte[] ReceiveByte { get; set; }

    public void Close()
    {
      if (socket != null && socket.Connected)
      {
        IsConnected = false;
        recListenThread.Abort();
        Log.WriteLog("關閉連接!");
        socket.Shutdown(SocketShutdown.Both);
        socket.Close();
      }
    }

    private Thread recListenThread;

  }

前臺調用,聲明Timer定時器,每個一秒觸發一次。觸發事件如下:

private string flag = "";
    private void QueryTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      Now = DateTime.Now;

      if (!tcp.Send("MEAS:VOLTage:ALL?\n"))
      {
        queryTimer.Enabled = false;
        StartContent = "開始";
        ConnContent = "連接";
        tcp.IsConnected = false;
        MessageBox.Show("查詢失敗!");
        return;
      }
      flag = "V";
      Thread.Sleep(50);

      if (!tcp.Send("MEAS:CURR:ALL?\n"))
      {
        queryTimer.Enabled = false;
        StartContent = "開始";
        ConnContent = "連接";
        tcp.IsConnected = false;
        MessageBox.Show("查詢失敗!");
        return;
      }
      flag = "C";

      #region 測試
      //angle += 18;
      //if (angle > 360)
      //{
      //  angle = 18;
      //}

      #endregion
    }

刷新UI界面的事件如下:

private void Tcp_ReceiveEvent()
    {
      Task.Run(() =>
      {
        Application.Current.Dispatcher.Invoke(() =>
        {
          RemoteIP = tcp.TagetIPEP.ToString();
          switch (flag)
          {
            case "V":
              VoltValue = Math.Round(Convert.ToDouble(tcp.ReceiveStr.Split(',')[0]), 3);
              break;
            case "C":
              CurrentValue = Math.Round(Convert.ToDouble(tcp.ReceiveStr.Split(',')[0]), 3);
              break;
            default:
              break;
          }

          #region 測試
          //VoltValue = Math.Round(Math.Sin((angle) * pi / 180) * 16 + 16, 3);
          //CurrentValue = Math.Round(Math.Sin((angle) * pi / 180) * 2.5 + 2.5, 3);
          #endregion

          VoltValues.Add(VoltValue);
          CurrentValues.Add(CurrentValue);

          if (VoltValues.Count > 30)
          {
            VoltValues.RemoveAt(0);
            CurrentValues.RemoveAt(0);
          }
        });
      });
    }

感謝各位的閱讀!關于“C#WPF上位機實現和下位機TCP通訊的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

屏山县| 灌阳县| 连云港市| 富蕴县| 永济市| 香河县| 疏附县| 邵东县| 永州市| 哈尔滨市| 文山县| 蓬莱市| 大城县| 冷水江市| 耿马| 高青县| 九寨沟县| 溧阳市| 县级市| 连城县| 舟曲县| 昌江| 亳州市| 宁津县| 泗水县| 华安县| 青阳县| 额尔古纳市| 嘉鱼县| 德安县| 枣强县| 资阳市| 怀宁县| 昭苏县| 从江县| 通化市| 会宁县| 芒康县| 磴口县| 嘉祥县| 萨嘎县|