C#实现数据采集系统-modbustcp数据通知

实现数据推送

在ModbusTcp模块中已经能够获取到正确的plc值,接下来做的就是如何让外部调用的程序获取到相关的数据,主要就是两种方法:通过对象属性进行共享;通过事件进行推送

方法一:通过类属性共享

RegisterPoint类中增加public object Value { get; set; }value属性来存储当前实时值,外部调用程序直接读点位集合中对象的Value值。

 public class RegisterPoint
 {
    
    
     public string UID {
    
     get; set; }

     public string Name {
    
     get; set; }

     /// <summary>
     /// 数据类型
     /// </summary>
     public Type Type {
    
     get; set; }

     /// <summary>
     /// 寄存器类型
     /// </summary>
     public string RegisterType {
    
     get; set; }

     public int Address {
    
     get; set; }

     public int Length {
    
     get; set; }

     /// <summary>
     /// 当前值
     /// </summary>
     public object Value {
    
     get; set; }
 }

赋值

  void DealData(RegisterPoint point, byte[] bytes)
  {
    
    
      //处理返回数据
      var funcCode = ReadFuncCodes[point.RegisterType];
      object value;
      //...处理过程
      
      //赋值
      point.Value = value;
      //   Console.WriteLine($"Point:{point.UID}-->Value:{value}");
  }

使用,在调用的地方获取Value值

while (true)
{
    
    
    foreach (RegisterPoint point in points)
    {
    
    
        Console.WriteLine($"Point:{
      
      point.UID}-->Value:{
      
      point.Value}");
    }
    Thread.Sleep(1000);
}

方法二:事件通知

添加一个事件,参数为点位类型和值

  public event Action<RegisterPoint, object> ValueUpdated;

在数据处理中调用,推送值

void DealData(RegisterPoint point, byte[] bytes)
  {
    
    
      //处理返回数据
      var funcCode = ReadFuncCodes[point.RegisterType];
      object value;
      //...处理过程
      
      //赋值
        ValueUpdated?.Invoke(point, value);
      //   Console.WriteLine($"Point:{point.UID}-->Value:{value}");
  }

在调用方,订阅事件使用

    //Main
    ModbusTcp modbusTcp = new ModbusTcp(deviceLink, points);
    modbusTcp.DoMonitor();
    modbusTcp.ValueUpdated += ModbusTcp_ValueUpdated;
//事件方法
private static void ModbusTcp_ValueUpdated(RegisterPoint point, object value)
{
    
    
    Console.WriteLine($"Point:{
      
      point.UID}-->Value:{
      
      value}");
}

代码

Program.cs

    internal class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Console.WriteLine("Hello, World!");

            DeviceLink deviceLink = new DeviceLink()
            {
    
    
                Ip = "127.0.0.1",
                Port = 502,
                SlaveID = 1
            };
            //Coil
            //DiscreteInput
            //HoldingRegister
            //InputRegister
            List<RegisterPoint> points = new List<RegisterPoint>();

            points.Add(
                new RegisterPoint()
                {
    
    
                    UID = "001",
                    RegisterType = "HoldingRegister",
                    Address = 0,
                    Type = typeof(short),
                    Length = 1,
                }
            );

            points.Add(
                new RegisterPoint()
                {
    
    
                    UID = "002",
                    RegisterType = "HoldingRegister",
                    Address = 1,
                    Type = typeof(ushort),
                    Length = 1,
                }
            );

            points.Add(
                new RegisterPoint()
                {
    
    
                    UID = "003",
                    RegisterType = "HoldingRegister",
                    Address = 2,
                    Type = typeof(int),
                    Length = 2,
                }
            );

            ModbusTcp modbusTcp = new ModbusTcp(deviceLink, points);
            modbusTcp.DoMonitor();
            modbusTcp.ValueUpdated += ModbusTcp_ValueUpdated;
            //获取Value值
            //while (true)
            //{
    
    
            //    foreach (RegisterPoint point in points)
            //    {
    
    
            //        Console.WriteLine($"Point:{point.UID}-->Value:{point.Value}");
            //    }
            //    Thread.Sleep(1000);
            //}

            Console.ReadKey();
        }
	//事件推送
        private static void ModbusTcp_ValueUpdated(RegisterPoint point, object value)
        {
    
    
            Console.WriteLine($"Point:{
      
      point.UID}-->Value:{
      
      value}");
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_39427511/article/details/140726707