You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mesnac.Action.Feeding.Qingquan
{
/// <summary>
/// PlcSchema.xml文档中PLC下传数据项类 -- 对应add节点
/// </summary>
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;
}
/// <summary>
/// 数据库字段名
/// </summary>
public string DataFieldName { get; set; }
/// <summary>
/// Plc别名
/// </summary>
public string EquipRunName { get; set; }
/// <summary>
/// 偏移值
/// </summary>
public int PlcShifting { get; set; }
/// <summary>
/// 默认值
/// </summary>
public int DefaultValue { get; set; }
/// <summary>
/// 长度
/// </summary>
public int ValueLen { get; set; }
/// <summary>
/// 数据处理方式
/// </summary>
public string DataType { get; set; }
/// <summary>
/// 乘数
/// </summary>
public int Multiply { get; set; }
/// <summary>
/// 原始值
/// </summary>
public object SetValue { get; set; }
/// <summary>
/// 写入数据
/// </summary>
/// <returns></returns>
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);
}
/// <summary>
/// 把字符串解析为PLC地址块数据
/// </summary>
/// <returns></returns>
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);
}
}
}