Пример #1
0
        /// <summary>
        /// get can bus response data
        /// </summary>
        /// <param name="destID"></param>
        /// <param name="source"></param>
        /// <param name="command"></param>
        /// <param name="waitTime"></param>
        /// <param name="receiveDataLen"></param>
        /// <returns></returns>
        public byte[] getCanBusResponseData(int destID, int source, int command, double waitTime, ref int receiveDataLen)
        {
            CanbusMessage message = getCanBusResponseData(destID, source, command, waitTime);

            if (message != null)
            {
                receiveDataLen = message.DataLen;
                return(message.ByteData);
            }
            else
            {
                receiveDataLen = 0;
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// get can bus response data
        /// </summary>
        /// <param name="destID"></param>
        /// <param name="source"></param>
        /// <param name="command"></param>
        /// <param name="waitTime"></param>
        /// <param name="receiveDataLen"></param>
        /// <returns></returns>
        public CanbusMessage getCanBusResponseData(int destID, int source, int command, double waitTime)
        {
            TPCANMsg CANMsg;
            // While this mode is selected
            TPCANTimestamp CANTimeStamp;
            TPCANStatus    stsResult;
            DateTime       now      = DateTime.Now;
            DateTime       end      = now.AddSeconds(waitTime);
            int            flag_num = 0;

            lock (this.lockObjForPowerOnThreadAndMessageReq)
            {
                while (this.isRunning)
                {
                    now = DateTime.Now;
                    if (now > end)    // if waiting time is over 1.5s, we think it has no response
                    {
                        return(null); //
                    }
                    // Waiting for Receive-Event
                    //if (m_ReceiveEvent.WaitOne(50))
                    {
                        do
                        {
                            stsResult = PCANBasic.Read(this._canChannel, out CANMsg, out CANTimeStamp);
                            if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                            {
                                if (destID != -1 && CANMsg.ID != destID)
                                {
                                    continue;                                       // it was not from specify node
                                }
                                if (CANMsg.LEN < 1)
                                {
                                    continue;
                                }

                                if (source != -1 && CANMsg.DATA[1] != source)
                                {
                                    continue;
                                }
                                if (command != -1 && CANMsg.DATA[0] != command)
                                {
                                    continue;
                                }

                                //receiveDataLen = CANMsg.LEN;
                                CanbusMessage message = new CanbusMessage(DateTime.Now, (int)CANMsg.ID, CANMsg.LEN, CANMsg.DATA);
                                return(message);
                            }
                            flag_num++;
                            if (flag_num >= 1000)
                            {
                                flag_num = 0;
                                now      = DateTime.Now;
                                if (now > end)    // if waiting over time, we think it has no response
                                {
                                    return(null); //
                                }
                            }
                        } while (this.isRunning && (!Convert.ToBoolean(stsResult & TPCANStatus.PCAN_ERROR_QRCVEMPTY)));
                    }
                }
            }
            return(null);
        }