Example #1
0
 public long GetReceivedCount()
 {
     return(ECANDLL.GetReceiveNum(deviceType, useDeviceIndex, useCANIndex));
 }
Example #2
0
        /// <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
        }