Example #1
0
        /// <summary>
        /// Receive data from device
        /// </summary>
        /// <param name="what"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        private async Task <OdbResponse> receiveDataFromDevice(OdbPid what, DateTime start)
        {
            //await for response
            await Task.Delay(this.getReponseByPid(what));

            //create response
            OdbResponse odbResponse = new OdbResponse();

            odbResponse.Pid = what;

            try
            {
                //reader
                uint loaded = await reader.LoadAsync(BUFFER_STEP);

                String response = reader.ReadString(reader.UnconsumedBufferLength);
                while (loaded == BUFFER_STEP)
                {
                    loaded = await reader.LoadAsync(BUFFER_STEP);

                    response += reader.ReadString(reader.UnconsumedBufferLength);
                }
                odbResponse.Response = this.clearResponse(response, what);
                odbResponse.IsValid  = this.isValidResponse(response, what);
            }
            catch
            {
                odbResponse.Response = "";
                odbResponse.IsValid  = false;
            }

            odbResponse.Time = DateTime.Now.Subtract(start);

            return(odbResponse);
        }
Example #2
0
        /// <summary>
        /// Send message and check response
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public async Task <OdbResponse> SendAndCheck(OdbPid what)
        {
            OdbResponse response = await this.Send(what);

            if (!response.Response.Contains(what.ExpectedResponse))
            {
                throw new OdbException(OdbError.WrongResponseFromDevice);
            }
            return(response);
        }
Example #3
0
        /// <summary>
        /// Send message, return bytes response
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public async Task <OdbResponse> Send(OdbPid what)
        {
            if (!this.IsConnected)
            {
                throw new OdbException(OdbError.DeviceIsNotConnected);
            }

            //timer
            DateTime start = DateTime.Now;

            //send
            try
            {
                writer.WriteString(what.Pid + "\r\r");
                await writer.StoreAsync();

                await writer.FlushAsync();
            }
            catch
            {
                this.IsConnected = false;
                throw new OdbException(OdbError.DeviceIsNotConnected);
            }

            //receive data from device
            OdbResponse odbResponse = await receiveDataFromDevice(what, start);

            //try again on error reponse
            while (!odbResponse.IsValid && this.tryCount > 0)
            {
                reporter.ReportInfo("Incorrect response '" + odbResponse.Response + "' from device. Try another request. Current try step is " + this.tryCount + ".");
                odbResponse = await receiveDataFromDevice(what, start);

                this.tryCount--;
            }
            this.tryCount = RESPONSE_TRY_COUNT;

            //report response to console
            reporter.ReportResponse(odbResponse);

            return(odbResponse);
        }
 /// <summary>
 /// resolve data
 /// </summary>
 /// <param name="response"></param>
 /// <param name="what"></param>
 /// <returns></returns>
 public OdbData ResolveData(OdbResponse response, OdbPid what)
 {
     return this.ResolveData(response.Response, what);
 }
        /// <summary>
        /// Receive data from device
        /// </summary>
        /// <param name="what"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        private async Task<OdbResponse> receiveDataFromDevice(OdbPid what, DateTime start)
        {            
            //await for response
            await Task.Delay(this.getReponseByPid(what));

            //create response
            OdbResponse odbResponse = new OdbResponse();
            odbResponse.Pid = what;

            try
            {
                //reader
                uint loaded = await reader.LoadAsync(BUFFER_STEP);

                String response = reader.ReadString(reader.UnconsumedBufferLength);
                while (loaded == BUFFER_STEP)
                {
                    loaded = await reader.LoadAsync(BUFFER_STEP);
                    response += reader.ReadString(reader.UnconsumedBufferLength);
                }
                odbResponse.Response = this.clearResponse(response, what);
                odbResponse.IsValid = this.isValidResponse(response, what);
            }
            catch
            {
                odbResponse.Response = "";
                odbResponse.IsValid = false;
            }

            odbResponse.Time = DateTime.Now.Subtract(start);

            return odbResponse;
        }
Example #6
0
        /// <summary>
        /// report response
        /// </summary>
        /// <param name="odbResponse"></param>
        public void ReportResponse(OdbResponse odbResponse)
        {
            if (!OdbClient.OBD_REPORTER_ENABLED)
            {
                return;
            }

            if (odbResponse.IsValid)
            {
                Debug.WriteLine(ResponsePrefix + odbResponse.Pid.Pid + " (" + odbResponse.Pid.Description + ") > " + odbResponse.Response);
            }
            else
            {
                Debug.WriteLine(ErrorPrefix + odbResponse.Pid.Pid + " (" + odbResponse.Pid.Description + ") > Invalid response from OBD device.");
            }
        }
Example #7
0
        /// <summary>
        /// Resolve incoming data and setup odb data
        /// </summary>
        /// <param name="response"></param>
        /// <param name="what"></param>
        /// <returns></returns>
        public OdbData ResolveData(OdbResponse response, OdbPid what)
        {
            int counter = 0;
            String[] bytes = response.Response.Split(' ');
            OdbData data = OdbPids.GetResponseFormatForProtocolNumber(this.SelectedProtocol, what.ByteCount);

            if (bytes.Length < what.ByteCount)
            {
                return null;
            }

            try
            {
                data.Protocol = OdbPids.GetPidForProtocolNumber(this.SelectedProtocol);
                for (int i = 0; i < data.Header.Length; i++)
                {
                    data.Header[i] = bytes[counter];
                    counter++;
                }
                for (int i = 0; i < data.Info.Length; i++)
                {
                    data.Info[i] = bytes[counter];
                    counter++;
                }
                for (int i = 0; i < data.Data.Length; i++)
                {
                    data.Data[i] = bytes[counter];
                    counter++;
                }
                for (int i = 0; i < data.Ender.Length; i++)
                {
                    data.Ender[i] = bytes[counter];
                    counter++;
                }
            }
            catch
            {
                return null;
            }
            return data;
        }
Example #8
0
 /// <summary>
 /// resolve data
 /// </summary>
 /// <param name="response"></param>
 /// <param name="what"></param>
 /// <returns></returns>
 public OdbData ResolveData(OdbResponse response, OdbPid what)
 {
     return(this.ResolveData(response.Response, what));
 }