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 } }