コード例 #1
0
ファイル: CanBus.cs プロジェクト: jfilaa/CAN_Bus
    public object ParseFrame(Ramec ramec)
    {
        object result = null;
        switch (ramec.ID)
        {
            case 0x470: // doors state
                        // 0x470 00 XX 00 00 00 – kontakty dveří
                {

                    break;
                }
            case 0x02C1: // winkers state
                         // 0x2C1 0X 00 00 00 04 – blinkry
                {
                    winkers = WinkerState.None;
                    if ((ramec.Data[0] & 1) > 1) winkers = WinkerState.LeftWinker;
                    if ((ramec.Data[0] & 2) > 1) winkers = WinkerState.RightWinker;

                    result = new CAN_0x02C1(winkers);
                    break;
                }
            case 0x0571: // Batery Voltage
                         // 0x571 XX 00 00 00 00 00 – napětí akumulátoru[V]
                {
                    //BateryVoltage = (ramec.Data[0] / 2 + 50) / 10;
                    result = new CAN_0x0571((ramec.Data[0] / 2 + 50) / 10);
                    break;
                }
            case 0x351: // Speed
                        // 0x351 00 XX YY 00 00 00 00 00 - rychlost
                {
                    //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201;
                    speed = BitConverter.ToUInt16(ramec.Data, 1) / 201;
                    result = new CAN_0x351(speed);
                    break;
                }
            case 0x359: // Speed
                        //0x359 00 XX YY 00 00 00 00 00 - rychlost
                {
                    //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201;
                    speed = BitConverter.ToUInt16(ramec.Data, 1) / 201;
                    result = new CAN_0x359(speed);
                    break;
                }
            case 0x35B: // RPM, Cooling Temp
                        // 0x35B 00 XX YY ZZ 00 00 00 – otáčky motoru + teplota vody
                {
                    if (ramec.Data[0] != 0x07)
                    {
                        //RPM = (UInt16)((ramec.Data[2] * 256 + ramec.Data[1]) / 4);
                        RPM = (UInt16)(BitConverter.ToUInt16(ramec.Data, 1) / 4);
                        CoolingTemp = ramec.Data[3] - 10;
                        result = new CAN_0x35B(RPM, CoolingTemp);
                    }
                    break;
                }
            case 0x054B: // get distance from parking sensors
                {
                    distance.FrontLeft = ramec.Data[0];
                    distance.FrontRight = ramec.Data[1];
                    distance.RearLeft = ramec.Data[2];
                    distance.RearRight = ramec.Data[3];
                    distance.FrontLeft = ramec.Data[4];
                    distance.FrontRight = ramec.Data[5];
                    distance.RearLeft = ramec.Data[6];
                    distance.RearRight = ramec.Data[7];
                    result = new CAN_0x054B(distance);
                    break;
                }
            case 0x65F: // mh to bude VIN (oh, it's VIN, it need's special work)
                {
                    byte[] pole;
                    switch (ramec.Data[0]) // first part of VIN
                    {
                        case 0:
                            VIN_Ready = false;
                            pole = ramec.Data.Skip(5).Take(3).ToArray(); // take 3 bytes from 6st byte
                            VIN = System.Text.Encoding.Default.GetString(pole);
                            result = new CAN_0x065F(VIN);
                            break;
                        case 1:
                            if (VIN.Length >= 3)
                            {
                                pole = ramec.Data.Skip(1).Take(7).ToArray(); // take 7 bytes from firts byte
                                string partOfVin = System.Text.Encoding.Default.GetString(pole);
                                VIN = VIN.Substring(0, 3) + partOfVin; // replace midle part of VIN
                                result = new CAN_0x065F(partOfVin);
                            }
                            break;
                        case 2:
                            if (VIN.Length >= (3 + 7))
                            {
                                pole = ramec.Data.Skip(1).Take(7).ToArray(); // the same
                                VIN = VIN.Substring(0, 3 + 7) + System.Text.Encoding.Default.GetString(pole); // replace last part of VIN
                                VIN_Ready = true;
                                result = new VIN_Code(VIN);
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                }
            case 0x065D: // oh, data from Odometer, date (sometimes) and time
                {
                    int Hour = (ramec.Data[5] & 0xF0) / 16 + (ramec.Data[6] & 1) * 16;
                    int Min = (ramec.Data[6] & 0x7E) / 2;
                    int Sec = (ramec.Data[7] & 0x1F) * 2 + (ramec.Data[6] & 0x80) / 128;
                    int Year = ramec.Data[3] / 128 + (ramec.Data[4] & 0x07) * 128;
                    int Month = (ramec.Data[4] & 0x78) / 8;
                    int Day = (ramec.Data[4] & 0x80) / 128 + (ramec.Data[5] & 0x0F) * 2;

                    if (Year == 0) Year = DateTime.Now.Year; // if date not supported by car the fill today date
                    if (Month == 0) Month = DateTime.Now.Month;
                    if (Day == 0) Day = DateTime.Now.Day;
                    Time = new DateTime(Year, Month, Day, Hour, Min, Sec);
                    //Odometer = (UInt32)(ramec.Data[1] + ramec.Data[2] * 256 + (ramec.Data[3] & 0x0F) * 256 * 256);
                    ramec.Data[3] &= 0x0F;
                    Odometer = BitConverter.ToUInt32(ramec.Data, 1);
                    result = new CAN_0x065D(Time, Odometer);
                    break;
                }
            default:
                break;
        }
        return result;
    }
コード例 #2
0
ファイル: CanBus.cs プロジェクト: k1132/CAN_Bus
    public object ParseFrame(Ramec ramec)
    {
        object result = null;

        switch (ramec.ID)
        {
        case 0x470:     // doors state
                        // 0x470 00 XX 00 00 00 – kontakty dveří
        {
            break;
        }

        case 0x02C1:     // winkers state
                         // 0x2C1 0X 00 00 00 04 – blinkry
        {
            winkers = WinkerState.None;
            if ((ramec.Data[0] & 1) > 1)
            {
                winkers = WinkerState.LeftWinker;
            }
            if ((ramec.Data[0] & 2) > 1)
            {
                winkers = WinkerState.RightWinker;
            }

            result = new CAN_0x02C1(winkers);
            break;
        }

        case 0x0571:     // Batery Voltage
                         // 0x571 XX 00 00 00 00 00 – napětí akumulátoru[V]
        {
            //BateryVoltage = (ramec.Data[0] / 2 + 50) / 10;
            result = new CAN_0x0571((ramec.Data[0] / 2 + 50) / 10);
            break;
        }

        case 0x351:     // Speed
                        // 0x351 00 XX YY 00 00 00 00 00 - rychlost
        {
            //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201;
            speed  = BitConverter.ToUInt16(ramec.Data, 1) / 201;
            result = new CAN_0x351(speed);
            break;
        }

        case 0x359:     // Speed
                        //0x359 00 XX YY 00 00 00 00 00 - rychlost
        {
            //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201;
            speed  = BitConverter.ToUInt16(ramec.Data, 1) / 201;
            result = new CAN_0x359(speed);
            break;
        }

        case 0x35B:     // RPM, Cooling Temp
                        // 0x35B 00 XX YY ZZ 00 00 00 – otáčky motoru + teplota vody
        {
            if (ramec.Data[0] != 0x07)
            {
                //RPM = (UInt16)((ramec.Data[2] * 256 + ramec.Data[1]) / 4);
                RPM         = (UInt16)(BitConverter.ToUInt16(ramec.Data, 1) / 4);
                CoolingTemp = ramec.Data[3] - 10;
                result      = new CAN_0x35B(RPM, CoolingTemp);
            }
            break;
        }

        case 0x054B:     // get distance from parking sensors
        {
            distance.FrontLeft  = ramec.Data[0];
            distance.FrontRight = ramec.Data[1];
            distance.RearLeft   = ramec.Data[2];
            distance.RearRight  = ramec.Data[3];
            distance.FrontLeft  = ramec.Data[4];
            distance.FrontRight = ramec.Data[5];
            distance.RearLeft   = ramec.Data[6];
            distance.RearRight  = ramec.Data[7];
            result = new CAN_0x054B(distance);
            break;
        }

        case 0x65F:     // mh to bude VIN (oh, it's VIN, it need's special work)
        {
            byte[] pole;
            switch (ramec.Data[0])         // first part of VIN
            {
            case 0:
                VIN_Ready = false;
                pole      = ramec.Data.Skip(5).Take(3).ToArray();        // take 3 bytes from 6st byte
                VIN       = System.Text.Encoding.Default.GetString(pole);
                result    = new CAN_0x065F(VIN);
                break;

            case 1:
                if (VIN.Length >= 3)
                {
                    pole = ramec.Data.Skip(1).Take(7).ToArray();       // take 7 bytes from firts byte
                    string partOfVin = System.Text.Encoding.Default.GetString(pole);
                    VIN    = VIN.Substring(0, 3) + partOfVin;          // replace midle part of VIN
                    result = new CAN_0x065F(partOfVin);
                }
                break;

            case 2:
                if (VIN.Length >= (3 + 7))
                {
                    pole      = ramec.Data.Skip(1).Take(7).ToArray();                                   // the same
                    VIN       = VIN.Substring(0, 3 + 7) + System.Text.Encoding.Default.GetString(pole); // replace last part of VIN
                    VIN_Ready = true;
                    result    = new VIN_Code(VIN);
                }
                break;

            default:
                break;
            }
            break;
        }

        case 0x065D:     // oh, data from Odometer, date (sometimes) and time
        {
            int Hour  = (ramec.Data[5] & 0xF0) / 16 + (ramec.Data[6] & 1) * 16;
            int Min   = (ramec.Data[6] & 0x7E) / 2;
            int Sec   = (ramec.Data[7] & 0x1F) * 2 + (ramec.Data[6] & 0x80) / 128;
            int Year  = ramec.Data[3] / 128 + (ramec.Data[4] & 0x07) * 128;
            int Month = (ramec.Data[4] & 0x78) / 8;
            int Day   = (ramec.Data[4] & 0x80) / 128 + (ramec.Data[5] & 0x0F) * 2;

            if (Year == 0)
            {
                Year = DateTime.Now.Year;                    // if date not supported by car the fill today date
            }
            if (Month == 0)
            {
                Month = DateTime.Now.Month;
            }
            if (Day == 0)
            {
                Day = DateTime.Now.Day;
            }
            Time = new DateTime(Year, Month, Day, Hour, Min, Sec);
            //Odometer = (UInt32)(ramec.Data[1] + ramec.Data[2] * 256 + (ramec.Data[3] & 0x0F) * 256 * 256);
            ramec.Data[3] &= 0x0F;
            Odometer       = BitConverter.ToUInt32(ramec.Data, 1);
            result         = new CAN_0x065D(Time, Odometer);
            break;
        }

        default:
            break;
        }
        return(result);
    }