unsafe private CAN_FRAME[] GetCANFrames() { CAN_SEND_MODE mSendMode = (CAN_SEND_MODE)CAN.CAN_SEND_MODE_LIST[this.cbxSendMode.SelectedItem.ToString()]; CAN_FRAME_TYPE mFrameType = (CAN_FRAME_TYPE)CAN.CAN_FRAME_TYPE_LIST[this.cbxFrameType.SelectedItem]; CAN_FRAME_FORMAT mFrameFormat = (CAN_FRAME_FORMAT)CAN.CAN_FRAME_FORMAT_LIST[this.cbxFrameFormat.SelectedItem]; uint id = Convert.ToUInt32(tbxFrameID.Text, 16); uint mSendNum = Convert.ToUInt32(this.tbxSendNum.Text, 10); double mSendInterval = Convert.ToDouble(this.tbxSendInterval.Text); string[] sDataArr = new Regex("[\\s]+").Replace(this.tbxFrameData.Text.Trim(), " ").Split(' '); byte mDataLen = (byte)sDataArr.Length; StringBuilder sbBuilder = new StringBuilder("0x"); for (int index = mDataLen - 1; index >= 0; index--) { sbBuilder.Append(sDataArr[index].PadLeft(2, '0')); } ulong data = Convert.ToUInt64(sbBuilder.ToString(), 16); bool incID = chbxIncID.Checked; bool incData = chbxIncData.Checked; CAN_FRAME[] pCANFrames = new CAN_FRAME[mSendNum]; for (uint index = 0; index < mSendNum; index++) { CAN_OBJ pCANObj = new CAN_OBJ(); if (!incID) { pCANObj.ID = id; } else { pCANObj.ID = id + index; if (mFrameFormat == CAN_FRAME_FORMAT.STANDARD_FRAME && pCANObj.ID > CAN.STANDARD_FRAME_ID_MAXIMUM) { pCANObj.ID = CAN.STANDARD_FRAME_ID_MAXIMUM; } else if (mFrameFormat == CAN_FRAME_FORMAT.EXTENDED_FRAME && pCANObj.ID > CAN.EXTENDED_FRAME_ID_MAXIMUM) { pCANObj.ID = CAN.EXTENDED_FRAME_ID_MAXIMUM; } } //pCANObj.ID = incID ? id + index : id; pCANObj.DataLen = mDataLen; ulong ulData = incData ? data + index : data; Marshal.Copy(BitConverter.GetBytes(ulData), 0, (IntPtr)pCANObj.Data, (int)CAN.FRAME_DATA_LENGTH_MAXIMUM); pCANObj.SendType = (byte)mSendMode; pCANObj.RemoteFlag = (byte)mFrameType; pCANObj.ExternFlag = (byte)mFrameFormat; pCANObj.TimeFlag = (byte)CAN_FRAME_TIME_FLAG.INVALID; pCANObj.TimeStamp = 0; pCANFrames[index] = new CAN_FRAME(pCANObj, DateTime.Now, CAN_FRAME_DIRECTION.SEND, CAN_FRAME_STATUS.UNKNOWN); } return(pCANFrames); }
public static extern ECANStatus Receive( UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, out CAN_OBJ Receive, UInt32 length, UInt32 WaitTime);
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); }
public void SendTest() { CAN_OBJ sendMsg = new CAN_OBJ(); byte[] Msg = new byte[8]; sendMsg.SendType = 0; sendMsg.data = new byte[8]; //sendMsg.Reserved = new byte[2]; sendMsg.ID = 0x601; sendMsg.DataLen = Convert.ToByte(8); sendMsg.ExternFlag = 0; sendMsg.RemoteFlag = 0; for (int i = 0; i < Msg.Length; i++) { sendMsg.data[i] = Msg[i]; } uint mLen = 1; if (ECANDLL.Transmit(1, 0, 0, ref sendMsg, (ushort)mLen) != ECANStatus.STATUS_OK) { MessageBox.Show("fail"); } else { MessageBox.Show("sucess"); } }
public bool Send(ref CAN_OBJ obj) { if (!IsOpened) { return(false); } return(ECANStatus.STATUS_OK == ECANDLL.Transmit(DefaultDeviceType, 0, 0, ref obj, 1)); }
public override bool SendData(CANData cdin) { CAN_OBJ co = new CAN_OBJ(); co.data = cdin.GetData(); co.ID = (uint)cdin.ID; co.DataLen = (byte)cdin.getDataLen(); co.SendType = 1; co.ExternFlag = 0x01; co.RemoteFlag = 0x00; lock (this) { return(ECANDLL.Transmit(deviceType, useDeviceIndex, useCANIndex, ref co, 1) == ECANStatus.STATUS_OK); } }
public Motion() { mCan = new ComProc(); sendMsg = new CAN_OBJ(); recMsg = new CAN_OBJ(); speed = 0; position = 0; error = 0; iscomplete = true; SendTimer = new System.Timers.Timer(); SendTimer.Enabled = false; //初始化读取实时速度和位置的计时器,每5ms发送一次读取指令,读取速度和位置信息 SendTimer.AutoReset = false; SendTimer.Interval = 50; SendTimer.Elapsed += new System.Timers.ElapsedEventHandler(Send_tick); }
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; }
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); }
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 send(object obj) { CAN_OBJ frameinfo = new CAN_OBJ(); while (sendRunning) { while (!queueSendBuffer.IsEmpty) { CanDataWithInfo bi = null; bool result = queueSendBuffer.TryDequeue(out bi); //Console.WriteLine("queue count : " + queueSendBuffer.Count); if (!result || null == bi) { continue; } byte[] bySend; if (bi.bytes.Length != 8) { bySend = new byte[8]; Array.Copy(bi.bytes, 0, bySend, 0, bi.bytes.Length); } else { bySend = bi.bytes; } frameinfo.ID = bi.canId; frameinfo.SendType = sendType; frameinfo.RemoteFlag = 0; frameinfo.ExternFlag = 0; frameinfo.DataLen = (byte)bi.bytes.Length; frameinfo.data = bySend; //frameinfo.Reserved = new byte[2]; //logger.Info(Untils.ToHexString(frameinfo.data) + bi.info); //Console.WriteLine(bi.info); sendMsg(frameinfo, bi); Thread.Sleep(1); } Thread.Sleep(5); } }
private void sendMsg(CAN_OBJ frameinfo, CanDataWithInfo bi) { if (ECANDLL.Transmit(4, 0, 0, ref frameinfo, (ushort)1) != ECANStatus.STATUS_OK) { Console.WriteLine("Transmit : fault"); ERR_INFO info = new ERR_INFO(); if (ECANDLL.ReadErrInfo(4, 0, 0, out info) != ECANStatus.STATUS_OK) { Console.WriteLine("ReadErrInfo : fault"); } else { Console.WriteLine("ReadErrInfo : code " + info.ErrCode); } } else { //写入文件记录 //logger.Info(Untils.ToHexString(frameinfo.data) + bi.info); eventSended?.Invoke(this, new ByteEventArgs(DateTime.Now, new BytesWithInfo(frameinfo.data, bi.info))); } }
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); }
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 SendMessages2() { int sCount = 0; do { if (this.gSendMsgBufHead2 == this.gSendMsgBufTail2) { break; } CAN_OBJ mMsg = this.gSendMsgBuf2[this.gSendMsgBufTail2]; this.gSendMsgBufTail2 += 1; if (this.gSendMsgBufTail2 >= SEND_MSG_BUF_MAX) { this.gSendMsgBufTail2 = 0; } uint mLen = 1; if (ECANDLL.Transmit(1, i, 1, ref mMsg, (ushort)mLen) != ECANStatus.STATUS_OK) { } sCount++; }while (sCount < 200); }
private void SendMessages() { int sCount = 0; do { if (this.gSendMsgBufHead == this.gSendMsgBufTail) { break; } CAN_OBJ mMsg = this.gSendMsgBuf[this.gSendMsgBufTail]; this.gSendMsgBufTail += 1; if (this.gSendMsgBufTail >= SEND_MSG_BUF_MAX) { this.gSendMsgBufTail = 0; } uint mLen = 1; string status; if (ECANDLL.Transmit(1, 0, 0, ref mMsg, (ushort)mLen) != ECANStatus.STATUS_OK) { status = "send fail"; TxErrCnt++; if (TxErrCnt >= 0xFFFFFFFF) { TxErrCnt = 0xFFFFFFFF; } } else { status = "send success"; } sCount++; } while (sCount < 200); }
public bool Receive(out CAN_OBJ obj, uint timeout = 0) { return(ECANStatus.STATUS_OK == ECANDLL.Receive(DefaultDeviceType, 0, 0, out obj, 1, timeout)); }
private void ReadMessages() { // lock write lock canDataRWLock.EnterWriteLock(); try { // lock write lock //canDataRWLock.TryEnterWriteLock(200); CAN_OBJ frameinfo = new CAN_OBJ(); int mCount = 0; bool add2buff; /* * int i = 0, j = 0; * for (i = 0; i < dataChart.addChartCnt; i++) * { * for(j = 0; j < dataChart.addElementsCnt; j++) * { * if(dataChart.chart[i].Titles[j].Text == "S0") * * } * } * */ while (this.mCan.gRecMsgBufHead != this.mCan.gRecMsgBufTail && mCount < 50) { /* * frameinfo = this.mCan.gRecMsgBuf[this.mCan.gRecMsgBufTail]; * this.mCan.gRecMsgBufTail += 1; * if (this.mCan.gRecMsgBufTail >= ComProc.REC_MSG_BUF_MAX) * { * this.mCan.gRecMsgBufTail = 0; * } */ // add queue data index //canDataSemaphore.Wait(); if (dataChart.addChartCnt > 0) { if (monitorData.head < LevCanData.MAX_BUFF_LEN) { monitorData.head += 1; } else { monitorData.head = 0; } add2buff = true; } else { add2buff = false; } // decode data monitorData.copyData(this.mCan.gRecMsgBuf[this.mCan.gRecMsgBufTail].data, 8); monitorData.decodingData(this.mCan.gRecMsgBuf[this.mCan.gRecMsgBufTail].ID, this.mCan.gRecMsgBuf[this.mCan.gRecMsgBufTail].TimeStamp); // update data grid view //int point_id = monitorData.getCanDataLevPoint(frameinfo.ID); //if (point_id == 0) //MessageBox.Show("PointId that decoded by method decodingData(candataID) Equas to zero","Warning", MessageBoxButtons.OK); //else //updateDataGridViewCellContents(point_id); //canDataSemaphore.Release(); this.mCan.gRecMsgBufTail += 1; if (this.mCan.gRecMsgBufTail >= ComProc.REC_MSG_BUF_MAX) { this.mCan.gRecMsgBufTail = 0; } mCount++; } } finally { // unlock write lock canDataRWLock.ExitWriteLock(); } }
private void ReadMessages2() { CAN_OBJ frameinfo = new CAN_OBJ(); int mCount = 0; while (this.mCan.gRecMsgBufHead2 != this.mCan.gRecMsgBufTail2) { string tmpstr; frameinfo = this.mCan.gRecMsgBuf2[this.mCan.gRecMsgBufTail2]; this.mCan.gRecMsgBufTail2 += 1; if (this.mCan.gRecMsgBufTail2 >= ComProc.REC_MSG_BUF_MAX) { this.mCan.gRecMsgBufTail2 = 0; } string str = "Rec: "; if (frameinfo.TimeFlag == 0) { tmpstr = "Time: "; } else { tmpstr = "Time:" + string.Format("{0:X8}h", frameinfo.TimeStamp); } str = str + tmpstr; tmpstr = " ID:" + string.Format("{0:X8}h", frameinfo.ID); str = str + tmpstr + " Format:"; if (frameinfo.RemoteFlag == 0) { tmpstr = "Data "; } else { tmpstr = "Romte "; } str = str + tmpstr + " Type:"; if (frameinfo.ExternFlag == 0) { tmpstr = "Stand "; } else { tmpstr = "Exten "; } str = str + tmpstr; if (frameinfo.RemoteFlag == 0) { str = str + " Data:"; if (frameinfo.DataLen > 8) { frameinfo.DataLen = 8; } int mlen = frameinfo.DataLen - 1; for (int j = 0; j <= mlen; j++) { tmpstr = string.Format("{0:X2}h", frameinfo.data[j]); str = str + tmpstr; } } this.lbxParamsLog.Items.Add(str); if (this.lbxParamsLog.Items.Count > 500) { this.lbxParamsLog.Items.Clear(); } mCount++; if (mCount >= 50) { break; } Application.DoEvents(); } }
public void Timer_Tick(object sender, EventArgs e) { can.AddBuf1(APPID); if (!can.RxFramesBuf1[APPID].IsEmpty) { while (!can.RxFramesBuf1[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf1[APPID].Dequeue(); GSM_UpDownMotorRunReq1 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt1 = (frame.data[2] & 0x03); } } can.AddBuf2(APPID); if (!can.RxFramesBuf2[APPID].IsEmpty) { while (!can.RxFramesBuf2[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf2[APPID].Dequeue(); GSM_UpDownMotorRunReq2 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt2 = (frame.data[2] & 0x03); } } can.AddBuf3(APPID); if (!can.RxFramesBuf3[APPID].IsEmpty) { while (!can.RxFramesBuf3[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf3[APPID].Dequeue(); GSM_UpDownMotorRunReq3 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt3 = (frame.data[2] & 0x03); } } can.AddBuf4(APPID); if (!can.RxFramesBuf4[APPID].IsEmpty) { while (!can.RxFramesBuf4[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf4[APPID].Dequeue(); GSM_UpDownMotorRunReq4 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt4 = (frame.data[2] & 0x03); } } can.AddBuf5(APPID); if (!can.RxFramesBuf5[APPID].IsEmpty) { while (!can.RxFramesBuf5[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf5[APPID].Dequeue(); GSM_UpDownMotorRunReq5 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt5 = (frame.data[2] & 0x03); } } can.AddBuf6(APPID); if (!can.RxFramesBuf6[APPID].IsEmpty) { while (!can.RxFramesBuf6[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf6[APPID].Dequeue(); GSM_UpDownMotorRunReq6 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt6 = (frame.data[2] & 0x03); } } can.AddBuf7(APPID); if (!can.RxFramesBuf7[APPID].IsEmpty) { while (!can.RxFramesBuf7[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf7[APPID].Dequeue(); GSM_UpDownMotorRunReq7 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt7 = (frame.data[2] & 0x03); } } can.AddBuf8(APPID); if (!can.RxFramesBuf8[APPID].IsEmpty) { while (!can.RxFramesBuf8[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf8[APPID].Dequeue(); GSM_UpDownMotorRunReq8 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt8 = (frame.data[2] & 0x03); } } can.AddBuf9(APPID); if (!can.RxFramesBuf9[APPID].IsEmpty) { while (!can.RxFramesBuf9[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf9[APPID].Dequeue(); GSM_UpDownMotorRunReq9 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt9 = (frame.data[2] & 0x03); } } can.AddBuf10(APPID); if (!can.RxFramesBuf10[APPID].IsEmpty) { while (!can.RxFramesBuf10[APPID].IsEmpty) { CAN_OBJ frame = can.RxFramesBuf10[APPID].Dequeue(); GSM_UpDownMotorRunReq10 = (frame.data[2] & 0x30) >> 4; GSM_UpDownMotorSt10 = (frame.data[2] & 0x03); } } }
public static extern ECANStatus Transmit( UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref CAN_OBJ Send, UInt16 length);
/// <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 }