HONGRI_uds_tools/CAN_Tool/CanDevice/ZLG/ZLG_CAN.cs
liudagui 48f9148fb6 1.添加boot模式下支持的DID
2.预加周立功驱动,还没有加好。
2024-11-13 18:01:40 +08:00

271 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using ZLGCAN.Basic;
using System.Windows.Forms;
using USB2XXX;
using System.IO;
using System.Drawing;
namespace Can
{
class ZLGCanDevice : CanDevice
{
const int NULL = 0;
Thread ReadThread;
IntPtr device_handle_;
IntPtr channel_handle_;
//标准CAN
#region CAN
//CAN初始化
public override string CanInit()
{
string ret;
uint device_type; // 设备类型
uint device_index; // 设备号
uint channel_index; // 通道
uint reserved;
uint temp_error;
ZCAN_CHANNEL_INIT_CONFIG config_ = new ZCAN_CHANNEL_INIT_CONFIG();
//
device_type = Define.ZCAN_USBCAN2; // 设备类型 USBCANII / II + 、 MiniPCIeCAN II
device_index = 0; // 设备
channel_index = 0;// 通道号
reserved = 0;
temp_error = 0;
//
config_.can_type = Define.TYPE_CAN;
config_.can.filter = 0;
config_.can.acc_code = 0;
config_.can.acc_mask = 0xFFFFFFFF;
config_.can.mode = 0;
// 打开设备
device_handle_ = Method.ZCAN_OpenDevice(device_type, device_index, reserved);
if(NULL == (int)device_handle_)
{
temp_error = 1;
}
// 设波特率
string path = channel_index + "/baud_rate_custom";
string baudrate = "500000"; // 500K
Method.ZCAN_SetValue(device_handle_, path, Encoding.ASCII.GetBytes(baudrate));
//
// 初始化设置
IntPtr pConfig = Marshal.AllocHGlobal(Marshal.SizeOf(config_));
Marshal.StructureToPtr(config_, pConfig, true);
channel_handle_ = Method.ZCAN_InitCAN(device_handle_, (uint)channel_index, pConfig);
if (NULL == (int)channel_handle_)
{
temp_error = 1;
}
// 启动对应的can
if (Method.ZCAN_StartCAN((IntPtr)channel_index) != Define.STATUS_OK)
{
temp_error = 1;
}
if (temp_error == 1)
{
ret = "Error!";
}
else
{
ret = "OK";
}
return ret;
}
//CAN逆初始化
public override string CanDeInit()
{
string ret;
if(Method.ZCAN_CloseDevice(device_handle_) == Define.STATUS_OK)
{
ret = "OK";
}
else
{
ret = "断开连接失败";
}
return ret;
}
public override string CanReceiveEventEnable()
{
string ret;
if (null == ReadThread)
{
ret = "线程为null";
}
else
{
ReadThread.Start();
ret = "OK";
}
return ret;
}
public override string CanReceiveEventDisable()
{
string ret;
if (null == ReadThread)
{
ret = "线程为null";
}
else
{
ReadThread.Abort();
ret = "OK";
}
return ret;
}
// CAN发送一帧报文
public override void CanSendMessage(UInt32 id, byte[] data, int len)
{
//uint temp_error;
// temp_error = 0;
ZCAN_Transmit_Data can_data = new ZCAN_Transmit_Data();
can_data.frame.data = new byte[8];
can_data.frame.can_id = id;
for(int i = 0;i< len;i++)
{
can_data.frame.data[i] = data[i];
}
can_data.frame.can_dlc = (byte)len;
can_data.transmit_type = 0; // 正常发送
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(can_data));
// Marshal.StructureToPtr(can_data, ptr, true);
if(Method.ZCAN_Transmit(device_handle_, ptr, 1) != 1)
{
// temp_error = 1;
}
}
//CAN从FIFO接收一帧报文
public override bool CanReadMessage(ref UInt32 id, byte[] data, ref int len)
{
ZCAN_Receive_Data[] can_data = new ZCAN_Receive_Data[16];
const int TYPE_CAN = 0;
uint len_size;
bool ret = false;
len_size = Method.ZCAN_GetReceiveNum(channel_handle_, TYPE_CAN);
if(len_size > 0)
{
int size = Marshal.SizeOf(typeof(ZCAN_Receive_Data));
IntPtr ptr = Marshal.AllocHGlobal((int)100 * size);
Method.ZCAN_Receive(channel_handle_, ptr, 100, 50);
for (int i = 0; i < len_size; ++i)
{
can_data[i] = (ZCAN_Receive_Data)Marshal.PtrToStructure(
(IntPtr)((Int64)ptr + i * size), typeof(ZCAN_Receive_Data));
}
}
return ret;
}
// 设置过滤设置
public override string CanSetFilter(UInt32 stratId, UInt32 endId)
{
return "OK";
}
#endregion
//CANFD
#region CANFD
//CANFD初始化
public override string CanFdInit()
{
return "OK";
}
//CANFD初始化
public override string CanFdDeInit()
{
// return this.CanDeInit();
string ret;
ret = "OK";
return ret;
}
// CanFD 过滤器设置
public override string CanFdSetFilter(UInt32 stratId, UInt32 endId)
{
//清除所有过滤器
//设置过滤
return "OK";
}
// 开接收
public override string CanFdReceiveEventEnable()
{
return this.CanReceiveEventEnable();
}
// 关接收
public override string CanFdReceiveEventDisable()
{
return this.CanReceiveEventDisable();
}
//CANFD发送一帧报文
public override void CanFdSendMessage(UInt32 id, byte[] data, int len)
{
}
//CANFD从FIFO接收一帧报文
public override bool CanFdReadMessage(ref UInt32 id, byte[] data, ref int len)
{
bool ret = false;
return ret;
}
#endregion
}
}