下位机使用北京大华程控电源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*/};
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();

            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(]), );
                            break;
                        case "C":
                            CurrentValue = Math.Round(Convert.ToDouble(tcp.ReceiveStr.Split(]), );
                            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);

                    )
                    {
                        VoltValues.RemoveAt();
                        CurrentValues.RemoveAt();
                    }
                });
            });
        }

C# WPF上位机实现和下位机TCP通讯的更多相关文章

  1. C#做上位机软件——绘图并传输给下位机

    拿到任务之后首先分成了几个部分: 1.绘图.学习了GDI+ 2.图片保存. 3.将图片转换成byte[].由于使用Socket通信,只能传输byte[]数据,所以这一步是向下位机传输的关键. 相应地, ...

  2. WPF开发汽车采样机上位机软件

    由于项目需要,需开发同一套汽车.火车.皮带采样机的上位机软件. 看过之前的上位机软件,老版本都是DelPhi.VB开发,稍微新语言开发的是采用winform开发.要不就是使用组态软件. Delphi语 ...

  3. 嵌入式Linux学习笔记(六) 上位机QT界面实现和串口通讯实现

    目录 (1).参考资料 (2).QT界面布局实现 (3).数据和操作逻辑 在上一章我们实现了下位机的协议制定,并通过串口通讯工具完成了对设备内外设(LED)的状态修改,下面就要进行上位机软件的实现了( ...

  4. vb配置下位机CAN寄存器小结

    2011-12-14 18:44:32 效果图 1,完成设计(由于没有eeprom等存储设备,所以每次上电后需要通过串口配置某些寄存器).在设计中,列出技术评估难度,并进行尝试,参看<我的设计& ...

  5. "废物利用"也抄袭——“完全”DIY"绘图仪"<二、下位机程序设计>

    就不说怎么组装了吧,一把辛酸泪.说程序,因为这有两把辛酸泪……一把给下位机的C代码一把为了VB.NET的图像处理……不过就上上一篇说的,它们可以正确运行了,并且今天克服了Arduino上电过程中步进电 ...

  6. STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发

    这里我主要说一下如何做一个USB下位机,这里主要分3部分:1.建立工程:2.添加报文描述符:3.数据的传输.这里就不讲USB的理论知识了,有想要了解的自行百度一下就可以了. 建立工程:工程建立参考:h ...

  7. 转载 STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发

    STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发  本文转载自 https://www.cnblogs.com/xingboy/p/9913963.html 这里我主要说一 ...

  8. bootargs说明 和 下位机静态ip的设置

    setenv bootargs root=/dev/nfs       nfsroot=192.168.1.8:/opt/rootfs      ip=192.168.1.110:192.168.1. ...

  9. C#使用struct直接转换下位机数据

    编写上位机与下位机通信的时候,涉及到协议的转换,比较多会使用到二进制.传统的方法,是将数据整体获取到byte数组中,然后逐字节对数据进行解析.这样操作工作量比较大,对于较长数据段更容易计算位置出错. ...

随机推荐

  1. js中的排序方法

    一.冒泡排序 var  arr=[22,1,33,19,77]; function  bubbleSort(arr){ for(var i=0;i<arr.length-1;i++){ for( ...

  2. Oracle 12 Rman增量备份

    增量备份 增量备份主要作用是仅复制自上次备份以来已更改的数据块.您可以使用RMAN创建数据文件,表空间或整个数据库的增量备份. 将增量备份作为策略的一部分的主要原因是: 用于基于增量更新备份的策略,其 ...

  3. 页面适配的小栗子 - github

    我模拟了一个QQ音乐的radiostation页面,用了媒体查询以及流式布局,页面大部分内容是js根据json生成的,使用了less来编写css样式表. 下面是仓库地址,感兴趣的小伙伴可以打开看看,将 ...

  4. 0x11栈之火车进栈

    参考<算法竞赛进阶指南>p.49 题目链接:https://www.acwing.com/problem/content/description/131/ 递推与递归的宏观描述 对于一个待 ...

  5. sqlserver存储过程的使用

    参考网址:https://www.cnblogs.com/chaoa/articles/3894311.html(存储过程)    https://www.cnblogs.com/selene/p/4 ...

  6. 最近在写h5的页面,发现有一款框架还不错给大家推荐一下

    wui 说一下自己写项目的体会吧,自我感觉wui还不错,能够让自己很快地把页面布局好,而且里面一些js效果也不用自己去手写jQuery代码很方便,下面让我们一起来认识一下wui这个框架吧 1,acco ...

  7. python 2.7 读写 opc数据

    运行环境 python2.7+window server2008+keep server 1.安装OpenOPC a> 下载 OpenOPC-1.3.1.win32-py2.7 (1).exe  ...

  8. jenkins构建成功,但war包没有发布到tomcat下

    如题,jenkins构建成功,在jenkins的workspace中有生成的war包,但没有发布到tomcat的webapps目录. 构建日志 找了很多原因发现应该还是项目相对路径不对导致的,我的wa ...

  9. 又谈T检验

    今天有同学的论文被指摘了,就是又用了T检验,又用了ANOVA,reviewer直接说用ANOVA就行了.所以回想下了T检验. 简而言之,T检验就是用来比较均值的,样本均值和已知总体均值是否有差异.(也 ...

  10. @mentions for Users with ActionText; 使用Tribute.js库

    git clone从https://github.com/chentianwei411/at-mentions-with-action-text 先fork下来,然后拷贝https连接,最后在term ...