0

I am working on a tracking project at the end point.i stuck. I have some ASCII data to i want to convert that data and want to get vehicle status. The data what i got from GPS tracking device.

*HQ,1700082911,V1,084938,A,2304.5102,N,07231.5528,E,003.7,179.11,270917,FFFFFBFF#

Now i got everything what i need from this script. but as per device document instruction i am not able to find vehicle status.there is a ASCII code string in above data which is FFFFFBFF for this what document says i mentioned below.

vehicle_status(FFFFFBFF): Vehicle Status, 4 bytes, indicate the status of the vehicle running, Accessory status or Alarm status etc. ASCII symbol indicate the status, the following is the definition of every bit, Bit=0 means active.

And as per this instruction they have given me a table.Sorry i cant draw a table here so i have attached a screen shot GPS tracking device document instruction here to get vehicle status .

hope so you have understand my question i have a string FFFFFBFF# this is ASCII i want to convert it.

Shakti Sisodiya
  • 218
  • 2
  • 5
  • 14
  • That's an ASCII representation of a number in hex, and you need to interpret the individual bits of that number. So, first convert it to a number, and then apply [bit operations](http://php.net/manual/en/language.operators.bitwise.php) – fvu Oct 06 '17 at 07:02
  • related: https://stackoverflow.com/questions/2791869/most-efficient-way-to-extract-bit-flags https://stackoverflow.com/questions/3511709/what-is-the-correct-way-to-check-if-bit-field-is-turn-on-in-php – fvu Oct 06 '17 at 07:05

1 Answers1

0

This code extracts the bits and matches these with the table you have given. I only filled out some information as an example.

<?php

$hex = 'FFFFFBFF';

$stati = [
    // byte nr
    0 => [
        // byte 1
        0 => 'Tempearature alarm',
        1 => 'Password error alarm',
        2 => 'GPRS error alarm',
        3 => 'Turn off engine',
        4 => 'Power down alarm',
        5 => 'High triggered sensor 1 is high',
        6 => 'High triggered sensor 2 is high',
        7 => 'Low triggered sensor 1 is GND',
    ],
    1 => [
        // byte 2
        0 => 'GPS fault alarm',
        1 => 'Battery low alarm',
        2 => 'nop',
        3 => 'Use battery backup',
        4 => 'Battery removed',
        5 => 'No GPS antenna',
        6 => 'GPS antenna shot',
        7 => 'Low triggered sensor 2 is GND',
    ],
    2 => [
        // byte 3
        0 => 'Door open',
        1 => 'Arm',
        2 => 'ACC off',
        3 => 'nop',
        4 => 'nop',
        5 => 'Engine start',
        6 => 'nop',
        7 => 'Over speed',
    ],
    3 => [
        // byte 4
        0 => 'Shock alarm',
        1 => 'SOS alarm',
        2 => 'Over alarm',
        3 => 'Start engine alarm',
        4 => 'Enter Geo-fence alarm',
        5 => 'No GPS antenna',
        6 => 'GPS antenna shot',
        7 => 'Break Geo-fence alarm',
    ],
];

foreach (str_split($hex, 2) as $byte_nr => $byte) {

    $bits = str_pad(base_convert($byte, 16, 2), 8, '0', STR_PAD_LEFT);
    foreach (str_split($bits) as $bit_nr => $bit) {

        if ($bit == 0) {
            echo $stati[$byte_nr][$bit_nr].PHP_EOL; // echos "Engine start"
        }
    }
}
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18