public static function packetData($cmd='',$subcmd='',$content=''){ 
        $byte=array();
        $byte[]=self::$symbol.self::$s1_1;//帧头2位
        $byte[]=self::$symbol.self::$s1_2;
        $byte[]=self::$symbol.self::$s1_3;
        $byte[]=self::$symbol.self::$s1_4;
        $byte[]=self::$symbol.$cmd;//命令1位
        if($cmd==62){
            $len=19;
            if ($content!=='') {
                $content.='   ';
                $gbkstr = mb_convert_encoding($content,'GBK','UTF-8');//中文编码GBK
                $len+=strlen($gbkstr);
                $len_txt=strlen($gbkstr);
            }

            $byte[]=dechex($len);
            $byte[]=$subcmd;//行号
            $byte[]='15'; //(($len_txt>2)?'15':'00');//显示模式
            $byte[]='01';//显示速度
            $byte[]='00';//停留模式
            $byte[]='00';//停留时间
            $byte[]='01';//退出模式
            $byte[]='01';//退出速度
            $byte[]='03';//字体类型
            $byte[]='00';//显示次数
            if($subcmd=='00' || $subcmd=='02'){
                $byte[]='FF';//红色分量
                $byte[]='00';
                $byte[]='00';
            }else{
                $byte[]='FF';//绿色分量
                $byte[]='FF';
                $byte[]='FF';
            }
            $byte[]='00';
            $byte[]='00';
            $byte[]='00';
            $byte[]='00';
            $byte[]='00';
            $byte[]=dechex($len_txt);
            $byte[]='00';
        }elseif($cmd==30){
            $len=1;
            if ($content!=='') {
                $gbkstr = mb_convert_encoding($content,'GBK','UTF-8');//中文编码GBK
                $len+=strlen($gbkstr);
            }
            $byte[]=dechex($len);
            $byte[]='02';
        }elseif($cmd==31){
            $byte[]='00';
        }elseif($cmd=='05'){
            $byte[]='08';
            $year=str_pad(dechex(date('Y')),4,'0',STR_PAD_LEFT);
            $byte[]=substr($year,-2);
            $byte[]=substr($year,0,2);
            $byte[]=dechex(date('m'));
            $byte[]=dechex(date('d'));
            $byte[]=dechex(date('w')+1);
            $byte[]=dechex(date('H'));
            $byte[]=dechex(date('i'));
            $byte[]=dechex(date('s'));
        }elseif($cmd=='0D'){
            $byte[]='01';
            $byte[]=dechex(intval($subcmd));
        }elseif($cmd=='0F') {
            $byte[] = '06';
            $byte[] = '00';
            $byte[] = '01';
            $byte[] = '00';
            $byte[] = '00';
            $byte[] = '00';
            $byte[] = '05';
        }

        if ($content!=='') {
            $hex = self::str2hex($gbkstr);
            $content_arr=str_split($hex,2);

            foreach ($content_arr as $k=>$v){
                $byte[]=self::$symbol.$v;
            }
        }
        $test='';
        foreach($byte as $k => $v){
            $byte[$k]=strtoupper($v);
            $test.=chr(hexdec(strtoupper($v)));
        }
        //CRC16校验
        $res= self::genCRC( $test );
        $byte[]=self::$symbol.strtoupper($res['lo']);
        $byte[]=self::$symbol.strtoupper($res['hi']);
   
        //base64
        $result=array();
        //$result['test']=$test;
        $result['byte']=$byte;
        $result['dataLen']=count($byte);
        $str='';
        for ($i = 0; $i < count($byte); $i++){
            $str.=chr(hexdec($byte[$i]));
        }
        $result['data']=base64_encode($str);     dump($result);
        return $result;
    }

This function utilizes a variety of techniques, including:

  • Data Packing: The function assembles data into a byte array, incorporating different elements like commands, subcommands, and content.
  • Encoding: Chinese characters are encoded in GBK format for compatibility.
  • CRC16 Checksum: A CRC16 checksum is calculated to ensure data integrity during transmission.
  • Base64 Encoding: The final packet is encoded in base64, making it suitable for transmission across different mediums.

The packetData function is designed to be flexible and handle different data formats. You can modify the content, commands, and subcommands to suit your specific communication needs.

Example Usage:

$packet = packetData('62', '00', 'Hello World!');

// $packet will contain the data packet in base64 format

Code Explanation:

  1. Data Structure: The function takes cmd, subcmd, and content as input to generate a byte array representing the packet.
  2. Pre-processing: The content is encoded into GBK if necessary, and the total length of the packet is calculated.
  3. Adding Elements: Various data elements, such as commands, subcommands, and content are appended to the byte array.
  4. CRC16 Checksum Calculation: A CRC16 checksum is calculated for error detection.
  5. Base64 Encoding: The byte array is converted to a string and encoded in base64 for transmission.

Note: The symbol, s1_1, s1_2, s1_3, s1_4, str2hex, and genCRC functions are assumed to be defined elsewhere in your code base. These functions likely handle specific protocol requirements or data transformations.

By understanding the code and its functionalities, you can effectively use this packetData function to build reliable communication systems in your PHP applications.


原文地址: https://www.cveoy.top/t/topic/nmp3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录