我有一个GPS设备通过TCP向我的服务器(带有PHP监听器的Linux)发送数据包,我有所有参数,但有一个I / O状态。
tk207协议将该参数定义为:
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定义:包含0.9的数字字符串。固定数字时,除了特殊指令外,在左边填写ASCII码0(Ox30H)缺少数字。
我正在获得像这样的价值EDITED *
01000000
01000001
11000000
11000001
但是我无法弄清楚如何从该字符串中获取7个参数。
任何帮助将不胜感激。
编辑: 在我有关于该领域的定义之前,我正在研究如何从该字段中检索开/关值的一些示例
$gpioInput = 0;
for ($i = 0; $i <= 7; $i++) {
$b = substr($gpioStr, 7 - $i, 1);
if ($b != "0") {
$gpioInput |= (1 << $i);
}
}
答案 0 :(得分:1)
我的理解是你试图访问&#34;全球&#34; value,它是所有字段的汇总$gpioInput |= (1 << $i);
如果是这样的话,那就是&#34;关闭&#34;当任何数字为零时,下一个代码应该是有帮助的。
<?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]);
答案 1 :(得分:0)
我可能会遗漏一些基本的东西,但从我从定义中可以理解的东西看起来很简单:每个IO状态值都是一个8位的字符串,以字符串值而不是普通字节的形式返回。
取您提供的示例的第一行,并将其旋转90度以便于阅读:
0 <- Fill digit.
1 <- Main power: Off.
0 <- ACC: Closed
0 <- Blender: Did not start
0 <- Empty/Heavy: Did not start.
0 <- Front door: Did not start.
0 <- Oil state
0 <- Arm/disarm
然后对其他三行做同样的事。
将其变为数组的快捷方法是使用str_split()
。这可能会使其更容易遍历,或在必要时进行操作。
答案 2 :(得分:0)
它是字符串类型,而不是服务返回的二进制类型。 所以你可以像:
那样处理它foreach(str_split($gpioStr,8) as $gpioStrLine){
// $gpioStrLine[0]<- Fill digit.
// $gpioStrLine[1]<- Main power.
// $gpioStrLine[2]<- ACC
// $gpioStrLine[3]<- Blender
// $gpioStrLine[4]<- Empty/Heavy.
// $gpioStrLine[5]<- Front door.
// $gpioStrLine[6]<- Oil state
// $gpioStrLine[7]<- Arm/disarm
}