0

swoole tcp如何获取下位机的16进制指令数据

//指令集的结尾是0xEF
$tcp_server = $serv->addlistener('0.0.0.0', 9503, SWOOLE_SOCK_TCP);
$tcp_server->set(array(
    'open_length_check' => true,
    'package_length_type' => 'N',
    'package_length_offset' => 0,
    'package_max_length' => 8192,
    //'package_body_offset' => 4,
    'open_eof_check' => true,
    'package_eof' => 0xEF
));
//连接已经成功,打印了Client:Connect
$tcp_server->on('connect', function ($serv, $fd){
    echo "Client:Connect\r\n";
});
//这是接收代码
$tcp_server->on('receive', function ($serv, $fd, $from_id, $data){
    //$buffer = str_split(bin2hex($data), 2);
    echo "开始接收数据^^^^";
    $buffer = bin2hex($data);
    var_dump($buffer);
    echo hexdec($buffer[0]);
    echo "!!!";
    echo hexdec($buffer[1]);

    echo "...";

    //取所有数组之和
    echo (hexdec($buffer[0])+hexdec($buffer[1]));
    //将十进制数转为二进制
    echo ( decbin (hexdec($buffer[0])+hexdec($buffer[1])));
    $a=decbin (hexdec($buffer[0])+hexdec($buffer[1]));
    echo ",,,,";
    //取最后1个字节
    echo substr($a,-8);
    //二进制转十进制
    echo "......";
    echo bindec(substr($a,-8));
    print($data);
    $data = json_decode($data, true);
    //已下省略业务代码
}
  • 问题1:感觉没有触发receive回调
  • 问题2:如何打印出数据
chenjq 2
2019-08-20 提问
1 个回答
0

你这种情况只需要eof检测即可, 把包长检测去掉试试
/**
'open_length_check' => true,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_max_length' => 8192,
//'package_body_offset' => 4,
**/

撰写答案

推广链接