Ejemplo n.º 1
0
        /// <summary>
        /// This method sends a KWP request and returns a KWPReply. The method returns
        /// when a reply has been received, after a failure or after a timeout.
        /// The open and startSession methods must be called and returned possitive result
        /// before this method is used.
        /// </summary>
        /// <param name="a_request">The KWPRequest.</param>
        /// <param name="r_reply">The reply to the KWPRequest.</param>
        /// <returns>RequestResult.</returns>
        public override RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply)
        {
            string sendString    = "";
            string receiveString = "";

            byte[] reply = new byte[0xFF];
            try
            {
                for (int i = 1; i < a_request.getData().Length; i++)
                {
                    string tmpStr = a_request.getData()[i].ToString("X");
                    if (tmpStr.Length == 1)
                    {
                        sendString += "0" + tmpStr + " ";
                    }
                    else
                    {
                        sendString += tmpStr + " ";
                    }
                }
                sendString += "\r";

                m_serialPort.Write(sendString);
                //receiveString = "5A 90 59 53 33 45 46 35 39 45 32 33 33 30 32 30 38 32 37 \r\n\r\n";// m_serialPort.ReadTo(">");
                receiveString = m_serialPort.ReadTo(">");
                string tmpString = receiveString;

                int    insertPos = 1;
                string subString = "";

                while (receiveString.Length > 4)
                {
                    int index = receiveString.IndexOf(" ");
                    subString        = receiveString.Substring(0, index);
                    reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16);
                    insertPos++;
                    receiveString = receiveString.Remove(0, index + 1);
                }
                insertPos--;

                reply[0] = (byte)insertPos; //Length

                r_reply = new KWPReply(reply, a_request.getNrOfPID());
                return(RequestResult.NoError);
            }
            catch (Exception)
            {
                r_reply = new KWPReply(reply, a_request.getNrOfPID());
                return(RequestResult.ErrorSending);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method sends a KWP request and returns a KWPReply. The method returns
        /// when a reply has been received, after a failure or after a timeout.
        /// The open and startSession methods must be called and returned possitive result
        /// before this method is used.
        /// </summary>
        /// <param name="a_request">The KWPRequest.</param>
        /// <param name="r_reply">The reply to the KWPRequest.</param>
        /// <returns>RequestResult.</returns>
        public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply)
        {
            string sendString    = "";
            string receiveString = "";

            for (int i = 1; i < a_request.getData().Length; i++)
            {
                sendString += a_request.getData()[i].ToString("X") + " ";
            }
            sendString += "\r";
            m_serialPort.Write(sendString);
            //receiveString = "49 01 01 00 00 00 31 \n\r49 02 02 44 34 47 50 \n\r49 02 03 30 30 52 35 \n\r49 02 04 25 42";// m_serialPort.ReadTo(">");
            receiveString = m_serialPort.ReadTo(">");
            char[] chrArray  = receiveString.ToCharArray();
            byte[] reply     = new byte[0xFF];
            int    insertPos = 1;
            int    index     = 0;
            string subString = "";

            while (receiveString.Length > 4)
            {
                //Remove first three bytes

                //TODO. Remove Mode and PIDs
                for (int i = 0; i < 3; i++)
                {
                    index         = receiveString.IndexOf(" ");
                    receiveString = receiveString.Remove(0, index + 1);
                }
                //Read data for the rest of the row.
                for (int i = 0; i < 4; i++)
                {
                    index = receiveString.IndexOf(" ");
                    if (index == 0) //Last row not 4 bytes of data.
                    {
                        continue;
                    }
                    subString        = receiveString.Substring(0, index);
                    reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16);
                    insertPos++;
                    receiveString = receiveString.Remove(0, index + 1);
                }
            }

            reply[0] = (byte)insertPos; //Length

            r_reply = new KWPReply(reply, a_request.getNrOfPID());
            return(RequestResult.NoError);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method sets up the address and length for writing to flash. It must be called before
        /// the sendWriteDataRequest method is called.
        /// </summary>
        /// <param name="a_address">The addres to start writing to.</param>
        /// <param name="a_length">The length to write</param>
        /// <returns>KWPResult</returns>
        public KWPResult sendWriteRequest(uint a_address, uint a_length)
        {
            LogDataString("sendWriteRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;
            byte[] addressAndLength = new byte[7];
            //set address (byte 0 to 2)
            addressAndLength[0] = (byte)(a_address >> 16);
            addressAndLength[1] = (byte)(a_address >> 8);
            addressAndLength[2] = (byte)(a_address);
            //set length (byte 3 to 6);
            addressAndLength[3] = (byte)(a_length >> 24);
            addressAndLength[4] = (byte)(a_length >> 16);
            addressAndLength[5] = (byte)(a_length >> 8);
            addressAndLength[6] = (byte)(a_length);

            //Send request
            //Mode = 0x34
            //PID = no PID used by this request
            //Data = aaallll (aaa = address, llll = length)
            //Expected result = 0x74
            result = sendRequest(new KWPRequest(0x34, addressAndLength), out reply);
            if (result != KWPResult.OK)
                return result;
            if (reply.getMode() != 0x74)
                return KWPResult.NOK;
            else
                return result;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Send unknown request
        /// </summary>
        /// <returns>KWPResult</returns>
        public KWPResult sendUnknownRequest()
        {
            LogDataString("sendUnknownRequest");
            //Console.WriteLine("sendUnknownRequest");
            KWPReply reply = new KWPReply();
            KWPResult result;

            //Send request
            //Mode = 0x3E
            //PID = no PID used by this request
            //Expected result = 0x7E
            result = sendRequest(new KWPRequest(0x3E), out reply);
            if (reply.getMode() != 0x7E)
                return KWPResult.NOK;
            else
                return result;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method send a request to download the symbol map.
        /// After this request has been made data can be fetched with a data transfer request
        /// </summary>
        /// <param name="a_data">The data to be written.</param>
        /// <returns>KWPResult</returns>
        public KWPResult sendReadSymbolMapRequest()
        {
            LogDataString("sendReadSymbolMapRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;

            //Send request
            //Mode = 0x31
            //PID = 0x50
            //Data = data to be flashed
            //Expected result = 0x71
            result = sendRequest(new KWPRequest(0x31, 0x50), out reply);
            if (reply.getMode() != 0x71)
                return KWPResult.NOK;
            else
                return result;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// sendEraseRequest sends an erase request to the ECU.
        /// This method must be called before the ECU can be flashed.
        /// </summary>
        /// <returns>KWPResult</returns>
        public KWPResult sendEraseRequest()
        {
            LogDataString("sendEraseRequest");

            KWPReply reply = new KWPReply();
            KWPReply reply2 = new KWPReply();
            KWPResult result = KWPResult.Timeout;
            int i = 0;

            //First erase message. Up to 5 retries.
            //Mode = 0x31
            //PID = 0x52
            //Expected result is 0x71
            result = sendRequest(new KWPRequest(0x31, 0x52), out reply);
            Console.WriteLine("Erase(1) " + reply.ToString());
            if (result != KWPResult.OK)
                return result;
            while (reply.getMode() != 0x71)
            {
                System.Threading.Thread.Sleep(1000);
                result = sendRequest(new KWPRequest(0x31, 0x52), out reply);
                Console.WriteLine("Erase(2:" + i.ToString() + ") " + reply.ToString());
                if (i++ > 15) return KWPResult.Timeout;
            }
            if (result != KWPResult.OK)
                return result;

            //Second erase message. Up to 10 retries.
            //Mode = 0x31
            //PID = 0x53
            //Expected result is 0x71
            i = 0;
            result = sendRequest(new KWPRequest(0x31, 0x53), out reply2);
            Console.WriteLine("Erase(3) " + reply2.ToString());
            if (result != KWPResult.OK)
                return result;
            while (reply2.getMode() != 0x71)
            {
                System.Threading.Thread.Sleep(1000);
                result = sendRequest(new KWPRequest(0x31, 0x53), out reply2);
                Console.WriteLine("Erase(4:" + i.ToString() + ") " + reply2.ToString());
                if (i++ > 20) return KWPResult.Timeout;
            }

            //Erase confirm message
            //Mode = 0x3E
            //Expected result is 0x7E
            result = sendRequest(new KWPRequest(0x3E, 0x53), out reply2);
            Console.WriteLine("Erase(5) " + reply2.ToString());

            return result;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This method requests data to be transmitted.
        /// </summary>
        /// <param name="a_data">The data to be transmitted.</param>
        /// <returns>KWPResult</returns>
        public KWPResult sendDataTransferRequest(out byte[] a_data)
        {
            LogDataString("sendDataTransferRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;

            //Send request
            //Mode = 0x36
            //PID = no PID used by this request
            //Data = no data
            //Expected result = 0x76
            result = sendRequest(new KWPRequest(0x36), out reply);
            a_data = reply.getData();
            if (reply.getMode() != 0x76)
                return KWPResult.NOK;
            else
                return result;
        }
Ejemplo n.º 8
0
 public bool ResetECU()
 {
     LogDataString("ResetECU");
     KWPReply reply = new KWPReply();
     KWPResult result;
     byte[] data = new byte[1];
     data[0] = (byte)0x00;
     KWPRequest req = new KWPRequest(0x11, 0x01, data);
     Console.WriteLine(req.ToString());
     result = sendRequest(req, out reply);
     if (reply.getMode() == 0x51)
     {
         Console.WriteLine("Reset Success: " + reply.ToString());
         return true;
     }
     else if (reply.getMode() == 0x7F)
     {
         Console.WriteLine("Reset Failed: " + reply.ToString());
     }
     return false;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// This method sends a KWP request and returns a KWPReply. The method returns
 /// when a reply has been received, after a failure or after a timeout.
 /// The open and startSession methods must be called and returned possitive result
 /// before this method is used.
 /// </summary>
 /// <param name="a_request">The KWPRequest.</param>
 /// <param name="r_reply">The reply to the KWPRequest.</param>
 /// <returns>RequestResult.</returns>
 public abstract RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply);
Ejemplo n.º 10
0
        /// <summary>
        /// This method sends a KWPRequest and returns a KWPReply.
        /// </summary>
        /// <param name="a_request">The request.</param>
        /// <param name="a_reply">The reply.</param>
        /// <returns>KWPResult</returns>
        private KWPResult sendRequest(KWPRequest a_request, out KWPReply a_reply, int expectedLength)
        {
            int _maxSendRetries = 3;
            KWPResult _kwpResult = KWPResult.Timeout;
            LogDataString("sendRequest");

            KWPReply reply = new KWPReply();
            RequestResult result;
            a_reply = new KWPReply();
            //<GS-11012010> was allemaal 1000
            int keepAliveTimeout = 1000;
            //Console.WriteLine("Checking KWP device open");
            if (!m_kwpDevice.isOpen())
                return KWPResult.DeviceNotConnected;

            // reset the timer for keep alive (set to 1 seconds now)
            if (stateTimer == null)
                stateTimer = new System.Threading.Timer(sendKeepAlive, new Object(), keepAliveTimeout, keepAliveTimeout);
            stateTimer.Change(keepAliveTimeout, keepAliveTimeout);

            m_requestMutex.WaitOne();
            int _retryCount = 0;
            result = RequestResult.Unknown; // <GS-11022010>
            while (_retryCount < _maxSendRetries && result != RequestResult.NoError)
            {
                LogDataString(a_request.ToString());
                result = m_kwpDevice.sendRequest(a_request, out reply);
                if ((int)reply.getLength() != expectedLength)
                {
                    result = RequestResult.InvalidLength;

                }
                if (result == RequestResult.NoError)
                {
                    a_reply = reply;
                    LogDataString(reply.ToString());
                    LogDataString(""); // empty line
                    m_requestMutex.ReleaseMutex();
                    //return KWPResult.OK;
                    _kwpResult = KWPResult.OK;
                }
                else
                {
                    LogDataString("Timeout in KWPHandler::sendRequest");
                    m_requestMutex.ReleaseMutex();
                    //return KWPResult.Timeout;
                    _kwpResult = KWPResult.Timeout;
                }
            }
            return _kwpResult;
        }
Ejemplo n.º 11
0
 public bool ClearDTCCodes()
 {
     LogDataString("ClearDTCCodes");
     KWPReply reply = new KWPReply();
     KWPResult result;
     byte[] data = new byte[1];
     data[0] = (byte)0xFF;
     //data[1] = (byte)0xFF;
     //data[2] = (byte)0x00;
     KWPRequest req = new KWPRequest(0x14, 0xFF, data);
     Console.WriteLine(req.ToString());
     result = sendRequest(req, out reply);
     Console.WriteLine(reply.ToString());
     return true;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// This method sends a KWPRequest and returns a KWPReply.
        /// </summary>
        /// <param name="a_request">The request.</param>
        /// <param name="a_reply">The reply.</param>
        /// <returns>KWPResult</returns>
        private KWPResult sendRequest(KWPRequest a_request, out KWPReply a_reply)
        {
            LogDataString("sendRequest");

            KWPReply reply = new KWPReply();
            RequestResult result;
            a_reply = new KWPReply();
            //<GS-11012010> was allemaal 1000
            int keepAliveTimeout = 1000;
            //Console.WriteLine("Checking KWP device open");
            if (!m_kwpDevice.isOpen())
                return KWPResult.DeviceNotConnected;

            // reset the timer for keep alive (set to 1 seconds now)
            if (stateTimer == null)
                stateTimer = new System.Threading.Timer(sendKeepAlive, new Object(), keepAliveTimeout, keepAliveTimeout);
            stateTimer.Change(keepAliveTimeout, keepAliveTimeout);

            m_requestMutex.WaitOne();

            LogDataString(a_request.ToString());

            result = m_kwpDevice.sendRequest(a_request, out reply);
            a_reply = reply;
            if (result == RequestResult.NoError)
            {
                LogDataString(reply.ToString());
                LogDataString(""); // empty line

                m_requestMutex.ReleaseMutex();
                return KWPResult.OK;
            }
            else
            {
                LogDataString("Timeout in KWPHandler::sendRequest: " + result.ToString());
                Console.WriteLine("Timeout in KWPHandler::sendRequest: " + result.ToString());
                m_requestMutex.ReleaseMutex();
                return KWPResult.Timeout;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Send a request for a sequrity access with one out of two methods to 
        /// calculate the key.
        /// </summary>
        /// <param name="a_method">Key calculation method [1,2]</param>
        /// <returns>true if sequrity access was granted, otherwise false</returns>
        private bool requestSequrityAccessLevel(uint a_method)
        {
            LogDataString("requestSequrityAccessLevel: " + a_method.ToString());
            KWPReply reply = new KWPReply();
            KWPResult result;
            byte[] seed = new byte[2];
            byte[] key = new byte[2];
            // Send a seed request.
            KWPRequest requestForKey = new KWPRequest(0x27, 0x05);
            Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() +  " request for key: " + requestForKey.ToString());
            result = sendRequest(requestForKey, out reply);
            Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " request for key result: " + reply.ToString());

            if (result != KWPResult.OK)
                return false;
            if (reply.getData().Length < 2)
                return false;
            seed[0] = reply.getData()[0];
            seed[1] = reply.getData()[1];
            if (a_method == 1)
                key = calculateKey(seed, 0);
            else
                key = calculateKey(seed, 1);
            // Send key reply.
            KWPRequest sendKeyRequest = new KWPRequest(0x27, 0x06, key);
            Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() +  " send Key request: " + sendKeyRequest.ToString());
            result = sendRequest(sendKeyRequest, out reply);
            Console.WriteLine("requestSequrityAccessLevel " + a_method.ToString() + " send Key reply: " + reply.ToString());
            if (result != KWPResult.OK)
            {
                Console.WriteLine("Security access request was not send");
                return false;
            }

            //Check if sequrity was granted.
            Console.WriteLine("Mode: " + reply.getMode().ToString("X2"));
            Console.WriteLine("Data: " + reply.getData()[0].ToString("X2"));
            if ((reply.getMode() == 0x67) && (reply.getData()[0] == 0x34)) // WAS [0]
            {
                Console.WriteLine("Security access granted: " + a_method.ToString());
                return true;
            }

            Console.WriteLine("Security access was not granted: " + reply.ToString());
            return false;
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Send a KWP request.
        /// </summary>
        /// <param name="a_request">A KWP request.</param>
        /// <param name="r_reply">A KWP reply.</param>
        /// <returns>The status of the request.</returns>
        public override RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply)
        {
            uint row;
            uint all_rows = row = nrOfRowsToSend(a_request.getData());

            m_kwpCanListener.setupWaitMessage(0x258);

            // Send one or several request messages.
            for (; row > 0; row--)
            {
                CANMessage msg = new CANMessage(0x240, 0, 8);
                msg.elmExpectedResponses = a_request.ElmExpectedResponses;
                msg.setData(createCanMessage(a_request.getData(), row - 1));
                if ((msg.getData() & 0xFFFFUL) == 0xA141UL)
                {
                    msg.elmExpectedResponses = 0;
                }
                if (all_rows == 22)
                {
                    msg.elmExpectedResponses = row == 1 ? 1 : 0; // on last message (expect 1 reply)
                }
                if (!m_canDevice.sendMessage(msg))
                {
                    r_reply = new KWPReply();
                    return(RequestResult.ErrorSending);
                }
            }

            var response = m_kwpCanListener.waitMessage(getTimeout());

            // Receive one or several replys and send an ack for each reply.
            if (response.getID() == 0x258)
            {
                uint nrOfRows = (uint)(response.getCanData(0) & 0x3F) + 1;
                row = 0;
                if (nrOfRows == 0)
                {
                    throw new Exception("Wrong nr of rows");
                }
                //Assume that no KWP reply contains more than 0x200 bytes
                byte[] reply = new byte[0x200];
                reply = collectReply(reply, response.getData(), row);
                sendAck(nrOfRows - 1);
                nrOfRows--;

                m_kwpCanListener.setupWaitMessage(0x258);

                while (nrOfRows > 0)
                {
                    response = m_kwpCanListener.waitMessage(getTimeout());
                    if (response.getID() == 0x258)
                    {
                        row++;
                        reply = collectReply(reply, response.getData(), row);
                        sendAck(nrOfRows - 1);
                        nrOfRows--;
                    }
                    else
                    {
                        logger.Debug("1response.getID == " + response.getID());
                        r_reply = new KWPReply();
                        return(RequestResult.Timeout);
                    }
                }
                r_reply = new KWPReply(reply, a_request.getNrOfPID());
                return(RequestResult.NoError);
            }
            else
            {
                logger.Debug("2response.getID == " + response.getID());
                r_reply = new KWPReply();
                return(RequestResult.Timeout);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// This method send a request for reading a symbol. 
        /// </summary>
        /// <param name="a_address">The symbol number to read [0..0xFFFF-1].</param>
        /// <returns></returns>
        public bool setSymbolRequest(uint a_symbolNumber)
        {
            LogDataString("setSymbolRequest");

            KWPReply reply = new KWPReply();
            KWPResult result = KWPResult.Timeout;
            byte[] symbolNumber = new byte[5];
            //First two bytes should be zero
            symbolNumber[0] = 0;
            symbolNumber[1] = 0;
            symbolNumber[2] = 0x80;
            //set symbol number (byte 2 to 3);
            symbolNumber[3] = (byte)(a_symbolNumber >> 8);
            symbolNumber[4] = (byte)(a_symbolNumber);
            //<GS-11022010>
            // check result .. it should be 2 bytes long
            int _retryCount = 0;
            while (_retryCount++ < 3 && result != KWPResult.OK)
            {
                //result = sendRequest(new KWPRequest(0x2C, 0xF0, 0x03, symbolNumber), out reply);
                result = sendRequest(new KWPRequest(0x2C, 0xF0, 0x03, symbolNumber), out reply/*, 2*/);
                if (reply.getLength() != 2)
                {
                    result = KWPResult.Timeout;
                    LogDataString("Got wrong response on sendRequest in setSymbolRequest, len = " + reply.getLength().ToString("D2"));
                    Console.WriteLine("Got wrong response on sendRequest in setSymbolRequest, len = " + reply.getLength().ToString("D2"));
                }
            }
            if (result == KWPResult.OK)
            {
                return true;
            }
            else
            {
                LogDataString("setSymbolRequest timed out");
                Console.WriteLine("setSymbolRequest timed out");
                return false;
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// This method writes to a symbol in RAM.
        /// The ECU must not be write protected for this to work.
        /// </summary>
        /// <param name="a_symbolNumber">Symbol number to write to.</param>
        /// <param name="a_data">Data to write.</param> 
        /// <returns></returns>
        public bool writeSymbolRequestAddress(uint a_address, byte[] a_data)
        {
            LogDataString("writeSymbolRequest: " + a_address.ToString("X8") + "len: " + a_data.Length.ToString("X4"));

            KWPReply reply = new KWPReply();
            KWPResult result;
            byte[] symbolNumberAndData = new byte[4 + a_data.Length];
            //symbolNumberAndData[0] = (byte)(a_address >> 24);
            symbolNumberAndData[0] = (byte)(a_address >> 16);
            symbolNumberAndData[1] = (byte)(a_address >> 8);
            symbolNumberAndData[2] = (byte)(a_address);
            symbolNumberAndData[3] = (byte)(a_data.Length);
            // len len
            // adr adr adr

            for (int i = 0; i < a_data.Length; i++)
                symbolNumberAndData[i + 4] = a_data[i];

            string requestString = "RequestString: ";
            foreach (byte b in symbolNumberAndData)
            {
                requestString += b.ToString("X2") + " ";
            }
            Console.WriteLine(requestString);
            // end dump to console
            Console.WriteLine("SymbolNumberAndData length: " + symbolNumberAndData.Length.ToString("X8"));
            KWPRequest t_request = new KWPRequest(0x3D, /*0x81, */symbolNumberAndData);
            Console.WriteLine(t_request.ToString());
            result = sendRequest(t_request, out reply);
            if (result != KWPResult.OK)
            {
                Console.WriteLine("Result != KWPResult.OK");
                return false;
            }
            Console.WriteLine("Result = " + reply.getData()[0].ToString("X2"));
            Console.WriteLine("Result-total = " + reply.ToString());
            if (reply.getData()[0] == 0x7D)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Force adaption for E85.
 /// </summary>
 /// <returns>KWPResult</returns>
 public KWPResult forceE85Adaption()
 {
     KWPReply reply = new KWPReply();
     KWPResult result;
     byte[] data = new byte[2];
     data[0] = 0;
     data[1] = 0;
     result = sendRequest(new KWPRequest(0x3B, 0xA6, data), out reply);
     if (result != KWPResult.OK)
         return result;
     byte[] data2 = new byte[1];
     data2[0] = 1;
     result = sendRequest(new KWPRequest(0x3B, 0xA5, data2), out reply);
     return result;
 }
Ejemplo n.º 18
0
 public bool ReadFreezeFrameData(uint frameNumber)
 {
     LogDataString("ReadFreezeFrameData");
     KWPReply reply = new KWPReply();
     KWPResult result;
     byte[] data = new byte[1];
     data[0] = (byte)0x00;
     //data[1] = (byte)0x00;
     //data[2] = (byte)0x00;
     KWPRequest req = new KWPRequest(0x12, Convert.ToByte(frameNumber), data);
     Console.WriteLine(req.ToString());
     result = sendRequest(req, out reply);
     Console.WriteLine(reply.ToString());
     return true;
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Get E85 adaption status.
 /// </summary>          
 /// <param name="r_status">The adaptino status for E85.</param>
 /// <returns>KWPResult</returns>
 public KWPResult getE85AdaptionStatus(out string r_status)
 {
     KWPReply reply = new KWPReply();
     KWPResult result;
     r_status = "Error";
     result = sendRequest(new KWPRequest(0x21, 0xA5), out reply);
     if (result == KWPResult.OK)
     {
         byte[] res = reply.getData();
         if(reply.getData()[0] == 1)
             r_status = "Forced";
         if(reply.getData()[0] == 2)
             r_status = "Ongoing";
         if(reply.getData()[0] == 3)
             r_status = "Completed";
         if(reply.getData()[0] == 4)
             r_status = "Unknown";
         if(reply.getData()[0] == 5)
             r_status = "Not started";
         return KWPResult.OK;
     }
     else
     {
         r_status = "";
         return KWPResult.Timeout;
     }
 }
Ejemplo n.º 20
0
        /// <summary>
        /// This method sends a request to exit data transfer exit. It should be called when a 
        /// read session has been finished.
        /// </summary>
        /// <returns>true on success, otherwise false.</returns>
        public bool sendDataTransferExitRequest()
        {
            LogDataString("sendDataTransferExitRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;
            result = sendRequest(new KWPRequest(0x82, 0x00), out reply);
            if (result == KWPResult.OK)
                return true;
            else
                return false;
        }
Ejemplo n.º 21
0
        /// <summary>
        /// This method requests the E85 level.
        /// </summary>
        /// <param name="r_level">The requested E85 level.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getE85Level(out float r_level)
        {
            KWPReply reply = new KWPReply();
            KWPResult result;
            float level;

            result = sendRequest(new KWPRequest(0x21, 0xA7), out reply); // Request Diagnostic Data Mode $21 - Offset (1 byte)
            if (reply.getMode() == 0x61 && reply.getPid() == 0xA7 && reply.getLength() == 4)
            {
                level = (reply.getData()[0] << 8) | reply.getData()[1];
                r_level = level / 10;
                return KWPResult.OK;
            }
            else if (reply.getMode() == 0x7F && reply.getPid() == 0x21 && reply.getLength() == 3)
            {
                Console.WriteLine(TranslateErrorCode(reply.getData()[0]));
            }
            r_level = 0;
            return KWPResult.NOK;
        }
Ejemplo n.º 22
0
        public KWPResult sendEraseMemorySpaceRequest(Int32 StartAddress, Int32 Length)
        {
            /*02 FLASH memory erasure
            1 start address (high byte)
            2 start address (middle byte)
            3 start address (low byte)
            4 stop address (high byte)
            5 stop address (middle byte)
            6 stop address (low byte)
            */
            Int32 StopAddress = StartAddress + Length;
            LogDataString("sendEraseRequest");

            KWPReply reply = new KWPReply();
            KWPReply reply2 = new KWPReply();
            KWPResult result = KWPResult.Timeout;

            //First erase message. Up to 5 retries.
            //Mode = 0x31
            //PID = 0x51
            //Expected result is 0x71
            byte[] a_data = new byte[6];
            int bt = 0;
            //a_data[0] = (byte)(StartAddress >> 24);
            a_data[bt++] = (byte)(StartAddress >> 16);
            a_data[bt++] = (byte)(StartAddress >> 8);
            a_data[bt++] = (byte)(StartAddress);
            //a_data[4] = (byte)(Length >> 24);
            a_data[bt++] = (byte)(StopAddress >> 16);
            a_data[bt++] = (byte)(StopAddress >> 8);
            a_data[bt++] = (byte)(StopAddress);

            requestSequrityAccess(true);
            KWPRequest req = new KWPRequest(0x31, 0x50, a_data);

            result = sendRequest(req, out reply);
            Console.WriteLine("Erase(1) " + reply.ToString());
            /*if (result != KWPResult.OK)
                return result;
            System.Threading.Thread.Sleep(10000);
            result = sendRequest(new KWPRequest(0x31, 0x53, a_data), out reply);
            Console.WriteLine("Erase(2:" + i.ToString() + ") " + reply.ToString());
            */
            if (result != KWPResult.OK)
                return result;

            result = sendRequest(new KWPRequest(0x3E, 0x50), out reply2); // tester present???
            Console.WriteLine("reply on exit " + reply2.ToString());

            return result;
        }
Ejemplo n.º 23
0
        /// <summary>
        /// This method sends a request for the engine type description.
        /// </summary>
        /// <param name="r_swVersion">The requested engine type description.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getEngineType(out string r_swVersion)
        {
            LogDataString("getEngineType");

            KWPReply reply = new KWPReply();
            KWPResult result;
            r_swVersion = "";
            result = sendRequest(new KWPRequest(0x1A, 0x97), out reply);
            if (result != KWPResult.OK)
                return result;
            r_swVersion = getString(reply);
            return result;
        }
Ejemplo n.º 24
0
        /// <summary>
        /// This method send a request for reading from ECU memory (both RAM and flash). 
        /// It sets up start address and the length to read.
        /// </summary>
        /// <param name="a_address">The address to start reading from.</param>
        /// <param name="a_length">The total length to read.</param>
        /// <returns>true on success, otherwise false</returns>
        public bool sendReadRequest(uint a_address, uint a_length)
        {
            LogDataString("sendReadRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;
            byte[] lengthAndAddress = new byte[5];
            //set length (byte 0 and 1)
            lengthAndAddress[0] = (byte)(a_length >> 8);
            lengthAndAddress[1] = (byte)(a_length);
            //set address (byte 2 to 4);
            lengthAndAddress[2] = (byte)(a_address >> 16);
            lengthAndAddress[3] = (byte)(a_address >> 8);
            lengthAndAddress[4] = (byte)(a_address);
            result = sendRequest(new KWPRequest(0x2C, 0xF0, 0x03, lengthAndAddress), out reply);
            if (result == KWPResult.OK)
                return true;
            else
                return false;
        }
Ejemplo n.º 25
0
        /// <summary>
        /// This method sends a request for the immobilizer ID.
        /// </summary>
        /// <param name="r_immo">The requested immo ID.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getImmo(out string r_immo)
        {
            LogDataString("getImmo");

            KWPReply reply = new KWPReply();
            KWPResult result;
            r_immo = "";
            result = sendRequest(new KWPRequest(0x1A, 0x92), out reply);
            if (result != KWPResult.OK)
                return result;
            r_immo = getString(reply);
            return result;
        }
Ejemplo n.º 26
0
        /// <summary>
        /// This method send a request to receive data from flash. The sendReadRequest
        /// method must be called before this.
        /// </summary>
        /// <param name="r_data">The requested data.</param>
        /// <returns></returns>
        public bool sendRequestDataByOffset(out byte[] r_data)
        {
            LogDataString("sendRequestDataByOffset");

            KWPReply reply = new KWPReply();
            KWPResult result;
            result = sendRequest(new KWPRequest(0x21, 0xF0), out reply);
            r_data = reply.getData();
            if (result == KWPResult.OK)
                return true;
            else
                return false;
        }
Ejemplo n.º 27
0
        /// <summary>
        /// This method sends a request for the software part number.
        /// </summary>
        /// <param name="r_swPartNo">The requested sofware part number.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getSwPartNumber(out string r_swPartNo)
        {
            LogDataString("getSwPartNumber");

            KWPReply reply = new KWPReply();
            KWPResult result;
            result = sendRequest(new KWPRequest(0x1A, 0x94), out reply);
            r_swPartNo = getString(reply);
            return result;
        }
Ejemplo n.º 28
0
        /// <summary>
        /// This method send data to be written to flash. sendWriteRequest must be called before this method.
        /// </summary>
        /// <param name="a_data">The data to be written.</param>
        /// <returns>KWPResult</returns>
        public KWPResult sendWriteDataRequest(byte[] a_data)
        {
            LogDataString("sendWriteDataRequest");

            KWPReply reply = new KWPReply();
            KWPResult result;

            //Send request
            //Mode = 0x36
            //PID = no PID used by this request
            //Data = data to be flashed
            //Expected result = 0x76
            result = sendRequest(new KWPRequest(0x36, a_data), out reply);
            if (reply.getMode() != 0x76)
                return KWPResult.NOK;
            else
                return result;
        }
Ejemplo n.º 29
0
        /// <summary>
        /// This method sends a request for the software version using diagnostic routine 0x51.
        /// </summary>
        /// <param name="r_swVersion">The requested software version.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getSwVersionFromDR51(out string r_swVersion)
        {
            LogDataString("getSwVersionFromDR51");

            KWPReply reply = new KWPReply();
            KWPResult result;
            r_swVersion = "";
            result = sendRequest(new KWPRequest(0x31, 0x51), out reply);
            if (result != KWPResult.OK)
                return result;
            r_swVersion = getString(reply);
            return result;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// This method sets the E85 level.
        /// </summary>
        /// <param name="a_level">The E85 level.</param>
        /// <returns>KWPResult</returns>
        public KWPResult setE85Level(int a_level)
        {
            KWPReply reply = new KWPReply();
            KWPResult result;
            int sendlevel = a_level * 10;
            byte[] level = new byte[2];
            level[0] = (byte)(sendlevel >> 8);
            level[1] = (byte)sendlevel;
            result = sendRequest(new KWPRequest(0x3B, 0xA7, level), out reply);
            if(reply.getMode() == 0x7B && reply.getPid() == 0xA7)
            {
                return KWPResult.OK;
            }
            else if(reply.getMode() == 0x7F && reply.getPid() == 0x3B && reply.getLength() == 3)
            {
                Console.WriteLine(TranslateErrorCode(reply.getData()[0]));
            }

            return KWPResult.NOK;
        }
Ejemplo n.º 31
0
        /// <summary>
        /// This method sends a request to get the offset of the symbol table.
        /// </summary>
        /// <param name="r_immo">Start address of the symbol table.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getSymbolTableOffset(out UInt16 r_offset)
        {
            LogDataString("getSymbolTableOffset");

            KWPReply reply = new KWPReply();
            KWPResult result;
            r_offset = 0;
            result = sendRequest(new KWPRequest(0x1A, 0x9B), out reply);
            if (result != KWPResult.OK)
                return result;
            r_offset = getUint16(reply);
            return result;
        }
Ejemplo n.º 32
0
        /// <summary>
        /// This method writes to a symbol in RAM.
        /// The ECU must not be write protected for this to work.
        /// </summary>
        /// <param name="a_symbolNumber">Symbol number to write to.</param>
        /// <param name="a_data">Data to write.</param> 
        /// <returns></returns>
        public bool writeSymbolRequest(uint a_symbolNumber, byte[] a_data)
        {
            LogDataString("writeSymbolRequest: " + a_symbolNumber.ToString());

            KWPReply reply = new KWPReply();
            KWPResult result;
            int LengthToTx = a_data.Length;
            if (LengthToTx > 0x41) LengthToTx = 0x41; // FA
            byte[] symbolNumberAndData = new byte[3 + /*a_data.Length*/ LengthToTx];
            //First two bytes should be the symbol number
            symbolNumberAndData[0] = (byte)(a_symbolNumber >> 8);
            symbolNumberAndData[1] = (byte)(a_symbolNumber);
            symbolNumberAndData[2] = (byte)(0);
            // len len
            // adr adr adr

            for (int i = 0; i < /*a_data.Length*/ LengthToTx; i++)
                symbolNumberAndData[i + 3] = a_data[i];

            string requestString = "RequestString: ";
            foreach (byte b in symbolNumberAndData)
            {
                requestString += b.ToString("X2") + " ";
            }
            Console.WriteLine(requestString);
            // end dump to console
            Console.WriteLine("SymbolNumberAndData length: " + symbolNumberAndData.Length.ToString("X8"));
            KWPRequest t_request = new KWPRequest(0x3D, 0x80, symbolNumberAndData);
            Console.WriteLine(t_request.ToString());
            result = sendRequest(t_request, out reply);
            if (result != KWPResult.OK)
            {
                Console.WriteLine("Result != KWPResult.OK");
                return false;
            }
            Console.WriteLine("Result = " + reply.getData()[0].ToString("X2"));
            Console.WriteLine("Resulttotal = " + reply.ToString());
            if (reply.getData()[0] == 0x7D)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Ejemplo n.º 33
0
        /// <summary>
        /// This method sends a request for the VIN (Vehicle ID Number).
        /// </summary>
        /// <param name="r_vin">The requested VIN.</param>
        /// <returns>KWPResult</returns>
        public KWPResult getVIN(out string r_vin)
        {
            LogDataString("getVIN");

            KWPReply reply = new KWPReply();
            KWPResult result;
            result = sendRequest(new KWPRequest(0x1A,0x90), out reply);
            if (result == KWPResult.OK)
            {
                r_vin = getString(reply);
                return KWPResult.OK;
            }
            else
            {
                r_vin = "";
                return KWPResult.Timeout;
            }
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Send a KWP request.
        /// </summary>
        /// <param name="a_request">A KWP request.</param>
        /// <param name="r_reply">A KWP reply.</param>
        /// <returns>The status of the request.</returns>
        public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply)
        {
            CANMessage msg = new CANMessage(0x240, 0, 8);
            uint       row = nrOfRowsToSend(a_request.getData());

            m_kwpCanListener.setupWaitMessage(0x258);

            // Send one or several request messages.
            for (; row > 0; row--)
            {
                msg.setData(createCanMessage(a_request.getData(), row - 1));
                if (!m_canDevice.sendMessage(msg))
                {
                    r_reply = new KWPReply();
                    return(RequestResult.ErrorSending);
                }
            }

            msg = m_kwpCanListener.waitMessage(timeoutPeriod);
            //         msg = m_kwpCanListener.waitForMessage(0x258, timeoutPeriod);

            // Receive one or several replys and send an ack for each reply.
            if (msg.getID() == 0x258)
            {
                uint nrOfRows = (uint)(msg.getCanData(0) & 0x3F) + 1;
                row = 0;
                if (nrOfRows == 0)
                {
                    throw new Exception("Wrong nr of rows");
                }
                //Assume that no KWP reply contains more than 0x200 bytes
                byte[] reply = new byte[0x200];
                reply = collectReply(reply, msg.getData(), row);
                sendAck(nrOfRows - 1);
                nrOfRows--;

                m_kwpCanListener.setupWaitMessage(0x258);

                while (nrOfRows > 0)
                {
//                    msg = m_kwpCanListener.waitForMessage(0x258, timeoutPeriod);
                    msg = m_kwpCanListener.waitMessage(timeoutPeriod);
                    if (msg.getID() == 0x258)
                    {
                        row++;
                        reply = collectReply(reply, msg.getData(), row);
                        sendAck(nrOfRows - 1);
                        nrOfRows--;
                    }
                    else
                    {
                        r_reply = new KWPReply();
                        return(RequestResult.Timeout);
                    }
                }
                r_reply = new KWPReply(reply, a_request.getNrOfPID());
                return(RequestResult.NoError);
            }
            else
            {
                r_reply = new KWPReply();
                return(RequestResult.Timeout);
            }
        }
Ejemplo n.º 35
0
        public bool ReadDTCCodes(out List<string> list)
        {
            list = new List<string>();
            LogDataString("ReadDTCCodes");
            KWPReply reply = new KWPReply();
            KWPResult result;
            byte[] data = new byte[2];
            data[0] = (byte)0xFF;
            data[1] = (byte)0xFF;
            //data[2] = (byte)0x00;
            // Status byte
            // 7 Warning lamp illuminated for this code
            // 6 Warning lamp pending for this code, not illuminate but malfunction was detected
            // 5 Warning lamp was previously illuminated for this code, malfunction not currently detected, code not yet erased
            // 4 Stored trouble code
            // 3 Manufacturer specific status
            // 2 Manufacturer specific status
            // 1 Current code - present at time of request
            // 0 Maturing/intermittent code - insufficient data to consider as a malfunction
            KWPRequest req = new KWPRequest(0x18 , 0x02); // Request Diagnostic Trouble Codes by Status
            Console.WriteLine(req.ToString());
            result = sendRequest(req, out reply);
            Console.WriteLine(reply.ToString());
            // J2190
            // Multiple Mode $58 response messages may be reported to a single request, depending on the number of diagnostic
            // trouble codes stored in the module. Each response message will report up to three DTCs for
            // which at least one of the requested status bits is set. If no codes are stored in the module that meet the
            // requested status, then the module will respond with the following:
            // Reply: 58,00
            if (reply.getMode() == 0x58)
            {
                if (reply.getPid() == 0x00)
                {
                    Console.WriteLine("No DTC's");
                    list.Add("No DTC's");
                    return true;
                }
                else
                {
                    //P0605
                    //P1231
                    //P1230
                    //P1530
                    //P1606
                    //P1460
                    //+		reply	{Reply:   14,58,
                    // 06,
                    // 06,05,E4,
                    // 12,31,48,
                    // 12,30,E8,
                    // 15,30,E1,
                    // 16,06,E8,
                    // 14,60,41}	TrionicCANLib.KWP.KWPReply
                    uint number = reply.getPid();
                    byte[] dtc = new byte[number*2];

                    byte[] read = reply.getData();
                    int j = 0;
                    int i = 0;
                    while(i < read.Length)
                    {
                        dtc[j++] = read[i++];
                        dtc[j++] = read[i++];
                        i++;
                    }

                    for (int n = 0; n < dtc.Length; n = n + 2)
                    {
                        list.Add("DTC: P" + dtc[n].ToString("X2") + dtc[n+1].ToString("X2"));
                    }
                }

            }

            return false;
        }