Пример #1
0
        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");
            }
        }
Пример #2
0
 public bool Send(ref CAN_OBJ obj)
 {
     if (!IsOpened)
     {
         return(false);
     }
     return(ECANStatus.STATUS_OK ==
            ECANDLL.Transmit(DefaultDeviceType, 0, 0, ref obj, 1));
 }
Пример #3
0
        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);
            }
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
 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)));
     }
 }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
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
        }