|
|
|
|
|
using CompressorXN_Common;
|
|
|
|
|
|
using CompressorXN_Model.Enums;
|
|
|
|
|
|
using MFSerial_Lib;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using thinger.DataConvertLib;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CompressorXN_Communication.MoFan
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MFLINHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
private static MFSerial mfser = new MFSerial();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 打开LIN设备
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="portName"></param>
|
|
|
|
|
|
public static void OpenLinDevice(string portName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!mfser.Lin_Is_Opened)
|
|
|
|
|
|
{
|
|
|
|
|
|
mfser.Lin_Open(portName, 115200);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
mfser.CanClose();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!mfser.Lin_Is_Opened)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>打开LIN设备异常!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MFLin_DevInfo dev = mfser.MFLinGetInfo();
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>Device Info:");
|
|
|
|
|
|
Console.WriteLine($"NAME: {dev.Name}");
|
|
|
|
|
|
Console.WriteLine("ProtocolVersion: " + dev.ProtocolVersion);
|
|
|
|
|
|
Console.WriteLine("SN: " + dev.SN);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化CAN
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bps"></param>
|
|
|
|
|
|
public static void InitLin(int bps)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool ret;
|
|
|
|
|
|
MFLin_CFG cfg = new MFLin_CFG();
|
|
|
|
|
|
//LIN总线波特率
|
|
|
|
|
|
cfg.Bps = bps;
|
|
|
|
|
|
|
|
|
|
|
|
ret = mfser.LinSet(cfg);
|
|
|
|
|
|
if (ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>Device Set Success");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>Device Set Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
MFLin_IDLength[] mlen = new MFLin_IDLength[2];
|
|
|
|
|
|
mlen[0] = new MFLin_IDLength
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendId)[0],
|
|
|
|
|
|
Len = 8
|
|
|
|
|
|
};
|
|
|
|
|
|
mlen[1] = new MFLin_IDLength
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.ReciveId)[0],
|
|
|
|
|
|
Len = 8
|
|
|
|
|
|
};
|
|
|
|
|
|
ret = mfser.LinSetIdLength(0, mlen);
|
|
|
|
|
|
if (ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>LinSetIdLength Success");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>LinSetIdLength Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送LIN报文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SendLinMsgThread()
|
|
|
|
|
|
{
|
|
|
|
|
|
MFLin_MSG sendMsg = new MFLin_MSG();
|
|
|
|
|
|
while (mfser.Lin_Is_Opened && GlobalVar.CAN_SendMsgFlag)
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Ch = 0;
|
|
|
|
|
|
sendMsg.Id = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendId)[0];
|
|
|
|
|
|
sendMsg.Dlc = ByteArrayLib.GetByteArrayFromInt(GlobalVar.agreementMsgVM.FrameLen)[0];//报文数据长度
|
|
|
|
|
|
switch (GlobalVar.stepEnum)
|
|
|
|
|
|
{
|
|
|
|
|
|
case StepEnum.idling:
|
|
|
|
|
|
string idlingHex = GlobalVar.agreementMsgVM.IdlingHex;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(idlingHex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(idlingHex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case StepEnum.speed1:
|
|
|
|
|
|
string speed1Hex = GlobalVar.agreementMsgVM.Speed1Hex;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(speed1Hex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(speed1Hex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case StepEnum.speed2:
|
|
|
|
|
|
string speed2Hex = GlobalVar.agreementMsgVM.Speed2Hex;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(speed2Hex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(speed2Hex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case StepEnum.speed3:
|
|
|
|
|
|
string speed3Hex = GlobalVar.agreementMsgVM.Speed3Hex;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(speed3Hex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(speed3Hex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case StepEnum.endolead:
|
|
|
|
|
|
string endoleadHex = GlobalVar.agreementMsgVM.EndoleadHex;
|
|
|
|
|
|
if (string.IsNullOrEmpty(endoleadHex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(endoleadHex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case StepEnum.singleDriver:
|
|
|
|
|
|
string singleDriverHex = GlobalVar.agreementMsgVM.SingleDriverHex;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(singleDriverHex))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(singleDriverHex);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool linMasterSendOne = mfser.LinMasterSendOne(sendMsg);
|
|
|
|
|
|
if (!linMasterSendOne)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>linMasterSendOne Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//附加报文
|
|
|
|
|
|
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalSendId))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Ch = 0;
|
|
|
|
|
|
sendMsg.Id = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalSendId)[0];
|
|
|
|
|
|
sendMsg.Dlc = 8;//报文数据长度
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalContent);
|
|
|
|
|
|
linMasterSendOne = mfser.LinMasterSendOne(sendMsg);
|
|
|
|
|
|
if (!linMasterSendOne)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>linMasterSendOne附加报文 Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool linMasterSend = mfser.LinMasterSend(0, ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.ReciveId)[0]);
|
|
|
|
|
|
if (!linMasterSend)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>linMasterSend Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(GlobalVar.agreementMsgVM.SendPeriod);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|