|
|
|
|
|
using CompressorXN_Common;
|
|
|
|
|
|
using CompressorXN_Model.Enums;
|
|
|
|
|
|
using MFSerial_Lib;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using thinger.DataConvertLib;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CompressorXN_Communication.MoFan
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MFCANHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
private static MFSerial mfser = new MFSerial();
|
|
|
|
|
|
|
|
|
|
|
|
public static void OpenCanDevice(string portName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!mfser.Can_Is_Opened)
|
|
|
|
|
|
{
|
|
|
|
|
|
mfser.Can_Open(portName, 115200);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
mfser.CanClose();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!mfser.Can_Is_Opened)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>打开CAN设备异常!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MFCan_DevInfo dev = mfser.MFCanGetInfo();
|
|
|
|
|
|
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 InitCan(int bps)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool ret;
|
|
|
|
|
|
MFCan_CFG cfg = new MFCan_CFG();
|
|
|
|
|
|
//CAN总线波特率
|
|
|
|
|
|
cfg.NormalBps = bps;
|
|
|
|
|
|
|
|
|
|
|
|
ret = mfser.CanSet(cfg);
|
|
|
|
|
|
if (ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>Device Set Success");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}>>>>Device Set Failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static Thread CANSendMsgThread = null;
|
|
|
|
|
|
private static Thread CANReadMsgThread = null;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 启动线程
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void CAN_StartTask()
|
|
|
|
|
|
{
|
|
|
|
|
|
CANSendMsgThread = new Thread(new ThreadStart(SendCanMsgThread));
|
|
|
|
|
|
CANReadMsgThread = new Thread(new ThreadStart(ReadCanMsgThread));
|
|
|
|
|
|
|
|
|
|
|
|
//启动接收数据线程
|
|
|
|
|
|
GlobalVar.CAN_ReadMsgFlag = true;
|
|
|
|
|
|
CANReadMsgThread.Start();
|
|
|
|
|
|
|
|
|
|
|
|
//启动发送数据线程
|
|
|
|
|
|
GlobalVar.CAN_SendMsgFlag = true;
|
|
|
|
|
|
CANSendMsgThread.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 停止线程
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void CAN_StopTask()
|
|
|
|
|
|
{
|
|
|
|
|
|
GlobalVar.CAN_ReadMsgFlag = false;
|
|
|
|
|
|
GlobalVar.CAN_SendMsgFlag = false;
|
|
|
|
|
|
CANSendMsgThread.Join();
|
|
|
|
|
|
CANReadMsgThread.Join();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送CAN报文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SendCanMsgThread()
|
|
|
|
|
|
{
|
|
|
|
|
|
MFCan_MSG sendMsg = new MFCan_MSG();
|
|
|
|
|
|
while (mfser.Can_Is_Opened && GlobalVar.CAN_SendMsgFlag)
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Ch = 0;
|
|
|
|
|
|
sendMsg.Id = Convert.ToUInt32(GlobalVar.agreementMsgVM.SendId, 16);
|
|
|
|
|
|
sendMsg.Dlc = ByteArrayLib.GetByteArrayFromInt(GlobalVar.agreementMsgVM.FrameLen)[0];//报文数据长度
|
|
|
|
|
|
sendMsg.Type = GlobalVar.agreementMsgVM.FrameType == "EXTEND" ? (byte)MFCan_MSG.CAN_MSG_TYPE_ED : (byte)MFCan_MSG.CAN_MSG_TYPE_SD;//息帧类型 0:标准数据帧 2:扩展数据帧
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
mfser.CanSendOne(sendMsg);
|
|
|
|
|
|
//附加报文
|
|
|
|
|
|
if (!string.IsNullOrEmpty(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalSendId))
|
|
|
|
|
|
{
|
|
|
|
|
|
sendMsg.Ch = 0;
|
|
|
|
|
|
sendMsg.Id = Convert.ToUInt32(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalSendId, 16);
|
|
|
|
|
|
sendMsg.Dlc = 8;//报文数据长度
|
|
|
|
|
|
sendMsg.Type = MFCan_MSG.CAN_MSG_TYPE_SD;//息帧类型 0:标准数据帧 2:扩展数据帧
|
|
|
|
|
|
sendMsg.Dat = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendAdditionalRules.AdditionalContent);
|
|
|
|
|
|
mfser.CanSendOne(sendMsg);
|
|
|
|
|
|
}
|
|
|
|
|
|
Thread.Sleep(GlobalVar.agreementMsgVM.SendPeriod);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接收CAN报文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void ReadCanMsgThread()
|
|
|
|
|
|
{
|
|
|
|
|
|
MFCan_MSG[] r_msg = new MFCan_MSG[10000];
|
|
|
|
|
|
int num;
|
|
|
|
|
|
while (mfser.Can_Is_Opened && GlobalVar.CAN_ReadMsgFlag)
|
|
|
|
|
|
{
|
|
|
|
|
|
num = mfser.MFCanGetMsg(r_msg, 10000);
|
|
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
//判断获取的是不是目标报文
|
|
|
|
|
|
if (r_msg[i].Id == Convert.ToUInt32(GlobalVar.agreementMsgVM.ReciveId, 16) && r_msg[i].Dlc == 8)
|
|
|
|
|
|
{
|
|
|
|
|
|
GetCANReciveValHelper.GetReciveVal(r_msg[i].Dat);//从接收报文中解析实际值
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|