Пример #1
0
        public OBDResponseList GetResponseList(OBDParameter param)
        {
            OBDResponseList responses = _obdDevice.Query(param);
            string          strItem   = "Responses: ";

            for (int i = 0; i < responses.ResponseCount; i++)
            {
                strItem += string.Format("[{0}] ", Utility.GetReadableHexString(0, responses.GetOBDResponse(i).Data));
            }
            strItem = strItem.TrimEnd();
            _log.TraceInfo(strItem);
            return(responses);
        }
Пример #2
0
        public List <OBDParameterValue> GetValueList(OBDParameter param)
        {
            List <OBDParameterValue> ValueList = new List <OBDParameterValue>();

            _log.TraceInfo("Requesting: " + param.OBDRequest);
            OBDResponseList responses = _obdDevice.Query(param);
            string          strItem   = "Responses: ";

            if (responses.ErrorDetected)
            {
                strItem += "Error Detected!";
                OBDParameterValue value = new OBDParameterValue {
                    ErrorDetected    = true,
                    StringValue      = "Error Detected in OBDResponseList!",
                    ShortStringValue = "ERROR_RESP"
                };
                ValueList.Add(value);
                _log.TraceInfo(strItem);
                return(ValueList);
            }
            else
            {
                for (int i = 0; i < responses.ResponseCount; i++)
                {
                    strItem += string.Format("[{0}] ", Utility.GetReadableHexString(0, responses.GetOBDResponse(i).Data));
                }
                strItem = strItem.TrimEnd();
                _log.TraceInfo(strItem);
            }

            for (int i = 0; i < responses.ResponseCount; i++)
            {
                OBDParameterValue obdValue = _obdInterpreter.GetValue(param, responses.GetOBDResponse(i));
                if (obdValue.ErrorDetected)
                {
                    _log.TraceError(string.Format("Values: [ECU: {0}, Error Detected!]", obdValue.ECUResponseID));
                }
                else
                {
                    _log.TraceInfo(GetLogString(param, obdValue));
                }
                ValueList.Add(obdValue);
            }
            return(ValueList);
        }
Пример #3
0
    public override OBDResponseList parse(OBDParameter param, string response)
    {
        if (response != null)
        {
            if (response.Length < 1)
            {
                response = "";
            }
        }
        else
        {
            response = "";
        }
        OBDResponseList obdResponseList = new OBDResponseList(response);

        response = strip(response);
        if (errorCheck(response))
        {
            obdResponseList.ErrorDetected = true;
            return(obdResponseList);
        }
        else
        {
            ArrayList arrayList1 = split(response);
            arrayList1.Sort();
            ArrayList arrayList2 = new ArrayList();
            ArrayList arrayList3 = new ArrayList();
            arrayList3.Add(arrayList1[0]);
            arrayList2.Add((object)arrayList3);
            string str1 = (string)arrayList1[0];
            if (str1.Length < OBDParser_ISO14230_4_KWP.HEADER_LENGTH)
            {
                obdResponseList.ErrorDetected = true;
                return(obdResponseList);
            }
            else
            {
                string strB   = str1.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
                int    index1 = 1;
                if (1 < arrayList1.Count)
                {
                    do
                    {
                        string str2 = (string)arrayList1[index1];
                        if (str2.Length >= OBDParser_ISO14230_4_KWP.HEADER_LENGTH)
                        {
                            if (str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH).CompareTo(strB) == 0)
                            {
                                arrayList3.Add((object)str2);
                            }
                            else
                            {
                                arrayList3 = new ArrayList();
                                arrayList3.Add(arrayList1[index1]);
                                arrayList2.Add((object)arrayList3);
                                strB = str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
                            }
                            ++index1;
                        }
                        else
                        {
                            goto label_14;
                        }
                    }while (index1 < arrayList1.Count);
                    goto label_15;
label_14:
                    obdResponseList.ErrorDetected = true;
                    return(obdResponseList);
                }
label_15:
                int index2 = 0;
                if (0 < arrayList2.Count)
                {
                    do
                    {
                        OBDResponse response1      = new OBDResponse();
                        ArrayList   arrayList4     = (ArrayList)arrayList2[index2];
                        int         dataStartIndex = getDataStartIndex(param);
                        string      str2           = (string)arrayList4[0];
                        int         num1           = -2 - dataStartIndex;
                        int         length1        = str2.Length + num1;
                        response1.Header = str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
                        response1.Data   = length1 > 0 ? str2.Substring(dataStartIndex, length1) : "";
                        int index3 = 1;
                        if (1 < arrayList4.Count)
                        {
                            int num2 = num1;
                            do
                            {
                                string str3    = (string)arrayList4[index3];
                                int    length2 = str3.Length + num2;
                                string str4    = length2 > 0 ? str3.Substring(dataStartIndex, length2) : "";
                                response1.Data = response1.Data + str4;
                                ++index3;
                            }while (index3 < arrayList4.Count);
                        }
                        obdResponseList.AddOBDResponse(response1);
                        ++index2;
                    }while (index2 < arrayList2.Count);
                }
                return(obdResponseList);
            }
        }
    }
Пример #4
0
        public override OBDResponseList Query(OBDParameter param)
        {
            OBDResponseList orl = _Parser.Parse(param, GetOBDResponse(param.OBDRequest));
            int             errorQty;

            for (errorQty = 0; errorQty < 2; errorQty++)
            {
                if (!orl.ErrorDetected)
                {
                    break;
                }
                if (Online)
                {
                    Thread.Sleep(500);
                }
                orl = _Parser.Parse(param, GetOBDResponse(param.OBDRequest));
            }
            // 重试3次后还是出错的话,软重启ELM327后再重试3次
            if (errorQty >= 2)
            {
                ConfirmAT("ATWS");
                InitELM327Format();
                ConfirmAT("ATSP" + ((int)_iProtocol).ToString("X1"));
                for (errorQty = 0; errorQty < 3; errorQty++)
                {
                    if (!orl.ErrorDetected)
                    {
                        break;
                    }
                    if (Online && errorQty != 0)
                    {
                        Thread.Sleep(500);
                    }
                    orl = _Parser.Parse(param, GetOBDResponse(param.OBDRequest));
                }
            }
            if (orl.RawResponse == "PENDING")
            {
                int waittingTime = 60; // 重试总时间,单位秒
                int interval     = 6;  // 重试间隔时间,单位秒
                // NRC=78 处理
                _log.TraceWarning("Receive only NRC78, handle pending message");
                switch (_iProtocol)
                {
                case ProtocolType.J1850_PWM:
                case ProtocolType.J1850_VPW:
                    // 间隔30秒重复发送一次请求,直到有正响应消息返回
                    interval = 30;
                    for (int i = waittingTime / interval; i > 0; i--)
                    {
                        Thread.Sleep(interval * 1000);
                        orl = _Parser.Parse(param, GetOBDResponse(param.OBDRequest));
                        if (!(orl.RawResponse == "PENDING" || orl.ErrorDetected))
                        {
                            break;
                        }
                    }
                    break;

                case ProtocolType.ISO9141_2:
                    // 间隔4秒重复发送一次请求,直到有正响应消息返回
                    interval = 4;
                    for (int i = waittingTime / interval; i > 0; i--)
                    {
                        Thread.Sleep(interval * 1000);
                        orl = _Parser.Parse(param, GetOBDResponse(param.OBDRequest));
                        if (!(orl.RawResponse == "PENDING" || orl.ErrorDetected))
                        {
                            break;
                        }
                    }
                    break;

                default:
                    // ISO14230-4 会在50ms内发送NRC78负响应直到发送正响应,v2.1以下的ELM327一般可收到所需要的消息,只需要过滤NRC78的负响应即可
                    // ISO15765-4 会在5s内发送NRC78负响应直到发送正响应,v2.1以下的ELM327无法收到所需要的消息,需要上层应用自己处理
                    // 间隔6s(5s加上传输延时)检测一次是否有正响应消息返回,如果没有则继续
                    for (int i = waittingTime / interval; i > 0; i--)
                    {
                        Thread.Sleep(interval * 1000);
                        string RxLine = _CommELM.GetRxLine();
                        if (RxLine != null && RxLine.Length > 0)
                        {
                            _log.TraceInfo("RX: " + RxLine.Replace("\r", "\\r"));
                            orl = _Parser.Parse(param, RxLine);
                            if (!(orl.RawResponse == "PENDING" || orl.ErrorDetected))
                            {
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            return(orl);
        }
Пример #5
0
 public override OBDResponseList parse(OBDParameter param, string response)
 {
     if (response != null)
     {
         if (response.Length < 1)
             response = "";
     }
     else
         response = "";
     OBDResponseList obdResponseList = new OBDResponseList(response);
     response = strip(response);
     if (errorCheck(response))
     {
         obdResponseList.ErrorDetected = true;
         return obdResponseList;
     }
     else
     {
         ArrayList arrayList1 = split(response);
         arrayList1.Sort();
         ArrayList arrayList2 = new ArrayList();
         ArrayList arrayList3 = new ArrayList();
         arrayList3.Add(arrayList1[0]);
         arrayList2.Add((object)arrayList3);
         string str1 = (string)arrayList1[0];
         if (str1.Length < OBDParser_ISO14230_4_KWP.HEADER_LENGTH)
         {
             obdResponseList.ErrorDetected = true;
             return obdResponseList;
         }
         else
         {
             string strB = str1.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
             int index1 = 1;
             if (1 < arrayList1.Count)
             {
                 do
                 {
                     string str2 = (string)arrayList1[index1];
                     if (str2.Length >= OBDParser_ISO14230_4_KWP.HEADER_LENGTH)
                     {
                         if (str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH).CompareTo(strB) == 0)
                         {
                             arrayList3.Add((object)str2);
                         }
                         else
                         {
                             arrayList3 = new ArrayList();
                             arrayList3.Add(arrayList1[index1]);
                             arrayList2.Add((object)arrayList3);
                             strB = str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
                         }
                         ++index1;
                     }
                     else
                         goto label_14;
                 }
                 while (index1 < arrayList1.Count);
                 goto label_15;
             label_14:
                 obdResponseList.ErrorDetected = true;
                 return obdResponseList;
             }
         label_15:
             int index2 = 0;
             if (0 < arrayList2.Count)
             {
                 do
                 {
                     OBDResponse response1 = new OBDResponse();
                     ArrayList arrayList4 = (ArrayList)arrayList2[index2];
                     int dataStartIndex = getDataStartIndex(param);
                     string str2 = (string)arrayList4[0];
                     int num1 = -2 - dataStartIndex;
                     int length1 = str2.Length + num1;
                     response1.Header = str2.Substring(0, OBDParser_ISO14230_4_KWP.HEADER_LENGTH);
                     response1.Data = length1 > 0 ? str2.Substring(dataStartIndex, length1) : "";
                     int index3 = 1;
                     if (1 < arrayList4.Count)
                     {
                         int num2 = num1;
                         do
                         {
                             string str3 = (string)arrayList4[index3];
                             int length2 = str3.Length + num2;
                             string str4 = length2 > 0 ? str3.Substring(dataStartIndex, length2) : "";
                             response1.Data = response1.Data + str4;
                             ++index3;
                         }
                         while (index3 < arrayList4.Count);
                     }
                     obdResponseList.AddOBDResponse(response1);
                     ++index2;
                 }
                 while (index2 < arrayList2.Count);
             }
             return obdResponseList;
         }
     }
 }
Пример #6
0
        public OBDResponseList Parse(OBDParameter param, string response, int headLen)
        {
            if (string.IsNullOrEmpty(response))
            {
                response = "";
            }

            OBDResponseList responseList = new OBDResponseList(response);

            response = Strip(response);
            response = ErrorFilter(response);
            if (ErrorCheck(response))
            {
                responseList.ErrorDetected = true;
                return(responseList);
            }

            List <string> legalLines = SplitByCR(response);

            legalLines = GetLegalLines(param, legalLines, headLen);
            List <string> lines = new List <string>();

            foreach (string item in legalLines)
            {
                if (item.Length > 0 && item.Length < headLen)
                {
                    // 过滤数据帧总长小于帧头长度的错误数据
                    continue;
                }
                string strNRC = GetNRC(item, headLen);
                if (strNRC.Length == 0)
                {
                    lines.Add(item);
                }
                else if (strNRC == "78")
                {
                    responseList.Pending = true;
                }
            }
            if (lines.Count == 0)
            {
                if (responseList.Pending)
                {
                    responseList.RawResponse = "PENDING";
                    return(responseList);
                }
                else
                {
                    responseList.ErrorDetected = true;
                    return(responseList);
                }
            }

            lines.Sort();
            List <List <string> > groups = new List <List <string> >();
            List <string>         group  = new List <string> {
                lines[0]
            };

            groups.Add(group);
            if (lines[0].Length < headLen)
            {
                responseList.ErrorDetected = true;
                return(responseList);
            }

            string header = lines[0].Substring(0, headLen);

            for (int i = 1; i < lines.Count; i++)
            {
                if (lines[i].Length >= headLen)
                {
                    if (lines[i].Substring(0, headLen).CompareTo(header) == 0)
                    {
                        group.Add(lines[i]);
                    }
                    else
                    {
                        group = new List <string> {
                            lines[i]
                        };
                        groups.Add(group);
                        header = lines[i].Substring(0, headLen);
                    }
                }
                else
                {
                    responseList.ErrorDetected = true;
                    return(responseList);
                }
            }
            for (int i = 0; i < groups.Count; i++)
            {
                OBDResponse obd_response = new OBDResponse();
                bool        bIsMultiline = false;
                if (groups[i].Count > 1)
                {
                    bIsMultiline = true;
                }
                int dataStartIndex = GetDataStartIndex(headLen, param, bIsMultiline);
                int length1        = groups[i][0].Length - dataStartIndex - 2;
                obd_response.Header = groups[i][0].Substring(0, headLen);
                obd_response.Data   = length1 > 0 ? groups[i][0].Substring(dataStartIndex, length1) : "";
                for (int j = 1; j < groups[i].Count; j++)
                {
                    int length2 = groups[i][j].Length - dataStartIndex - 2;
                    obd_response.Data += (length2 > 0 ? groups[i][j].Substring(dataStartIndex, length2) : "");
                }
                responseList.AddOBDResponse(obd_response);
            }
            return(responseList);
        }
Пример #7
0
 public static OBDParameterValue getValue(OBDParameter param, OBDResponseList responses, bool bEnglishUnits)
 {
     if (responses.ResponseCount == 1)
     {
         return getValue(param, responses.GetOBDResponse(0), bEnglishUnits);
     }
     if ((param.Service == 1) || (param.Service == 2))
     {
         if (((param.Parameter == 0) || (param.Parameter == 0x20)) || ((param.Parameter == 0x40) || (param.Parameter == 0x60)))
         {
             OBDParameterValue value7 = new OBDParameterValue();
             int num2 = 0;
             if (0 < responses.ResponseCount)
             {
                 do
                 {
                     OBDParameterValue value4 = getValue(param, responses.GetOBDResponse(num2), bEnglishUnits);
                     if (value4.ErrorDetected)
                     {
                         return value4;
                     }
                     int num = 0;
                     do
                     {
                         if (value4.getBitFlag(num))
                         {
                             value7.setBitFlag(num, true);
                         }
                         num++;
                     }
                     while (num < 0x20);
                     num2++;
                 }
                 while (num2 < responses.ResponseCount);
             }
             return value7;
         }
         if ((param.Parameter == 1) && (param.SubParameter == 0))
         {
             OBDParameterValue value2 = new OBDParameterValue();
             value2.BoolValue = false;
             int num5 = 0;
             if (0 < responses.ResponseCount)
             {
                 do
                 {
                     OBDParameterValue value6 = getValue(param, responses.GetOBDResponse(num5), bEnglishUnits);
                     if (value6.ErrorDetected)
                     {
                         return value6;
                     }
                     if (value6.BoolValue)
                     {
                         value2.BoolValue = true;
                     }
                     num5++;
                 }
                 while (num5 < responses.ResponseCount);
             }
             if (value2.BoolValue)
             {
                 value2.DoubleValue = 1.0;
                 value2.StringValue = "ON";
                 value2.ShortStringValue = "ON";
                 return value2;
             }
             value2.DoubleValue = 0.0;
             value2.StringValue = "OFF";
             value2.ShortStringValue = "OFF";
             return value2;
         }
         if ((param.Parameter == 1) && (param.SubParameter == 1))
         {
             OBDParameterValue value3 = new OBDParameterValue();
             value3.DoubleValue = 0.0;
             int num4 = 0;
             if (0 < responses.ResponseCount)
             {
                 do
                 {
                     OBDParameterValue value5 = getValue(param, responses.GetOBDResponse(num4), bEnglishUnits);
                     if (value5.ErrorDetected)
                     {
                         return value5;
                     }
                     value3.DoubleValue = value5.DoubleValue + value3.DoubleValue;
                     num4++;
                 }
                 while (num4 < responses.ResponseCount);
             }
             return value3;
         }
     }
     if ((param.Service != 3) && (param.Service != 7))
     {
         return getValue(param, responses.GetOBDResponse(0), bEnglishUnits);
     }
     OBDParameterValue value8 = new OBDParameterValue();
     StringCollection strings = new StringCollection();
     int index = 0;
     if (0 < responses.ResponseCount)
     {
         do
         {
             StringEnumerator enumerator = getValue(param, responses.GetOBDResponse(index), bEnglishUnits).StringCollectionValue.GetEnumerator();
             if (enumerator.MoveNext())
             {
                 do
                 {
                     strings.Add(enumerator.Current);
                 }
                 while (enumerator.MoveNext());
             }
             index++;
         }
         while (index < responses.ResponseCount);
     }
     value8.StringCollectionValue = strings;
     return value8;
 }