using CompressorXN_Common;
using CompressorXN_Communication.MyPlc;
using CompressorXN_Communication.TuMos;
using CompressorXN_Log;
using CompressorXN_Model.Enums;
using System;
namespace CompressorXN_Communication
{
public class GetCANReciveValHelper
{
///
/// 从接收报文中解析实际值
///
///
public static void GetReciveVal(byte[] data)
{
try
{
MsgHelper mFMsgHelper = new MsgHelper(data, 0, 8);
int reciveVal = 0;
int startBit = GlobalVar.agreementMsgVM.ReciveDetailRules.StartBit;
int len = GlobalVar.agreementMsgVM.ReciveDetailRules.Len;
decimal offset = GlobalVar.agreementMsgVM.ReciveDetailRules.Offset;
decimal precision = GlobalVar.agreementMsgVM.ReciveDetailRules.Precision;
switch (GlobalVar.agreementMsgVM.MsgFormat)
{
case "Intel":
reciveVal = (int)mFMsgHelper.GetIntelLsb(startBit, len);
break;
case "Motorola(LSB)":
reciveVal = (int)mFMsgHelper.GetMotoralaLsb(startBit, len);
break;
case "Motorola(MSB)":
reciveVal = (int)mFMsgHelper.GetMotoralaMsb(startBit, len);
break;
default:
break;
}
//实际转速=反馈值*精度+偏移量
reciveVal = Convert.ToInt32((reciveVal + offset) * precision);
GlobalVar.RealSpeed = reciveVal;
Console.WriteLine("实时转速=" + reciveVal);
//写入实时转速
OmronCIPHelper.SingleWriteValToPlc("D1410", "float", reciveVal);
int diff = 50;
switch (GlobalVar.stepEnum)
{
case StepEnum.speed1:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed1Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed1);
if (difference < diff)
{
Console.WriteLine("一段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1226", "float", 1);
// GlobalVar.stepEnum = StepEnum.speed2;
}
}
break;
case StepEnum.speed2:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed2Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed2);
if (difference < diff)
{
Console.WriteLine("二段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1230", "float", 1);
}
}
break;
case StepEnum.speed3:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed3Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed3);
if (difference < diff)
{
Console.WriteLine("三段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1234", "float", 1);
}
}
break;
}
}
catch (Exception ex)
{
LogHelper.Error(ex, "GetReciveVal");
}
}
///
/// 从接收报文中解析实际值
///
///
public static void GetFDReciveVal(byte[] data)
{
try
{
MsgHelper mFMsgHelper = new MsgHelper(data, 0, 64);
int reciveVal = 0;
int startBit = GlobalVar.agreementMsgVM.ReciveDetailRules.StartBit;
int len = GlobalVar.agreementMsgVM.ReciveDetailRules.Len;
decimal offset = GlobalVar.agreementMsgVM.ReciveDetailRules.Offset;
decimal precision = GlobalVar.agreementMsgVM.ReciveDetailRules.Precision;
switch (GlobalVar.agreementMsgVM.MsgFormat)
{
case "Intel":
reciveVal = (int)mFMsgHelper.GetIntelLsb(startBit, len);
break;
case "Motorola(LSB)":
reciveVal = (int)mFMsgHelper.GetMotoralaLsb(startBit, len);
break;
case "Motorola(MSB)":
reciveVal = (int)mFMsgHelper.GetMotoralaMsb(startBit, len);
break;
default:
break;
}
//实际转速=反馈值*精度+偏移量
reciveVal = Convert.ToInt32((reciveVal + offset) * precision);
GlobalVar.RealSpeed = reciveVal;
Console.WriteLine("实时转速=" + reciveVal);
//写入实时转速
OmronCIPHelper.SingleWriteValToPlc("D1410", "float", reciveVal);
int diff = 50;
switch (GlobalVar.stepEnum)
{
case StepEnum.speed1:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed1Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed1);
if (difference < diff)
{
Console.WriteLine("一段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1226", "float", 1);
// GlobalVar.stepEnum = StepEnum.speed2;
}
}
break;
case StepEnum.speed2:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed2Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed2);
if (difference < diff)
{
Console.WriteLine("二段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1230", "float", 1);
}
}
break;
case StepEnum.speed3:
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.Speed3Hex))
{
//两数相减绝对值
int difference = Math.Abs(reciveVal - GlobalVar.agreementMsgVM.Speed3);
if (difference < diff)
{
Console.WriteLine("三段升速到达目标转速");
//到达目标转速后,反馈PLC
OmronCIPHelper.SingleWriteValToPlc("D1234", "float", 1);
}
}
break;
}
}
catch (Exception ex)
{
LogHelper.Error(ex, "GetReciveVal");
}
}
}
}