Exemplo n.º 1
0
 public int getDataByte(int iByte)
 {
     if (ResponseType == OBD2Response.ResponseTypes.HexData && listData.Count > 0)
     {
         strData = (string)listData[0];
         int startIndex = iByte * 2;
         if (startIndex + 2 <= strData.Length)
         {
             return(OBD2.HexString2Int(strData.Substring(startIndex, 2)));
         }
     }
     return(-1);
 }
Exemplo n.º 2
0
    public OBD2Response(string strResp, bool bATCommand, bool bUsingHeaders, int iProtocol)
    {
        Debug.WriteLine("OBD2Response()");
        Debug.WriteLine(iProtocol.ToString());
        listECUResponses = new ArrayList();
        listData         = new ArrayList();
        listDTC          = new ArrayList();
        m_iService       = -1;
        m_iPID           = -1;
        strData          = "";
        strResponse      = strResp;
        dtTime           = DateTime.Now;
        if (strResponse != null)
        {
            if (strResponse.Length < 1)
            {
                strResponse = "";
            }
        }
        else
        {
            strResponse = "";
        }
        strResponse = strResponse.Trim();
        if (bATCommand)
        {
            return;
        }
        strResponse = strResponse.Replace("BUS", "");
        strResponse = strResponse.Replace("INIT", "");
        strResponse = strResponse.Replace("SEARCHING", "");
        strResponse = strResponse.Replace(":", "");
        strResponse = strResponse.Replace(".", "");
        strResponse = strResponse.Trim();
        if (ResponseType != OBD2Response.ResponseTypes.HexData)
        {
            return;
        }
        if (!bUsingHeaders)
        {
            m_iService = OBD2.HexString2Int(strResponse.Substring(0, 2)) - 64;
            if (m_iService != 3 && m_iService != 4 && m_iService != 7)
            {
                m_iPID = OBD2.HexString2Int(strResponse.Substring(2, 2));
            }
            int startIndex = 4;
            if (m_iService == 3 || m_iService == 4 || m_iService == 7)
            {
                startIndex = 2;
            }
            if (m_iService == 5 || m_iService == 2)
            {
                startIndex = 6;
            }
            strData = strResponse.Substring(startIndex, strResponse.Length - startIndex);
        }
        else
        {
            ArrayList arrayList1  = new ArrayList();
            string    str1        = "";
            int       startIndex1 = 0;
            if (0 < strResponse.Length)
            {
                do
                {
                    if (strResponse.Substring(startIndex1, 1).CompareTo("\r") == 0)
                    {
                        arrayList1.Add((object)str1);
                        str1 = "";
                    }
                    else
                    {
                        str1 = str1 + strResponse.Substring(startIndex1, 1);
                    }
                    ++startIndex1;
                }while (startIndex1 < strResponse.Length);
            }
            arrayList1.Add((object)str1);
            arrayList1.Sort();
            listECUResponses.Clear();
            ArrayList arrayList2 = new ArrayList();
            arrayList2.Add(arrayList1[0]);
            listECUResponses.Add((object)arrayList2);
            int    addressStartIndex = getECUAddressStartIndex(iProtocol);
            int    ecuAddressLength  = getECUAddressLength(iProtocol);
            string strB   = ((string)arrayList1[0]).Substring(addressStartIndex, ecuAddressLength);
            int    index1 = 1;
            if (1 < arrayList1.Count)
            {
                do
                {
                    if (((string)arrayList1[index1]).Substring(addressStartIndex, ecuAddressLength).CompareTo(strB) == 0)
                    {
                        arrayList2.Add(arrayList1[index1]);
                    }
                    else
                    {
                        arrayList2 = new ArrayList();
                        arrayList2.Add(arrayList1[index1]);
                        listECUResponses.Add((object)arrayList2);
                        strB = ((string)arrayList1[index1]).Substring(addressStartIndex, ecuAddressLength);
                    }
                    ++index1;
                }while (index1 < arrayList1.Count);
            }
            if (iProtocol <= 5)
            {
                if (strResponse.Length >= 8)
                {
                    m_iService = OBD2.HexString2Int(strResponse.Substring(6, 2)) - 64;
                }
                if (strResponse.Length >= 10 && m_iService != 3 && (m_iService != 4 && m_iService != 7))
                {
                    m_iPID = OBD2.HexString2Int(strResponse.Substring(8, 2));
                }
                listData.Clear();
                int index2 = 0;
                if (0 < listECUResponses.Count)
                {
                    do
                    {
                        ArrayList arrayList3 = (ArrayList)listECUResponses[index2];
                        int       num1       = 0;
                        if (arrayList3.Count > 1)
                        {
                            num1 = 2;
                        }
                        string str2   = "";
                        int    index3 = 0;
                        if (0 < arrayList3.Count)
                        {
                            int num2 = num1 + 10;
                            do
                            {
                                string str3        = (string)arrayList3[index3];
                                int    startIndex2 = num2;
                                if (m_iService == 3 || m_iService == 4 || m_iService == 7)
                                {
                                    startIndex2 = 8;
                                }
                                if (m_iService == 5 || m_iService == 2)
                                {
                                    startIndex2 = num1 + 12;
                                }
                                if (str3.Length >= startIndex2 + 4)
                                {
                                    str2 = str2 + str3.Substring(startIndex2, str3.Length + (-2 - startIndex2));
                                }
                                ++index3;
                            }while (index3 < arrayList3.Count);
                        }
                        listData.Add((object)str2);
                        if ((m_iService == 3 || m_iService == 7) && 4 <= str2.Length)
                        {
                            int startIndex2 = 0;
                            do
                            {
                                if (str2.Substring(startIndex2, 4).CompareTo("0000") != 0)
                                {
                                    listDTC.Add((object)OBD2.getDTCName(str2.Substring(startIndex2, 4)));
                                }
                                startIndex2 += 16;
                            }while (startIndex2 + 4 <= str2.Length);
                        }
                        ++index2;
                    }while (index2 < listECUResponses.Count);
                }
            }

            Debug.WriteLine("Data:");

            Debug.WriteLine(strData);
            if (iProtocol == 6 || iProtocol == 8)
            {
                listData.Clear();
                int index2 = 0;
                if (0 < listECUResponses.Count)
                {
                    do
                    {
                        ArrayList arrayList3 = (ArrayList)listECUResponses[index2];
                        int       num        = 0;
                        if (arrayList3.Count > 1)
                        {
                            num = 2;
                        }
                        string message = "";
                        int    index3  = 0;
                        if (0 < arrayList3.Count)
                        {
                            do
                            {
                                string str2 = (string)arrayList3[index3];
                                int    startIndex2;
                                if (index3 == 0)
                                {
                                    int startIndex3 = num + 5;
                                    int startIndex4 = startIndex3 + 2;
                                    startIndex2 = startIndex4 + 2;
                                    m_iService  = OBD2.HexString2Int(str2.Substring(startIndex3, 2)) - 64;
                                    if (m_iService != 3 && m_iService != 4 && m_iService != 7)
                                    {
                                        m_iPID = OBD2.HexString2Int(str2.Substring(startIndex4, 2));
                                    }
                                    else
                                    {
                                        startIndex2 -= 2;
                                    }
                                    if (m_iService == 3 || m_iService == 7)
                                    {
                                        startIndex2 += 2;
                                    }
                                    if (m_iService == 5 || m_iService == 2)
                                    {
                                        startIndex2 += 2;
                                    }
                                }
                                else
                                {
                                    startIndex2 = 5;
                                }
                                message = message + str2.Substring(startIndex2, str2.Length - startIndex2);
                                ++index3;
                            }while (index3 < arrayList3.Count);
                        }
                        listData.Add((object)message);
                        Debug.WriteLine("strData: ");
                        Debug.WriteLine(message);
                        if ((m_iService == 3 || m_iService == 7) && 4 <= message.Length)
                        {
                            int startIndex2 = 0;
                            do
                            {
                                if (message.Substring(startIndex2, 4).CompareTo("0000") != 0)
                                {
                                    listDTC.Add((object)OBD2.getDTCName(message.Substring(startIndex2, 4)));
                                }
                                startIndex2 += 4;
                            }while (startIndex2 + 4 <= message.Length);
                        }
                        ++index2;
                    }while (index2 < listECUResponses.Count);
                }
            }
            if (iProtocol == 7 || iProtocol == 9)
            {
                listData.Clear();
                int index2 = 0;
                if (0 < listECUResponses.Count)
                {
                    do
                    {
                        ArrayList arrayList3 = (ArrayList)listECUResponses[index2];
                        int       num        = 0;
                        if (arrayList3.Count > 1)
                        {
                            num = 2;
                        }
                        string str2   = "";
                        int    index3 = 0;
                        if (0 < arrayList3.Count)
                        {
                            do
                            {
                                string str3 = (string)arrayList3[index3];
                                int    startIndex2;
                                if (index3 == 0)
                                {
                                    int startIndex3 = num + 10;
                                    int startIndex4 = startIndex3 + 2;
                                    startIndex2 = startIndex4 + 2;
                                    m_iService  = OBD2.HexString2Int(str3.Substring(startIndex3, 2)) - 64;
                                    if (m_iService != 3 && m_iService != 4 && m_iService != 7)
                                    {
                                        m_iPID = OBD2.HexString2Int(str3.Substring(startIndex4, 2));
                                    }
                                    if (m_iService == 5 || m_iService == 2)
                                    {
                                        startIndex2 = 16;
                                    }
                                }
                                else
                                {
                                    startIndex2 = 10;
                                }
                                str2 = str2 + str3.Substring(startIndex2, str3.Length - startIndex2);
                                ++index3;
                            }while (index3 < arrayList3.Count);
                        }
                        listData.Add((object)str2);
                        if ((m_iService == 3 || m_iService == 7) && 4 <= str2.Length)
                        {
                            int startIndex2 = 0;
                            do
                            {
                                if (str2.Substring(startIndex2, 4).CompareTo("0000") != 0)
                                {
                                    listDTC.Add((object)OBD2.getDTCName(str2.Substring(startIndex2, 4)));
                                }
                                startIndex2 += 4;
                            }while (startIndex2 + 4 <= str2.Length);
                        }
                        ++index2;
                    }while (index2 < listECUResponses.Count);
                }
            }
            Debug.WriteLine(strResponse);
            Debug.WriteLine("listData:");

            int index4 = 0;
            if (0 < listData.Count)
            {
                do
                {
                    Debug.WriteLine((string)listData[index4]);
                    ++index4;
                }while (index4 < listData.Count);
            }
            if (m_iService != 3 && m_iService != 7)
            {
                return;
            }
            Debug.WriteLine("DTCS:");
            int index5 = 0;
            if (0 >= listDTC.Count)
            {
                return;
            }
            do
            {
                Debug.WriteLine((string)listDTC[index5]);
                ++index5;
            }while (index5 < listDTC.Count);
        }
    }