268 lines
6.5 KiB
C#
268 lines
6.5 KiB
C#
|
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_;
|
|||
|
|
|||
|
//<2F><>CAN
|
|||
|
#region CAN
|
|||
|
//CAN<41><4E>ʼ<EFBFBD><CABC>
|
|||
|
public override string CanInit()
|
|||
|
{
|
|||
|
string ret;
|
|||
|
|
|||
|
uint device_type; // <20>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>
|
|||
|
uint device_index; // <20>豸<EFBFBD><E8B1B8>
|
|||
|
uint channel_index; // ͨ<><CDA8>
|
|||
|
uint reserved;
|
|||
|
uint temp_error;
|
|||
|
|
|||
|
|
|||
|
ZCAN_CHANNEL_INIT_CONFIG config_ = new ZCAN_CHANNEL_INIT_CONFIG();
|
|||
|
|
|||
|
|
|||
|
//
|
|||
|
device_type = Define.ZCAN_USBCAN2; // <20>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD> <20><>USBCANII / II + <20><> MiniPCIeCAN II
|
|||
|
device_index = 0; // <20>豸
|
|||
|
channel_index = 0;// ͨ<><CDA8><EFBFBD><EFBFBD>
|
|||
|
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;
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>豸
|
|||
|
device_handle_ = Method.ZCAN_OpenDevice(device_type, device_index, reserved);
|
|||
|
if(NULL == (int)device_handle_)
|
|||
|
{
|
|||
|
temp_error = 1;
|
|||
|
}
|
|||
|
|
|||
|
// <20>貨<EFBFBD><E8B2A8><EFBFBD><EFBFBD>
|
|||
|
string path = channel_index + "/baud_rate_custom";
|
|||
|
string baudrate = "500000"; // 500K
|
|||
|
Method.ZCAN_SetValue(device_handle_, path, Encoding.ASCII.GetBytes(baudrate));
|
|||
|
|
|||
|
//
|
|||
|
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>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<41><4E><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
public override string CanDeInit()
|
|||
|
{
|
|||
|
string ret;
|
|||
|
if(Method.ZCAN_CloseDevice(device_handle_) == Define.STATUS_OK)
|
|||
|
{
|
|||
|
ret = "OK";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ret = "<22>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>";
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
public override string CanReceiveEventEnable()
|
|||
|
{
|
|||
|
string ret;
|
|||
|
if (null == ReadThread)
|
|||
|
{
|
|||
|
|
|||
|
ret = "<22>߳<EFBFBD>Ϊnull";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ReadThread.Start();
|
|||
|
ret = "OK";
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public override string CanReceiveEventDisable()
|
|||
|
{
|
|||
|
string ret;
|
|||
|
if (null == ReadThread)
|
|||
|
{
|
|||
|
ret = "<22>߳<EFBFBD>Ϊnull";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ReadThread.Abort();
|
|||
|
ret = "OK";
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
// CAN<41><4E><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD>
|
|||
|
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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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<41><4E>FIFO<46><4F><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public override string CanSetFilter(UInt32 stratId, UInt32 endId)
|
|||
|
{
|
|||
|
return "OK";
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
//CANFD
|
|||
|
#region CANFD
|
|||
|
|
|||
|
//CANFD<46><44>ʼ<EFBFBD><CABC>
|
|||
|
public override string CanFdInit()
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
return "OK";
|
|||
|
}
|
|||
|
|
|||
|
//CANFD<46><44>ʼ<EFBFBD><CABC>
|
|||
|
public override string CanFdDeInit()
|
|||
|
{
|
|||
|
// return this.CanDeInit();
|
|||
|
string ret;
|
|||
|
|
|||
|
ret = "OK";
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
// CanFD <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public override string CanFdSetFilter(UInt32 stratId, UInt32 endId)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>
|
|||
|
|
|||
|
|
|||
|
return "OK";
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public override string CanFdReceiveEventEnable()
|
|||
|
{
|
|||
|
return this.CanReceiveEventEnable();
|
|||
|
}
|
|||
|
|
|||
|
// <20>ؽ<EFBFBD><D8BD><EFBFBD>
|
|||
|
public override string CanFdReceiveEventDisable()
|
|||
|
{
|
|||
|
return this.CanReceiveEventDisable();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//CANFD<46><44><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD>
|
|||
|
public override void CanFdSendMessage(UInt32 id, byte[] data, int len)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//CANFD<46><44>FIFO<46><4F><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD>
|
|||
|
public override bool CanFdReadMessage(ref UInt32 id, byte[] data, ref int len)
|
|||
|
{
|
|||
|
bool ret = false;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
}
|
|||
|
}
|