Jump to content

Binary data handling


pdoria

Recommended Posts

Hi All,

 

Coming from the C# world I'm in a bit of pickle here trying to handle binary data and sending it to a socket... :-[

 

Here's what I'm trying to do:

 

    $header	="\x01\x00\x00\x00";		// header
    $datalength	="\x08";			// data length
    $codecid	="\x0A";			// codec ID
    $nrecs	="\x01";			// # of recs
    // garmin avl data
    $DLE		="\x10";		// DLE
    $packetid		="\xA1";		// packet id (161)
    $size_app_payload	="\x02";		// size_app_payload
    $fmi_packet_id	= reverse_endianess_short (0x0001);		// fmi packet id
    $checksum= $packetid+$size_app_payload+$fmi_packet_id;
    $checksum = -$checksum;
    $checksum &= 0xff;

    $ETX="\x03";

    $buf="";
    $buf=$header.$datalength.$codecid.$nrecs.$DLE.$packetid.$size_app_payload.$fmi_packet_id.$checksum.$DLE.$ETX."\x01";

 

First question:

Is this the right way to construct the buffer?

 

First difficulty: print the $buf contents as to check it out...  ;)

This

    for ($n=0; $n < strlen($buf); $n++) {
      printf("%02X\r\n", $buf[$n]);
    }

 

Only outputs zeroes...

 

Any pointers highly appreciated...  ;)

BR,

Pedro Doria Meunier

Link to comment
https://forums.phpfreaks.com/topic/180088-binary-data-handling/
Share on other sites

Okay not been awake for long but shouldn't

$checksum &= 0xff;

be

$checksum .= 0xff;

 

and

    for ($n=0; $n < strlen($buf); $n++) {
      printf("%02X\r\n", $buf[$n]);
    }

be

for ($n=0; $n < strlen($buf); $n++) {
  printf("%02b\r\n", ord($buf[$n]));
}

Link to comment
https://forums.phpfreaks.com/topic/180088-binary-data-handling/#findComment-950041
Share on other sites

Hi MadTechie

 

Thanks for replying!  :)

 

Actually I've solved my problem of outputting the data with:

    for ($n=0; $n < strlen($buf); $n++) {
      printf("%02X\r\n", bin2hex($buf[$n]));
    }

 

Now I'm having what seems to be an alignment problem... :-\

 

This code:

    $header		="\x01\x00\x00\x00";	// header
    $datalength		="\x00\x00\x00\x08";	// data length
    $codecid		="\x0A";		// codec ID
    $nrecs		="\x01";		// # of recs
    // garmin avl data
    $DLE		="\x10";		// DLE
    $packetid		="\xA1";		// packet id (161)
    $size_app_payload	="\x02";		// size_app_payload
    $fmi_packet_id   = reverse_endianess_short (0x0001);      // fmi packet id
    $checksum= $packetid+$size_app_payload+$fmi_packet_id;
    $checksum = -$checksum;
    $checksum &= 0xff;

    $ETX="\x03";




    $buf="";
    $buf=$header.$datalength.$codecid.$nrecs.$DLE.$packetid.$size_app_payload.$fmi_packet_id.$checksum.$DLE.$ETX.$nrecs;

    // crc16
    $crc16=0;
    $garminAVLData=array();
    $garminAVLData[]=$DLE;
    $garminAVLData[]=$packetid;
    $garminAVLData[]=$size_app_payload;
    $garminAVLData[]=$fmi_packet_id;
    $garminAVLData[]=$checksum;
    $garminAVLData[]=$DLE;
    $garminAVLData[]=$ETX;

    foreach ($garminAVLData as $val) {
      $crc=crc16($val, $crc16);
    }

    //$crc16 = pack("N", bin2hex($crc16));

    $buf.=$crc16;


    printf("Len: %d - IP: %s\r\n", strlen($buf), $ip);

    for ($n=0; $n < strlen($buf); $n++) {
      printf("%02X\r\n", bin2hex($buf[$n]));
    }

 

Outputs this:

01

00

00

00

00

00

00

08

00

01

0A

00

02

01

00

1E

0A

03

01

1E

 

One thing that pops to view is the wrong order for $codecid and $nrecs ...  :wtf:

The other is what I think are alignment issues...

What can I do to have a byte be a byte in $buf (IOW serialize the data)?

 

Already thankful for any help you can spare,

Best regards,

Pedro Doria Meunier

Link to comment
https://forums.phpfreaks.com/topic/180088-binary-data-handling/#findComment-950098
Share on other sites

Oh i thought you wanted binary output,

 

Here's two options, both should give the same results (untested)

 

for ($n=0; $n < strlen($buf); $n++) {
    	printf("%02s\r\n", dechex(ord($buf[$n])));
      	//printf("%02X\r\n", ord($buf[$n]));
    }

Link to comment
https://forums.phpfreaks.com/topic/180088-binary-data-handling/#findComment-950125
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.