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.
165 lines
5.7 KiB
C#
165 lines
5.7 KiB
C#
using CompressorXN_Common;
|
|
using CompressorXN_Model.Enums;
|
|
using System;
|
|
using System.Threading;
|
|
using thinger.DataConvertLib;
|
|
using USB2XXX;
|
|
|
|
namespace CompressorXN_Communication.TuMos
|
|
{
|
|
public class TMSLINHelper : BaseTMS
|
|
{
|
|
private static Byte LINIndex = 0;
|
|
private static Int32 ret = 0;
|
|
|
|
/// <summary>
|
|
/// 初始化LIN
|
|
/// </summary>
|
|
/// <param name="bps"></param>
|
|
public static void LinInit(int bps)
|
|
{
|
|
ret = USB2LIN_EX.LIN_EX_Init(DevHandle, LINIndex, bps, 1);
|
|
if (ret != USB2LIN_EX.LIN_EX_SUCCESS)
|
|
{
|
|
Console.WriteLine("Config LIN failed!");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Config LIN Success!");
|
|
}
|
|
}
|
|
|
|
private static Thread LINSendMsgThread = null;
|
|
private static Thread LINReadMsgThread = null;
|
|
|
|
/// <summary>
|
|
/// 启动线程
|
|
/// </summary>
|
|
public static void LIN_StartTask()
|
|
{
|
|
if (LINReadMsgThread == null)
|
|
{
|
|
LINReadMsgThread = new Thread(() => LIN_ReadMsgThread());
|
|
LINReadMsgThread.Start();
|
|
}
|
|
if (LINSendMsgThread == null)
|
|
{
|
|
LINSendMsgThread = new Thread(() => LIN_SendMsgThread());
|
|
LINSendMsgThread.Start();
|
|
}
|
|
|
|
GlobalVar.LIN_SendMsgFlag = true;
|
|
GlobalVar.LIN_GetMsgFlag = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止线程
|
|
/// </summary>
|
|
public static void LIN_StopTask()
|
|
{
|
|
GlobalVar.LIN_SendMsgFlag = false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送LIN报文
|
|
/// </summary>
|
|
public static void LIN_SendMsgThread()
|
|
{
|
|
while (true)
|
|
{
|
|
if (!GlobalVar.LIN_SendMsgFlag)
|
|
{
|
|
Thread.Sleep(10);
|
|
continue;
|
|
}
|
|
/************************************主机写数据************************************/
|
|
//主机模式发送数据,ID和数据根据实际情况进行修改
|
|
byte[] DataBuffer = new byte[8];
|
|
switch (GlobalVar.stepEnum)
|
|
{
|
|
case StepEnum.idling:
|
|
string idlingHex = GlobalVar.agreementMsgVM.IdlingHex;
|
|
if (!string.IsNullOrEmpty(idlingHex))
|
|
{
|
|
DataBuffer = ByteArrayLib.GetByteArrayFromHexString(idlingHex);
|
|
}
|
|
break;
|
|
|
|
case StepEnum.speed1:
|
|
string speed1Hex = GlobalVar.agreementMsgVM.Speed1Hex;
|
|
if (!string.IsNullOrEmpty(speed1Hex))
|
|
{
|
|
DataBuffer = ByteArrayLib.GetByteArrayFromHexString(speed1Hex);
|
|
}
|
|
break;
|
|
case StepEnum.speed2:
|
|
string speed2Hex = GlobalVar.agreementMsgVM.Speed2Hex;
|
|
if (!string.IsNullOrEmpty(speed2Hex))
|
|
{
|
|
DataBuffer = ByteArrayLib.GetByteArrayFromHexString(speed2Hex);
|
|
}
|
|
break;
|
|
case StepEnum.speed3:
|
|
string speed3Hex = GlobalVar.agreementMsgVM.Speed3Hex;
|
|
if (!string.IsNullOrEmpty(speed3Hex))
|
|
{
|
|
DataBuffer = ByteArrayLib.GetByteArrayFromHexString(speed3Hex);
|
|
}
|
|
break;
|
|
case StepEnum.endolead:
|
|
string endoleadHex = GlobalVar.agreementMsgVM.EndoleadHex;
|
|
if (!string.IsNullOrEmpty(endoleadHex))
|
|
{
|
|
DataBuffer = ByteArrayLib.GetByteArrayFromHexString(endoleadHex);
|
|
}
|
|
break;
|
|
|
|
}
|
|
byte ID = ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.SendId)[0];
|
|
ret = USB2LIN_EX.LIN_EX_MasterWrite(DevHandle, LINIndex, ID, DataBuffer, (byte)DataBuffer.Length, (byte)1);
|
|
if (ret != USB2LIN_EX.LIN_EX_SUCCESS)
|
|
{
|
|
Console.WriteLine("Write LIN failed!");
|
|
}
|
|
//else
|
|
//{
|
|
// Console.WriteLine("Write LIN Success!");
|
|
//}
|
|
|
|
Thread.Sleep(GlobalVar.agreementMsgVM.SendPeriod);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收LIN报文
|
|
/// </summary>
|
|
public static void LIN_ReadMsgThread()
|
|
{
|
|
while (true)
|
|
{
|
|
if (!GlobalVar.LIN_GetMsgFlag)
|
|
{
|
|
Thread.Sleep(10);
|
|
continue;
|
|
}
|
|
byte[] DataBuffer = new byte[8];
|
|
ret = USB2LIN_EX.LIN_EX_MasterRead(DevHandle, LINIndex, ByteArrayLib.GetByteArrayFromHexString(GlobalVar.agreementMsgVM.ReciveId)[0], DataBuffer);
|
|
if (ret < 0)
|
|
{
|
|
Console.WriteLine("Read LIN failed! ret={0}", ret);
|
|
}
|
|
else if (ret == 0)
|
|
{
|
|
Console.WriteLine("The slave machine is not responding!");
|
|
}
|
|
else
|
|
{
|
|
GetCANReciveValHelper.GetReciveVal(DataBuffer);//从接收报文中解析实际值
|
|
}
|
|
Thread.Sleep(GlobalVar.agreementMsgVM.RecivePeriod);
|
|
}
|
|
}
|
|
}
|
|
}
|