public function test()
{
//bin2hex
//ASCII 字符的字符串转换为十六进制值
$data = strtolower('04129da167763b');
$command = substr($data, 0, 2);
//异或检验和
//去掉后两位,校验
$wb = substr($data, 0, -2);
//获取后两位
$last_two = mb_substr(strval($data), -2, 2);
$check = $this->sumCheck($wb);
if($check == $last_two){
//通过
//04 命令字段 转2进制
//12 9d a1 设备唯一ID
$imei = substr($data, 2, 6);
//67 状态字段 转2进制
$state = str_pad(base_convert(substr($data, 8, 2), 16, 2), 8, "0", STR_PAD_LEFT);
//电量
$dl = substr($state, 0, 7);
//使用状态
$use = substr($state, 7, 1);
//74 信号强度
$signal = substr($data, 10, 2);
//回复
$ack = '05'.$imei.'01';
$ackData = $ack.$this->sumCheck($ack);
print_r($command);exit;
} else {
//未通过
}
}
function BlueCode_BCCXOR($str){
$split_length = 2;
$len = strlen($str);
$End = 0;
for($i = 0; $i < $len; $i+=$split_length){
$s = substr($str, $i, $split_length);
$End = $End ^ hexdec($s);
}
$End = strtoupper(str_pad(dechex($End), '2', '0', STR_PAD_LEFT));
return $End;
}
调用:
echo BlueCode_BCCXOR("5504A15F00");
输出:AF
在线异或校验/BCC校验工具:https://www.metools.info/code/c48.html
引用 https://www.bluecode.cn/show-621.html
function DataIn($fd, $data = '')
{
$config = new \EasySwoole\Mysqli\Config([
'host' => '',
'port' => '',
'user' => '',
'password' => '',
'database' => '',
'timeout' => 5,
'charset' => 'utf8mb4',
]);
//echo "fd:{$fd} send:{$data}\n";
$command = substr($data, 0, 2);
if($command == '04'){
//异或检验和
//去掉后两位,校验
$wb = substr($data, 0, -2);
//获取后两位
$last_two = mb_substr(strval($data), -2, 2);
$check = $this->sumCheck($wb);
//echo "check:{$check} last_two:{$last_two}\n";
if($check == $last_two){
//通过
//04 命令字段 转2进制
//12 9d a1 设备唯一ID
$imei = substr($data, 2, 6);
//echo "imei:{$imei}\n";
//67 状态字段 转2进制
$state = str_pad(base_convert(substr($data, 8, 2), 16, 2), 8, "0", STR_PAD_LEFT);
//电量
$dl = base_convert(substr($state, 0, 7), 2, 10);
//echo "dl:{$dl}\n";
//使用状态
$use = substr($state, 7, 1);
//echo "use:{$use}\n";
//74 信号强度
$signal = base_convert(substr($data, 10, 2), 16, 10);
//echo "signal:{$signal}\n";
$client = new \EasySwoole\Mysqli\Client($config);
$client->queryBuilder()->where('imei', $imei)->getOne('jq_device');
$result = $client->execBuilder();
if($result){
$client->queryBuilder()->where('imei', $imei)->update('jq_device', ['fd' => $fd,'createtime' => date('Y-m-d H:i:s', time())]);
} else {
$client->queryBuilder()->insert('jq_device', ['fd' => $fd, 'imei' => $imei, 'dl' => $dl, 'use' => $use, 'signal' => $signal, 'content' => $data, 'createtime' => date('Y-m-d H:i:s', time())]);
}
$client->execBuilder();
//file_put_contents('./log.txt', date("Y-m-d H:i:s") . " 设备:" . $imei . "\r\n电量:".$dl."\r\n使用状态:".$use."\r\n信号强度:".$signal."\r\n", FILE_APPEND);
//回复
$ack = '05'.$imei.'01';
$ackData = $ack.$this->sumCheck($ack);
$resString = "";
$dataArr = str_split($ackData, 2);
foreach ($dataArr as $k => $v) {
$resString .= chr(hexdec('0x' . $v));
}
$info = ServerManager::getInstance()->getSwooleServer()->connection_info($fd);
if (is_array($info)) {
ServerManager::getInstance()->getSwooleServer()->send($fd, $resString);
} else {
$this->response()->write("fd {$fd} not exist");
}
} else {
//未通过
}
}
}
I always look forward to your posts. Keep it coming!
This content is gold. Thank you so much!
Thank you for being so generous with your knowledge.
This is now one of my favorite blog posts on this subject.
This post cleared up so many questions for me.