2024-11-06 08:52:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using GC_ECan.Basic;
|
|
|
|
|
using System.Windows.Forms;
|
2024-11-13 10:01:40 +00:00
|
|
|
|
using Peak.Can.Basic;
|
2024-11-06 08:52:30 +00:00
|
|
|
|
|
|
|
|
|
namespace Can
|
|
|
|
|
{
|
|
|
|
|
class ECanDevice : CanDevice
|
|
|
|
|
{
|
|
|
|
|
Thread ReadThread;
|
2024-11-13 10:01:40 +00:00
|
|
|
|
AutoResetEvent ReceiveEvent;
|
|
|
|
|
|
|
|
|
|
bool IsConnect;
|
|
|
|
|
|
2024-11-06 08:52:30 +00:00
|
|
|
|
|
|
|
|
|
//<2F><>CAN
|
|
|
|
|
#region CAN
|
|
|
|
|
//CAN<41><4E>ʼ<EFBFBD><CABC>
|
|
|
|
|
public override string CanInit()
|
|
|
|
|
{
|
|
|
|
|
string ret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INIT_CONFIG init_config = new INIT_CONFIG();
|
|
|
|
|
init_config.AccCode = 0;
|
|
|
|
|
init_config.AccMask = 0xffffff;
|
|
|
|
|
init_config.Filter = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 500K
|
|
|
|
|
init_config.Timing0 = 0;
|
|
|
|
|
init_config.Timing1 = 0x1c;
|
|
|
|
|
|
|
|
|
|
init_config.Mode = 0;
|
|
|
|
|
|
|
|
|
|
if (ECANDLL.OpenDevice(1, 0, 0) != GC_ECan.Basic.ECANStatus.STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Open GC_USBCAN_II fault!");
|
|
|
|
|
ret = "Error!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ECANDLL.InitCAN(1, 0, 0, ref init_config) != GC_ECan.Basic.ECANStatus.STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Init GC_USBCAN_II fault!");
|
|
|
|
|
ret = "Error!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ECANDLL.StartCAN(1, 0, 0) != GC_ECan.Basic.ECANStatus.STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("StartCAN GC_USBCAN_II fault!");
|
|
|
|
|
ret = "Error!";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-13 10:01:40 +00:00
|
|
|
|
IsConnect = true;
|
|
|
|
|
|
|
|
|
|
ReadThread = new Thread(new ThreadStart(this.CANReadThreadFunc));
|
|
|
|
|
ReceiveEvent = new System.Threading.AutoResetEvent(false);
|
|
|
|
|
ReadThread.IsBackground = true;
|
|
|
|
|
|
2024-11-06 08:52:30 +00:00
|
|
|
|
ret = "OK";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//CAN<41><4E><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
public override string CanDeInit()
|
|
|
|
|
{
|
|
|
|
|
string ret;
|
|
|
|
|
if(ECANDLL.CloseDevice(1, 0) == GC_ECan.Basic.ECANStatus.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)
|
|
|
|
|
{
|
|
|
|
|
CAN_OBJ mMsg;
|
|
|
|
|
uint mLen = 1; // <20><><EFBFBD>Ͷ<EFBFBD><CDB6>ٴ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We create a TPCANMsg message structure
|
|
|
|
|
//
|
|
|
|
|
mMsg = new CAN_OBJ();
|
|
|
|
|
mMsg.data = new byte[8];
|
|
|
|
|
mMsg.ID = id;
|
|
|
|
|
mMsg.DataLen = (byte)len;
|
|
|
|
|
mMsg.ExternFlag = 0; // <20><><EFBFBD><D7BC><EFBFBD><EFBFBD>չѡ<D5B9><D1A1>
|
|
|
|
|
mMsg.RemoteFlag = 0; // <20><><EFBFBD>غ<EFBFBD>Զ<EFBFBD><D4B6>ѡ<EFBFBD><D1A1>
|
|
|
|
|
for(int i = 0; i< mMsg.DataLen; i++)
|
|
|
|
|
{
|
|
|
|
|
mMsg.data[i] = data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ECANDLL.Transmit(1, 0, 0, ref mMsg, (ushort)mLen) != ECANStatus.STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//CAN<41><4E>FIFO<46><4F><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD>
|
|
|
|
|
public override bool CanReadMessage(ref UInt32 id, byte[] data, ref int len)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
|
CAN_OBJ mMsg = new CAN_OBJ();
|
|
|
|
|
mMsg.data = new byte[8];
|
|
|
|
|
uint mLen = 1; // <20><><EFBFBD>ն<EFBFBD><D5B6>ٴ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
if (!((ECANDLL.Receive(1, 0, 0, out mMsg, mLen, 1) == ECANStatus.STATUS_OK) & (mLen > 0)))
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
id = mMsg.ID;
|
|
|
|
|
len = (int)mMsg.DataLen;
|
|
|
|
|
for(int i = 0;i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
data[i] = mMsg.data[i];
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public override string CanSetFilter(UInt32 stratId, UInt32 endId)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "OK";
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 10:01:40 +00:00
|
|
|
|
private void CANReadThreadFunc()
|
|
|
|
|
{
|
|
|
|
|
UInt32 id = 0;
|
|
|
|
|
byte[] data = new byte[8];
|
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
|
|
UInt32 iBuffer;
|
|
|
|
|
// TPCANStatus stsResult;
|
|
|
|
|
|
|
|
|
|
iBuffer = Convert.ToUInt32(ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
|
|
|
|
|
// Sets the handle of the Receive-Event.
|
|
|
|
|
//
|
|
|
|
|
// stsResult = PCANBasic.SetValue(PcanHandle, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));
|
|
|
|
|
|
|
|
|
|
//if (stsResult != TPCANStatus.PCAN_ERROR_OK)
|
|
|
|
|
//{
|
|
|
|
|
// return;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
// While this mode is selected
|
|
|
|
|
while (IsConnect)
|
|
|
|
|
{
|
|
|
|
|
if (ReceiveEvent.WaitOne(50))
|
|
|
|
|
{
|
|
|
|
|
while (true == CanReadMessage(ref id, data, ref len))
|
|
|
|
|
{
|
|
|
|
|
CanReadEventInvoke(id, data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 08:52:30 +00:00
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|