HONGRI_uds_tools/CAN_Tool/CanDevice/CanDevice.cs
liudagui beceab4798 第一次提交:
支持广成CAN,PCAN,同星(要用低版本的TSMaster),TOOMOSS(没有测试过)
2024-11-06 16:52:30 +08:00

205 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Can
{
class CanMessage
{
public UInt32 Id;
public byte[] Data;
public int Len;
public bool IsTx;
public CanMessage()
{
Id = 0;
Data = new byte[8];
Len = 8;
IsTx = true;
}
public CanMessage(UInt32 id, byte[] data, int len, bool isTx)
{
Id = id;
Data = data;
Len = len;
IsTx = isTx;
}
}
class CanDevice
{
public delegate void CanReadMessageCallBack(UInt32 id, byte[] data, int len);
private event CanReadMessageCallBack CanReadMessageEvent;
private static byte[] LenToDlcTable =
{
/*0~8*/
0,1,2,3,4,5,6,7,8,
/*9~12*/
9,9,9,9,
/*13~16*/
10,10,10,10,
/*17~20*/
11,11,11,11,
/*21~24*/
12,12,12,12,
/*25~32*/
13,13,13,13,13,13,13,13,
/*33~48*/
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
/*49~64*/
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
};
private static byte[] DlcToLenTable =
{
/*0~15*/
0,1,2,3,4,5,6,7,8,12,16,20,24,32,48,64
};
protected static byte MessagesLenToDlc(int len)
{
return LenToDlcTable[len];
}
protected static byte MessagesDlcToLen(int dlc)
{
return DlcToLenTable[dlc];
}
//标准CAN
#region CAN
//CAN初始化
public virtual string CanInit()
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CAN逆初始化
public virtual string CanDeInit()
{
this.UnregisterCanReadEventAll();
return "未选择具体设备或没完成相应设备的驱动";
}
public virtual string CanReceiveEventEnable()
{
return "未选择具体设备或没完成相应设备的驱动";
}
public virtual string CanReceiveEventDisable()
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CAN设置ID过滤器
public virtual string CanSetFilter(UInt32 stratId, UInt32 endId)
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CAN发送一帧报文
public virtual void CanSendMessage(UInt32 id, byte[] data, int len)
{
}
//CAN从FIFO读取一帧报文
public virtual bool CanReadMessage(ref UInt32 id, byte[] data, ref int len)
{
return false;
}
#endregion
//CANFD
#region CANFD
//CANFD初始化
public virtual string CanFdInit()
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CANFD逆初始化
public virtual string CanFdDeInit()
{
this.UnregisterCanReadEventAll();
return "未选择具体设备或没完成相应设备的驱动";
}
public virtual string CanFdReceiveEventEnable()
{
return "未选择具体设备或没完成相应设备的驱动";
}
public virtual string CanFdReceiveEventDisable()
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CANFD设置ID过滤器
public virtual string CanFdSetFilter(UInt32 stratId, UInt32 endId)
{
return "未选择具体设备或没完成相应设备的驱动";
}
//CANFD发送一帧报文
public virtual void CanFdSendMessage(UInt32 id, byte[] data, int len)
{
}
//CANFD从FIFO读取一帧报文
public virtual bool CanFdReadMessage(ref UInt32 id, byte[] data, ref int len)
{
return false;
}
#endregion
//注册接收报文事件
public void RegisterCanReadEvent(CanReadMessageCallBack canReadFunc)
{
CanReadMessageEvent += canReadFunc;
}
//删除一个接收报文事件
public void UnregisterCanReadEvent(CanReadMessageCallBack canReadFunc)
{
CanReadMessageEvent -= canReadFunc;
}
//删除所有注册的接收报文事件
public void UnregisterCanReadEventAll()
{
CanReadMessageEvent = null;
}
//触发接收报文事件
public void CanReadEventInvoke(UInt32 id, byte[] data, int len)
{
if (null != CanReadMessageEvent)
{
CanReadMessageEvent.Invoke(id, data, len);
}
}
}
}