/// <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; } }
/// <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; }
/// <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; }
/// <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; }
/// <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; logger.Trace("sendRequest"); KWPReply reply = new KWPReply(); RequestResult result; a_reply = new KWPReply(); //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.Change(keepAliveTimeout, keepAliveTimeout); } m_requestMutex.WaitOne(); int _retryCount = 0; result = RequestResult.Unknown; // <GS-11022010> while (_retryCount < _maxSendRetries && result != RequestResult.NoError) { logger.Trace(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; logger.Trace(reply.ToString()); logger.Trace(""); // empty line m_requestMutex.ReleaseMutex(); //return KWPResult.OK; _kwpResult = KWPResult.OK; } else { logger.Trace("Error in KWPHandler::sendRequest" + result.ToString() + " " + reply.ToString()); m_requestMutex.ReleaseMutex(); //return KWPResult.Timeout; _kwpResult = KWPResult.Timeout; } } return _kwpResult; }