285 lines
6.5 KiB
C#
285 lines
6.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Runtime.InteropServices;
|
||
using System.Threading;
|
||
using GC_ECan.Basic;
|
||
using System.Windows.Forms;
|
||
using Peak.Can.Basic;
|
||
|
||
namespace Can
|
||
{
|
||
class ECanDevice : CanDevice
|
||
{
|
||
Thread ReadThread;
|
||
AutoResetEvent ReceiveEvent;
|
||
|
||
bool IsConnect;
|
||
|
||
|
||
//±ê×¼CAN
|
||
#region CAN
|
||
//CAN³õʼ»¯
|
||
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;
|
||
|
||
// ²¨ÌØÂÊ 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
|
||
{
|
||
IsConnect = true;
|
||
|
||
ReadThread = new Thread(new ThreadStart(this.CANReadThreadFunc));
|
||
ReceiveEvent = new System.Threading.AutoResetEvent(false);
|
||
ReadThread.IsBackground = true;
|
||
|
||
ret = "OK";
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
//CANÄæ³õʼ»¯
|
||
public override string CanDeInit()
|
||
{
|
||
string ret;
|
||
if(ECANDLL.CloseDevice(1, 0) == GC_ECan.Basic.ECANStatus.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)
|
||
{
|
||
CAN_OBJ mMsg;
|
||
uint mLen = 1; // ·¢ËͶàÉÙ´Î
|
||
|
||
|
||
// 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; // ±ê×¼ºÍÀ©Õ¹Ñ¡Ôñ
|
||
mMsg.RemoteFlag = 0; // ±¾µØºÍÔ¶³ÌÑ¡Ôñ
|
||
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´ÓFIFO½ÓÊÕÒ»Ö¡±¨ÎÄ
|
||
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; // ½ÓÊÕ¶àÉÙ´Î
|
||
|
||
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;
|
||
}
|
||
|
||
// ÉèÖùýÂËÉèÖÃ
|
||
public override string CanSetFilter(UInt32 stratId, UInt32 endId)
|
||
{
|
||
|
||
|
||
|
||
return "OK";
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
#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
|
||
|
||
}
|
||
} |