RonEskinder RonEskinder - 1 month ago 7
PHP Question

Format N_STRING parameter from TCP data

I have a GPS device sending packets to my server (Linux with PHP listener) via TCP, i have all the parameters but one, I/O State.

The tk207 Protocol defines that parameter as this:

IO State
Length: 8 bits
Type: N_STRING
1:Main power, '0' means on power, '1' means off power。
2:ACC, '0' ACC close, '1' ACC open
3: blender, '0' Did not.start, '1' Just turning, '2' reverse turning。
4:Empty/heavy vehicles, '0' Did not.start, '1' Empty, '2' Heavy
5:Front door, '0' Did not start, ’1’ open, '2' close。
6: off the oil state, '1' '0' recovery oil
7: arm / disarm state, '1' '0' from the 8 fortification: there is no vibration


N_STRING definition: Contain the digit character string of 0.9. When fix digits, fill in ASCII code 0(Ox30H) on left for lacking digit except for special instruction.

I am getting values like this EDITED*

01000000
01000001
11000000
11000001


But i am unable to figure out how to get the 7 parameters from that string.

Any help will be appreciated.

EDIT:
Before i had the definition about the field, i was looking at some examples on how to retreive the on/off value from this field

$gpioInput = 0;
for ($i = 0; $i <= 7; $i++) {
$b = substr($gpioStr, 7 - $i, 1);
if ($b != "0") {
$gpioInput |= (1 << $i);
}
}

Answer

My understanding you tried to get access to a "global" value, which is an aggregation of all the fields $gpioInput |= (1 << $i); If it's so and it's "Off" when any digit is zero, then next code should be helpful.

<?php

const TK207_MAIN_POWER_ON = 0;
const TK207_MAIN_POWER_OFF = 1;

const TK207_ACC_CLOSE = 0;
const TK207_ACC_OPEN = 1;

const TK207_BLENDER_DID_NOT_START = 0;
const TK207_BLENDER_JUST_TURNING = 1;
const TK207_BLENDER_REVERSE_TURNING = 2;

const TK207_VEHICLES_DID_NOT_START = 0;
const TK207_VEHICLES_EMPTY = 1;
const TK207_VEHICLES_HEAVY = 2;

const TK207_FRONT_DOOR_DID_NOT_START = 0;
const TK207_FRONT_DOOR_OPEN = 1;
const TK207_FRONT_DOOR = 2;

const TK207_OFF_THE_OIL_OFF = 0;
const TK207_OFF_THE_OIL_RECOVERY = 1;

const TK207_STATE_ARM = 0; // ???
const TK207_STATE_DISARM = 1; // ???

//-------------------------------

function isOn($str) {
    return (int)array_reduce(str_split($str), function($carry, $item) {
            $carry &= (bool)$item;
            return $carry;
    }, 1);
}

function getValues($str) {
    return str_split($str);
}

function getOnOffValues($str) {
    return array_map(function ($item) {
        return (int)(bool)$item;
    }, str_split($str));
}

//-------------------------------

$offStr = '11210211';
$onStr  = '11211211';

echo isOn($offStr); // off

echo isOn($onStr); // on

list($mainPower, $acc, $blender, $vehicles, $frontDoor, $oil, $armDisarm) = getValues($onStr);
var_dump([$mainPower, $acc, $blender, $vehicles, $frontDoor, $oil, $armDisarm]);

list($mainPower, $acc, $blender, $vehicles, $frontDoor, $oil, $armDisarm) = getOnOffValues($offStr);
var_dump([$mainPower, $acc, $blender, $vehicles, $frontDoor, $oil, $armDisarm]);
Comments