void GetErrorInfo() { CAN_ERR_INFO errorInfo = new CAN_ERR_INFO(); ECANStatus readErrResult = ECANDLL.ReadErrInfo(deviceType, useCANIndex, useCANIndex, out errorInfo); if (readErrResult != ECANStatus.STATUS_OK) { Logger.logError("无法读取CAN错误!"); return; } if (errorInfo.ErrCode == 0) { Logger.logError("存在未知CAN错误!"); } else { Logger.logError("存在以下CAN错误:"); Logger.print("------------ECAN错误开始------------"); foreach (uint errKey in errExplain.Keys) { if ((errKey & errorInfo.ErrCode) > 0) { Logger.print(errExplain[errKey]); } } Logger.print("------------ECAN错误结束------------"); } }
protected override void ReceiveData() { if (GetReceivedCount() <= 0) { return; } CAN_OBJ buffer = new CAN_OBJ(); ECANStatus getStatus = ECANDLL.Receive( deviceType, useDeviceIndex, useCANIndex, ref buffer, 1, 20); if (getStatus == ECANStatus.STATUS_OK) { Push(new CANData((int)buffer.ID, buffer.data)); } // Or else you can get error info as you want }
public static void ThrowIfError(ECANStatus status, UInt32 deviceType, UInt32 deviceIndex, UInt32 channel) { if (status != ECANStatus.STATUS_OK) { CAN_ERR_INFO errorInfo; if (ReadErrInfo(deviceType, deviceIndex, channel, out errorInfo) == ECANStatus.STATUS_OK) { switch (errorInfo.ErrCode) { case 0x0100: throw (new Exception("Device already open")); case 0x0200: throw (new Exception("Open device error")); case 0x0400: throw (new Exception("Device did not open")); case 0x0800: throw (new Exception("Buffer overflow")); case 0x1000: throw (new Exception("Device not exist")); case 0x2000: throw (new Exception("Load dll failure")); case 0x4000: throw (new Exception("Execute the command failure error")); case 0x8000: throw (new Exception("Insufficient memory")); case 0x0001: throw (new Exception("CAN controller FIFO overflow")); case 0x0002: throw (new Exception("CAN controller error alarm")); case 0x0004: throw (new Exception("CAN controller passive error")); case 0x0008: throw (new Exception("CAN controller arbitration lose")); case 0x0010: throw (new Exception("CAN controller bus error")); case 0x0020: throw (new Exception("CAN receive register full")); case 0x0040: throw (new Exception("CAN receive register overflow")); case 0x0080: throw (new Exception("CAN controller active error")); default: throw (new Exception(String.Format("Unknown GCAN error ({0})", errorInfo.ErrCode))); } } } }