HONGRI_uds_tools/CAN_Tool/CanDevice/TOSUN/ToSun.cs

447 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSMaster;
namespace Can
{
class ToSunDevice : CanDevice
{
private string Name = "666";
private TCANQueueEvent CanQueueEvent;
private TCANFDQueueEvent CanFdQueueEvent;
public ToSunDevice()
{
this.CanQueueEvent = new TCANQueueEvent(OnCANRxEvent);
this.CanFdQueueEvent = new TCANFDQueueEvent(OnCANFDRxEvent);
}
~ToSunDevice()
{
//释放所有资源
TsMasterApi.finalize_lib_tsmaster();
}
//标准CAN
#region CAN
//CAN初始化
public override string CanInit()
{
//释放所有资源
//TsMasterApi.finalize_lib_tsmaster();
TsMasterApi.initialize_lib_tsmaster(Name);
//创建通道数
if ((TsMasterApi.tsapp_set_can_channel_count(1) == 0)
&& (TsMasterApi.tsapp_set_lin_channel_count(0) == 0)
)
{
}
else
{
return "创建通道失败";
}
TsMasterApi.tsapp_set_lin_channel_count(0);
/*int hardwareNum = 0;
TsMasterApi.tsapp_enumerate_hw_devices(out hardwareNum);
TLIBHWInfo tmpDeviceInfo = new TLIBHWInfo(0);
TsMasterApi.tsapp_get_hw_info_by_index(0, ref tmpDeviceInfo);*/
#if true
//创建通道映射
if (TsMasterApi.tsapp_set_mapping_verbose(Name, TLIBApplicationChannelType.APP_CAN,
APP_CHANNEL.CHN1, "TC1012", TLIBBusToolDeviceType.TS_USB_DEVICE, 12, 0, HARDWARE_CHANNEL.CHN1) == 0) //12
#else
if (TsMasterApi.tsapp_set_mapping_verbose(Name, TLIBApplicationChannelType.APP_CAN,
APP_CHANNEL.CHN1, "TC1011", TLIBBusToolDeviceType.TS_USB_DEVICE, 5, 0, HARDWARE_CHANNEL.CHN1) == 0) //12
#endif
{
}
else
{
return "创建通道映射失败";
}
#if true
//初始化通道参数
if (TsMasterApi.tsapp_configure_baudrate_can((int)APP_CHANNEL.CHN1, 500, false, true) != 0)
{
return "创建通道参数失败";
}
#else
if (TsMasterApi.tsapp_configure_can_regs(APP_CHANNEL.CHN1, 500, 7, 1, 1, 1, 0) != 0)
{
return "创建通道参数失败";
}
//tsapp_configure_can_regs(APP_CHANNEL AIdxChn, float ABaudrateKbps, int ASEG1, int ASEG2, int APrescaler, int ASJW, int A120OhmConnected);
#endif
//连接application连接硬件通道并开启接收FIFO
int ret = TsMasterApi.tsapp_connect();
string connectResult = TsMasterApi.tsapp_get_error_description(ret);
if (0 == ret)
{
TsMasterApi.tsfifo_enable_receive_fifo();
}
else
{
//Log(connectResult);
//Log("Connect Application Failed! Please check the mapping table and whether the Hardware is Ready?!");
return "连接设备失败";
}
TsMasterApi.tsapp_unregister_events_all((IntPtr)0);
//注册接收回调函数在每一次的回调函数vCANQueueEventObj中刷新数据段
/*if (0 == TsMasterApi.tsapp_register_event_can((IntPtr)0, CanQueueEvent))
{
}
else
{
return "创建接受报文事件失败";
}*/
return "OK";
}
//CAN逆初始化
public override string CanDeInit()
{
base.CanDeInit();
TsMasterApi.tsapp_unregister_events_all((IntPtr)0);
if(0 == TsMasterApi.tsapp_disconnect()
)
{
base.CanDeInit();
return "OK";
}
else
{
return "断开连接失败";
}
}
public override string CanReceiveEventEnable()
{
string ret;
//注册接收回调函数在每一次的回调函数vCANQueueEventObj中刷新数据段
if (0 == TsMasterApi.tsapp_register_event_can((IntPtr)0, CanQueueEvent))
{
ret = "OK";
}
else
{
ret = "创建接受报文事件失败";
}
return ret;
}
public override string CanReceiveEventDisable()
{
string ret;
if (0 == TsMasterApi.tsapp_unregister_events_all((IntPtr)0))
{
ret = "OK";
}
else
{
ret = "失能接收事件失败";
}
return ret;
}
public override string CanSetFilter(UInt32 stratId, UInt32 endId)
{
//清除所有过滤器
for (int i = 0; i < 0x7ff; i++)
{
TsMasterApi.tsfifo_delete_can_canfd_pass_filter(APP_CHANNEL.CHN1, (int)i);
}
//设置过滤
for(UInt32 i = stratId; i <= endId; i++)
{
TsMasterApi.tsfifo_add_can_canfd_pass_filter(APP_CHANNEL.CHN1, (int)i, true);
}
return "OK";
}
//CAN发送一帧报文
public override void CanSendMessage(UInt32 id, byte[] data, int len)
{
TLIBCAN msg = new TLIBCAN(0, (int)id, true, false, false, MessagesLenToDlc(len), data);
if (TsMasterApi.tsapp_transmit_can_async(ref msg) == 0)
{
}
else
{
}
}
//CAN从FIFO接收一帧报文
public override bool CanReadMessage(ref UInt32 id, byte[] data, ref int len)
{
TLIBCAN msg;
bool ret = false;
if(true == TsMasterApi.tsfifo_receive_can_message(out msg, 0))
{
id = (UInt32)msg.FIdentifier;
msg.FData.CopyTo(data, 0);
len = MessagesDlcToLen(msg.FDLC);
ret = true;
}
return ret;
}
//CAN接收事件
private void OnCANRxEvent(IntPtr AObj, ref TLIBCAN AData)
{
if (AData.FIsTx)//发送的报文
{
}
else if(false == AData.FIsError)//接受的报文
{
TLIBCAN msg;
while(true == TsMasterApi.tsfifo_receive_can_message(out msg, 0))
{
CanReadEventInvoke((UInt32)AData.FIdentifier, AData.FData, MessagesDlcToLen(AData.FDLC));
}
}
}
#endregion
//CANFD
#region CANFD
//CANFD初始化
public override string CanFdInit()
{
TsMasterApi.initialize_lib_tsmaster(Name);
//创建通道数
if ((TsMasterApi.tsapp_set_can_channel_count(1) == 0)
&& (TsMasterApi.tsapp_set_lin_channel_count(0) == 0)
)
{
}
else
{
return "创建通道失败";
}
//创建通道映射
if (TsMasterApi.tsapp_set_mapping_verbose(Name, TLIBApplicationChannelType.APP_CAN,
APP_CHANNEL.CHN1, "TC1012", TLIBBusToolDeviceType.TS_USB_DEVICE, 12, 0, HARDWARE_CHANNEL.CHN1) == 0)
{
}
else
{
return "创建通道映射失败";
}
//初始化通道参数
#if true
if (TsMasterApi.tsapp_configure_baudrate_canfd((int)APP_CHANNEL.CHN1, 500, 2000, TLIBCANFDControllerType.lfdtISOFDCAN, TLIBCANFDControllerMode.lfdmNormal, true) == 0)
{
}
else
{
return "创建通道参数失败";
}
#else
if (TsMasterApi.tsapp_configure_canfd_regs(APP_CHANNEL.CHN1,
500, 4, 1, 1, 1, //500
2000, 4, 1, 1, 1, //2000
TLIBCANFDControllerType.lfdtISOFDCAN, TLIBCANFDControllerMode.lfdmNormal, 0) != 0)
{
return "创建通道参数失败";
}
#endif
//连接application连接硬件通道并开启接收FIFO
int ret = TsMasterApi.tsapp_connect();
string connectResult = TsMasterApi.tsapp_get_error_description(ret);
if (0 == ret)
{
TsMasterApi.tsfifo_enable_receive_fifo();
}
else
{
return "连接设备失败";
}
TsMasterApi.tsapp_unregister_events_all((IntPtr)0);
return "OK";
}
//CANFD逆初始化
public override string CanFdDeInit()
{
base.CanFdDeInit();
TsMasterApi.tsapp_unregister_events_all((IntPtr)0);
if((0 == TsMasterApi.tsapp_disconnect())
)
{
return "OK";
}
else
{
return "断开连接失败";
}
}
public override string CanFdSetFilter(UInt32 stratId, UInt32 endId)
{
//清除所有过滤器
for (int i = 0; i < 0x7ff; i++)
{
TsMasterApi.tsfifo_delete_can_canfd_pass_filter(APP_CHANNEL.CHN1, (int)i);
}
//设置过滤
for (UInt32 i = stratId; i <= endId; i++)
{
TsMasterApi.tsfifo_add_can_canfd_pass_filter(APP_CHANNEL.CHN1, (int)i, true);
}
return "OK";
}
public override string CanFdReceiveEventEnable()
{
string ret;
//注册接收回调函数在每一次的回调函数vCANQueueEventObj中刷新数据段
if (0 == TsMasterApi.tsapp_register_event_canfd((IntPtr)0, CanFdQueueEvent))
{
ret = "OK";
}
else
{
ret = "创建接受报文事件失败";
}
return ret;
}
public override string CanFdReceiveEventDisable()
{
return this.CanReceiveEventDisable();
}
//CANFD发送一帧报文
public override void CanFdSendMessage(UInt32 id, byte[] data, int len)
{
TLIBCANFD msg = new TLIBCANFD(APP_CHANNEL.CHN1, (int)id, true, false, false, MessagesLenToDlc(len), data, true, true);
if (TsMasterApi.tsapp_transmit_canfd_async(ref msg) == 0)
{
}
else
{
}
}
//CANFD从FIFO接收一帧报文
public override bool CanFdReadMessage(ref UInt32 id, byte[] data, ref int len)
{
TLIBCANFD msg = new TLIBCANFD();
bool ret = false;
if (true == TsMasterApi.tsfifo_receive_canfd_message(ref msg, 0))
{
id = (UInt32)msg.FIdentifier;
msg.FData.CopyTo(data, 0);
len = MessagesDlcToLen(msg.FDLC);
ret = true;
}
return ret;
}
//CANFD接收报文事件
private void OnCANFDRxEvent(IntPtr AObj, ref TLIBCANFD AData)
{
//发送的报文
if (AData.FIsTx)
{
}
else if (false == AData.FIsError)//接受的报文
{
TLIBCANFD msg = new TLIBCANFD();
while (true == TsMasterApi.tsfifo_receive_canfd_message(ref msg, 0))
{
CanReadEventInvoke((UInt32)AData.FIdentifier, AData.FData, MessagesDlcToLen(AData.FDLC));
}
}
}
#endregion
}
}