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.

183 lines
8.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 从接收报文中解析实际值
/// </summary>
/// <param name="data"></param>
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");
}
}
/// <summary>
/// 从接收报文中解析实际值
/// </summary>
/// <param name="data"></param>
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");
}
}
}
}