private void ReadMessages2() { CAN_OBJ mMsg = new CAN_OBJ(); int sCount = 0; do { uint mLen = 1; if (!((ECANDLL.Receive(1, i, 1, out mMsg, mLen, 0) == ECANStatus.STATUS_OK) & (mLen > 0))) { break; } this.gRecMsgBuf2[this.gRecMsgBufHead2].ID = mMsg.ID; this.gRecMsgBuf2[this.gRecMsgBufHead2].DataLen = mMsg.DataLen; this.gRecMsgBuf2[this.gRecMsgBufHead2].data = mMsg.data; this.gRecMsgBuf2[this.gRecMsgBufHead2].ExternFlag = mMsg.ExternFlag; this.gRecMsgBuf2[this.gRecMsgBufHead2].RemoteFlag = mMsg.RemoteFlag; if ((mMsg.ID == 0x141) || (mMsg.ID == 0x139) || (mMsg.ID == 0x143)) { this.gRecMsgBufHead2 += 1; if (this.gRecMsgBufHead2 >= REC_MSG_BUF_MAX) { this.gRecMsgBufHead2 = 0; } } sCount++; }while (sCount < 500); }
private void ReadMessages() { int m = 0; int sCount = 0; do { CAN_OBJ mMsg = new CAN_OBJ(); mMsg.ID = 0; uint mLen = 1; if ((ECANDLL.Receive(1, i, 0, out mMsg, mLen, 0) == ECANStatus.STATUS_OK)) { } else { break; } if (mMsg.ID == 0x139) { mad0 = (mMsg.data[0] + mMsg.data[1] * 256) / 1000.0; } if (mMsg.ID == 0x141) { mad1 = (mMsg.data[0] + mMsg.data[1] * 256) / 1000.0; } if (mMsg.ID == 0x143) { mad2 = (mMsg.data[0] + mMsg.data[1] * 256) / 1000.0; m_starttime = m_starttime + 0.01; p.x = mad0; p.y = mad1; p.z = mad2; p.t = m_starttime; ClsStaticStation.m_Global.madlist.Add(p); } sCount++; }while (sCount < 500); }
private void ReadMessages() { mainform.canDataRWLock.EnterWriteLock(); CAN_OBJ mMsg = new CAN_OBJ(); try { int sCount = 0; do { uint mLen = 1; if (!((ECANDLL.Receive(1, 0, 0, out mMsg, mLen, 1) == ECANStatus.STATUS_OK) & (mLen > 0))) { break; } if (this.gRecMsgBuf[this.gRecMsgBufHead].data == null) { this.gRecMsgBuf[this.gRecMsgBufHead].data = new byte[8]; } this.gRecMsgBuf[this.gRecMsgBufHead].ID = mMsg.ID; this.gRecMsgBuf[this.gRecMsgBufHead].DataLen = mMsg.DataLen; for (int i = 0; i < 8; i++) { this.gRecMsgBuf[this.gRecMsgBufHead].data[i] = mMsg.data[i]; } this.gRecMsgBuf[this.gRecMsgBufHead].ExternFlag = mMsg.ExternFlag; this.gRecMsgBuf[this.gRecMsgBufHead].RemoteFlag = mMsg.RemoteFlag; this.gRecMsgBuf[this.gRecMsgBufHead].TimeFlag = mMsg.TimeFlag; this.gRecMsgBuf[this.gRecMsgBufHead].TimeStamp = mMsg.TimeStamp; this.gRecMsgBufHead += 1; if (this.gRecMsgBufHead >= REC_MSG_BUF_MAX) { this.gRecMsgBufHead = 0; } sCount++; } while (sCount < 500); } finally { mainform.canDataRWLock.ExitWriteLock(); } return; }
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 bool Comm(CAN_OBJ sendmsg, out CAN_OBJ recmsg) { int sendnum; int recnum; uint mLen; sendnum = 0; recnum = 0; CAN_OBJ msg = new CAN_OBJ(); if (!isOpen) { OpenComm(); } mLen = 1; while (ECANDLL.Transmit(1, 0, 0, ref sendmsg, (ushort)mLen) != ECAN.ECANStatus.STATUS_OK) { sendnum++; Thread.Sleep(1); if (sendnum == 10) { recmsg = msg; return(false); } } while (ECANDLL.Receive(1, 0, 0, out recmsg, 1, 1) != ECAN.ECANStatus.STATUS_OK) { recnum++; Thread.Sleep(1); if (recnum == 10) { return(false); } } return(true); }
public bool Comm(CAN_OBJ sendmsg, out CAN_OBJ recmsg) { int sendnum; int recnum; uint mLen; sendnum = 0; recnum = 0; CAN_OBJ msg = new CAN_OBJ(); if (!isOpen) { StackTrace st = new StackTrace(new StackFrame(true)); LogHelper.WriteMLog("Fail to open the USB-CAN!", st); OpenComm(); } mLen = 1; while (ECANDLL.Transmit(1, 0, 0, ref sendmsg, (ushort)mLen) != ECAN.ECANStatus.STATUS_OK) { sendnum++; if (sendnum == 3) { recmsg = msg; return(false); } } Thread.Sleep(1); while (ECANDLL.Receive(1, 0, 0, out recmsg, 1, 1) != ECAN.ECANStatus.STATUS_OK) { recnum++; if (recnum == 3) { return(false); } } return(true); }
private void receive(object obj) { CAN_OBJ vco1 = new CAN_OBJ(); while (receiveRunning) { //CAN_OBJ vco1 = new CAN_OBJ(); uint result = ECANDLL.Receive(4, 0, 0, out vco1, 1, 10000); //Console.WriteLine("GetReceiveNum result : " + result); Console.WriteLine("GetReceiveNum result data: " + Untils.ToHexString(vco1.data)); if (result != 0xFFFFFFFF && result != 0) { CanDataWithInfo can = new CanDataWithInfo(vco1.ID, vco1.data, ""); queueReceiveBuffer.Enqueue(can); } else { //Console.WriteLine("receive fault"); } Thread.Sleep(10); } }
private void ReadMessagesold() { CAN_OBJ mMsg = new CAN_OBJ(); int m = 0; int sCount = 0; do { uint mLen = 1; if (!((ECANDLL.Receive(1, i, 0, out mMsg, mLen, 1) == ECANStatus.STATUS_OK) & (mLen > 0))) { break; } // m = m + 1; //if (m == 5) { m = 0; this.gRecMsgBuf[this.gRecMsgBufHead].ID = mMsg.ID; this.gRecMsgBuf[this.gRecMsgBufHead].DataLen = mMsg.DataLen; this.gRecMsgBuf[this.gRecMsgBufHead].data = mMsg.data; this.gRecMsgBuf[this.gRecMsgBufHead].ExternFlag = mMsg.ExternFlag; this.gRecMsgBuf[this.gRecMsgBufHead].RemoteFlag = mMsg.RemoteFlag; this.gRecMsgBufHead += 1; if (this.gRecMsgBufHead >= REC_MSG_BUF_MAX) { this.gRecMsgBufHead = 0; } sCount++; } }while (sCount < 50); }
/// <summary> /// 下载 /// </summary> /// <param name="data">下载的数据</param> /// <param name="ctoBuffer">返回数据</param> /// <param name="ctoBufferLength">返回数据长度</param> /// <returns></returns> public EcanXcpResult XCP_Download(byte[] data, out byte[] ctoBuffer, UInt16 ctoBufferLength) { ctoBuffer = new byte[ctoBufferLength]; CAN_OBJ frameInfo = new CAN_OBJ(); // 报文帧 UInt32 numOfFrameTX; // 实际发送帧数量 UInt32 numOfFrameRX; // 接收帧数量 #region Packet Identifier:CMD Command:DOWNLOAD // 报文帧ID为 frameInfo.ID = masterID; // 发送帧类型 正常发送 frameInfo.SendType = 0; // 是否为远程帧 0为数据帧 frameInfo.RemoteFlag = 0; // 是否为扩展帧 0为标准帧,11位帧ID frameInfo.ExternFlag = 0; // 数据长度 frameInfo.DataLen = (byte)(data.Length + 2); // CAN报文的数据 frameInfo.data = new byte[8]; // 系统保留 //CANObj.Reserved = new byte[3]; frameInfo.data[0] = 0xF0; frameInfo.data[1] = (byte)data.Length; Array.Copy(data, 0, frameInfo.data, 2, data.Length); // 将Data复制到FrameInfo if ((numOfFrameTX = ECANDLL.Transmit(deviceType, deviceIndex, canIndex, frameInfo, 1)) != 1) // 如果发送命令失败 { // Function not available / Operation not implemented return(EcanXcpResult.XCP_ERR_NOT_IMPLEMENTED); } #endregion #region 读取应答数据 for (int i = 0; i < 5; i++) // 尝试读取5次 { if ((numOfFrameRX = ECANDLL.GetReceiveNum(deviceType, deviceIndex, canIndex)) > 0) // 如果收到数据帧 { numOfFrameRX = ECANDLL.Receive(deviceType, deviceIndex, canIndex, out frameInfo, 1, 10); if (frameInfo.data[0] == 0xFF) // Packet Identifier:RES { ctoBuffer = frameInfo.data; // 回传接收到的数据 // Acknowledge / no error return(EcanXcpResult.XCP_ERR_OK); } else if (frameInfo.data[0] == 0xFE) // Packet Identifier:ERR { ctoBuffer = frameInfo.data; // 回传接收到的数据 #region Error Code switch (frameInfo.data[1]) // 返回 Error Code { case 0: return(EcanXcpResult.XCP_ERR_CMD_SYNCH); case 0x10: return(EcanXcpResult.XCP_ERR_CMD_BUSY); case 0x11: return(EcanXcpResult.XCP_ERR_DAQ_ACTIVE); case 0x12: return(EcanXcpResult.XCP_ERR_PGM_ACTIVE); case 0x20: return(EcanXcpResult.XCP_ERR_CMD_UNKNOWN); case 0x21: return(EcanXcpResult.XCP_ERR_CMD_SYNTAX); case 0x22: return(EcanXcpResult.XCP_ERR_OUT_OF_RANGE); case 0x23: return(EcanXcpResult.XCP_ERR_WRITE_PROTECTED); case 0x24: return(EcanXcpResult.XCP_ERR_ACCESS_DENIED); case 0x25: return(EcanXcpResult.XCP_ERR_ACCESS_DENIED); case 0x26: return(EcanXcpResult.XCP_ERR_PAGE_NOT_VALID); case 0x27: return(EcanXcpResult.XCP_ERR_MODE_NOT_VALID); case 0x28: return(EcanXcpResult.XCP_ERR_SEGMENT_NOT_VALID); case 0x29: return(EcanXcpResult.XCP_ERR_SEQUENCE); case 0x2A: return(EcanXcpResult.XCP_ERR_DAQ_CONFIG); case 0x30: return(EcanXcpResult.XCP_ERR_MEMORY_OVERFLOW); case 0x31: return(EcanXcpResult.XCP_ERR_GENERIC); case 0x32: return(EcanXcpResult.XCP_ERR_VERIFY); case 0x33: return(EcanXcpResult.XCP_ERR_RESOURCE_TEMPORARY_NOT_ACCESSIBLE); default: return(EcanXcpResult.XCP_ERR_INVALID_PARAMETER); } #endregion } else { ctoBuffer = frameInfo.data;// 回传接收到的数据 // Invalid parameter value return(EcanXcpResult.XCP_ERR_INVALID_PARAMETER); } } else//如果没有数据帧,等待1ms { System.Threading.Thread.Sleep(1); } } // 尝试5次,超时 // A timeout was reached by calling a function synchronously return(EcanXcpResult.XCP_ERR_INTERNAL_TIMEOUT); #endregion }
public bool Receive(out CAN_OBJ obj, uint timeout = 0) { return(ECANStatus.STATUS_OK == ECANDLL.Receive(DefaultDeviceType, 0, 0, out obj, 1, timeout)); }