S7Helper.cs 7.41 KB
using HHECS.Bll;
using HHECS.Model;
using S7.Net;
using S7.Net.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HHECS.OPC
{
    public class S7Helper
    {
        /// <summary>
        /// 将DataItem中的value强制转换后赋值给value,用于读
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="props"></param>
        /// <returns></returns>
        public static BllResult ConvertPLCToNet(List<EquipmentProp> props, List<EquipmentTypePropTemplate> templates)
        {
            try
            {
                var temp = props.Join(templates, t => t.EquipmentTypePropTemplateId, i => i.Id, (prop, template) => new { prop, template }).ToList();

                foreach (var item in temp)
                {
                    switch (item.template.DataType)
                    {
                        case "BYTE":
                        case "DWORD":
                        case "BOOL":
                        case "WORD": item.prop.Value = item.prop.DataItem.Value.ToString(); break;
                        case "CHAR": item.prop.Value = ConverHelper.ASCIIToString((short[])item.prop.DataItem.Value).Trim().Replace("\u0003", ""); break;
                        default:
                            item.prop.Value = item.prop.DataItem.Value.ToString();
                            break;
                    }
                }
                return BllResultFactory.Sucess("成功");
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"数据类型转换异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 将属性value中的值强制转换赋值给dataitem,用于向PLC写入
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="props"></param>
        /// <param name="templates"></param>
        /// <returns></returns>
        public static BllResult ConvertNetToPLC(List<EquipmentProp> props,List<EquipmentTypePropTemplate> templates)
        {
            try
            {
                var temp = props.Join(templates, t => t.EquipmentTypePropTemplateId, i => i.Id, (prop, template) => new { prop, template }).ToList();
                foreach (var item in temp)
                {
                    switch (item.template.DataType)
                    {
                        case "INT":
                            item.prop.DataItem.Value = Convert.ToInt16(item.prop.Value);
                            break;
                        case "DINT":
                            item.prop.DataItem.Value = Convert.ToInt32(item.prop.Value);
                            break;
                        case "WORD":
                            item.prop.DataItem.Value = Convert.ToUInt16(item.prop.Value);
                            break;
                        case "DWORD":
                            item.prop.DataItem.Value = Convert.ToUInt32(item.prop.Value);
                            break;
                        case "CHAR":
                            item.prop.DataItem.Value = ConverHelper.StringToASCII(item.prop.Value);
                            break;
                        default:
                            item.prop.DataItem.Value = item.prop.Value.BinStringToByte();
                            break;
                    }
                }
                return BllResultFactory.Sucess("成功");
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"数据类型转换异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 从PLC读取值,读取的值赋值给value属性
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="props"></param>
        /// <param name="splitCount"></param>
        /// <param name="templates"></param>
        /// <returns></returns>
        public static BllResult PlcSplitRead(Plc plc, List<EquipmentProp> props, int splitCount, List<EquipmentTypePropTemplate> templates)
        {
            if (props.Count == 0)
            {
                return BllResultFactory.Error("待读集合无数据"); 
            }
            try
            {
                var tempList = props.Select(t => t.DataItem).ToList();
                int preIndex = 0;
                int currentIndex = splitCount;
                plc.Open();
                while (preIndex < tempList.Count)
                {
                    List<DataItem> temp1 = null;
                    if (preIndex < tempList.Count)
                    {
                        temp1 = tempList.Skip(preIndex).Take(currentIndex).ToList();
                        preIndex += currentIndex;
                    }
                    else
                    {
                        temp1 = tempList.Skip(preIndex).ToList();
                    }
                    plc.ReadMultipleVars(temp1);
                }
                //赋值属性
                var result = ConvertPLCToNet(props, templates);
                if (result.Success)
                {
                    return BllResultFactory.Sucess("去读成功.");
                }
                else
                {
                    return BllResultFactory.Error($"读取失败:{result.Msg}");
                }
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"读取PLC值出错:{ex.Message}");
            }

        }

        /// <summary>
        /// 写入PLC值,待写的值为value属性
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="props"></param>
        /// <param name="splitCount"></param>
        /// <param name="templates"></param>
        /// <returns></returns>
        public static BllResult PlcSplitWrite(Plc plc, List<EquipmentProp> props, int splitCount, List<EquipmentTypePropTemplate> templates)
        {
            if (props.Count == 0)
            {
                return BllResultFactory.Error("待写集合无数据");
            }
            //先转换
            var result = ConvertNetToPLC(props, templates);
            if (result.Success)
            {
                try
                {
                    var tempList = props.Select(t => t.DataItem).ToList();
                    int preIndex = 0;
                    int currentIndex = splitCount;
                    plc.Open();
                    while (preIndex < tempList.Count)
                    {
                        List<DataItem> temp1 = null;
                        if (preIndex < tempList.Count)
                        {
                            temp1 = tempList.Skip(preIndex).Take(currentIndex).ToList();
                            preIndex += currentIndex;
                        }
                        else
                        {
                            temp1 = tempList.Skip(preIndex).ToList();
                        }
                        plc.Write(temp1.ToArray());
                    }
                    return BllResultFactory.Sucess("写入成功");
                }
                catch (Exception ex)
                {
                    return BllResultFactory.Error($"写入出现异常:{ex.Message}");
                }
            }
            else
            {
                return BllResultFactory.Error($"写入失败,数据转换异常:{result.Msg}");
            }
           
        }

    }
}