using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mesnac.Action.Feeding.Qingquan { /// /// PlcSchema.xml文档中PLC下传数据项类 -- 对应add节点 /// public class PlcRecipeWriteItem { public PlcRecipeWriteItem() { this.DataFieldName = string.Empty; this.EquipRunName = string.Empty; this.DataType = string.Empty; this.PlcShifting = 0; this.DefaultValue = 0; this.ValueLen = 1; this.Multiply = 1; this.SetValue = null; } /// /// 数据库字段名 /// public string DataFieldName { get; set; } /// /// Plc别名 /// public string EquipRunName { get; set; } /// /// 偏移值 /// public int PlcShifting { get; set; } /// /// 默认值 /// public int DefaultValue { get; set; } /// /// 长度 /// public int ValueLen { get; set; } /// /// 数据处理方式 /// public string DataType { get; set; } /// /// 乘数 /// public int Multiply { get; set; } /// /// 原始值 /// public object SetValue { get; set; } /// /// 写入数据 /// /// public int[] WriteData() { if (string.IsNullOrWhiteSpace(DataType)) { return WriteData_Null(); } if (DataType.Equals("string", StringComparison.CurrentCultureIgnoreCase)) { return WriteData_string(); } if (DataType.Equals("now", StringComparison.CurrentCultureIgnoreCase)) { return WriteData_now(); } return WriteData_Null(); } private int[] MultiplyData(double[] buff) { int[] result = new int[buff.Length]; for (int i = 0; i < buff.Length; i++) { //result[i] = (int)(buff[i] * this.Multiply); result[i] = (int)Math.Round(buff[i] * this.Multiply); } return result; } private int[] WriteData_Null() { double[] buff = new double[1]; double ivalue = 0; if (SetValue != null && SetValue != DBNull.Value && double.TryParse(SetValue.ToString(), out ivalue)) { buff[0] = ivalue; } else { buff[0] = 0.0; } return MultiplyData(buff); } /// /// 把字符串解析为PLC地址块数据 /// /// private int[] WriteData_string() { string ss = string.Empty; if (SetValue != null && SetValue != DBNull.Value) { ss = SetValue.ToString(); } byte[] temp = System.Text.Encoding.Default.GetBytes(ss); //double[] buff = new double[temp.Length / 2]; //for (int i = 0; i < buff.Length; i++) //{ // buff[i] = temp[i * 2] + temp[i * 2 + 1] * 0x100; //} double[] buff = new double[temp.Length / 2 + temp.Length % 2]; for (int i = 0; i < buff.Length; i++) { if (i * 2 + 1 >= temp.Length) { buff[i] = temp[i * 2]; } else { buff[i] = temp[i * 2] + temp[i * 2 + 1] * 0x100; } } return MultiplyData(buff); } private int[] WriteData_now() { string ss = string.Empty; if (SetValue != null && SetValue != DBNull.Value) { ss = SetValue.ToString(); } DateTime dtNow = DateTime.Now; if (!DateTime.TryParse(ss, out dtNow)) { dtNow = DateTime.Now; } double[] buff = new double[] { dtNow.Year, dtNow.Month, dtNow.Day, dtNow.Hour, dtNow.Minute, dtNow.Second }; return MultiplyData(buff); } } }