将HEX转换为ASCII码,来自GPS追踪器的数据 [英] Convert HEX to ASCII, data from GPS tracker

查看:180
本文介绍了将HEX转换为ASCII码,来自GPS追踪器的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚买了一台GPS追踪器,它可以发短信给手机就好了。它还支持通过GPRS向服务器报告。



我已经将设备设置为在端口8123上联系我自己的服务器,它是一台FreeBSD服务器,我已检查过我是否收到数据包在该端口上。

我成功安装了一个用PHP编写的侦听器服务器,并且我可以从设备接收数据。但是,如何将部分十六进制数据转换为有用的(ASCII)?



示例数据字符串:

 $$ ^ T ^ @ E Y'^WÿU210104.000,A,5534.4079,N,01146.2510,E,0.00,,170411 ,, * 10 | 1.0 | 72 |0000á

不幸的是,我不知道我可以如何复制粘贴HEX零件



现在怎么做我把ID部分拿出来了?我试过 echo hexdec(mb_substr($ data,4,7));

<数据如下


$

 
GPRS数据包的命令格式如下:$ b
$ b从服务器到追踪器:
@@ \r\

从追踪器到服务器:
$$ \r\\\


注意:
写入命令时不要输入''。
所有多字节数据符合以下顺序:低字节之前的高字节。
GPRS数据包的大小(包括数据)大约为100字节

项目规格
@@ 2个字节。它表示从服务器到跟踪器的数据包的标题。
它是ASCII码(十六进制代码:0x40)

$$ 2个字节。它是从跟踪器到服务器的数据包的标题。
它是ASCII码(十六进制代码:0x24)

L 2个字节。这意味着整个数据包的长度,包括
的标题和结尾字符,它是十六进制代码

ID 7字节,ID必须是数字,不能超过14位,未使用的字节
将被填入'f'或'0xff'。它采用十六进制代码格式。
例如,如果ID是13612345678,那么它将显示为
:0x13,0x61,0x23,0x45,0x67,0x8f,0xff。
如果所有7个字节都是0xff,则它是一个广播命令。 ID是十六进制代码

命令2个字节。命令代码是十六进制代码。请参阅下面的
命令列表。

数据最小0字节和最大100字节。关于'数据'的描述见附件1。

校验和2个字节。它表示CRC-CCITT(默认为0xffff)
所有数据的校验和(不包括CRC本身和结尾字符)。
它是十六进制代码。
例如:24 24 00 11 13 61 23 45 67 8f ff 50 00 05 d8 0d 0a
0x05d8 = CRC-CCITT(24 24 00 11 13 61 23 45 67 8f ff 50 00)

\r\\\
2个字节。它是结尾字符,并以十六进制代码
(0x0d,0x0a,十六进制代码)

更新



有了Anomie的回答,我可以将它们拼凑在一起

  $ arr = unpack('H4length / H14id / H4cmd / H4crc / H4end',mb_substr($ data,2,11)。mb_substr($ data,-4)); 

var_dump($ arr);

这样会得到类似于



<$ p
$ {
[length] =>
string(4)0054
[id] =>
string (14)004512345678ff
[cmd] =>
字符串(4)9955
[crc] =>
字符串(4)c97e
[end] =>
string(4)0d0a
}


解决方案

听起来你需要将二进制数据转换为整数或字符串。最直接的方法是使用 unpack



例如,要提取已知的长度,您可以使用

  $ length_bin = substr($ string,2,2); 

要将其转换为整数,您可以使用类似于

  $ length = unpack('v',$ length_bin); $ length = $ length [1]; 

'v'代码适用于长度和校验和;如果你有一个以4字节存储的数字,可以使用'V',并且你可以使用'H *' >将其作为一串十六进制数字来获取。其他代码列在文档中。






稍微不简单的方法是在使用 C * 解包来手动完成位操作,以获得所有字节值。例如,

  $ bytes = unpack('C *',$ length_bin); 
$ length =($ bytes [0]<< 8)| $字节[1];


I have just bought a GPS Tracker, it can send SMS to cellphone just fine. It also supports reporting to a server via GPRS.

I have setup the device to contact my own server on port 8123, it's a FreeBSD server and i have checked that i recieve packets on that port.

I successfully have setup a listener server written in PHP, and i can receive data from the device. But how do i convert the partial HEX data to something usefull (ASCII)?

Example data string:

$$^@T^@E Y'^WÿU210104.000,A,5534.4079,N,01146.2510,E,0.00,,170411,,*10|1.0|72|0000á

Unfortunately i don't know how i can copy-paste the HEX parts

Now how do i get the ID part out? I have tried echo hexdec(mb_substr($data, 4, 7));

The data is following this protocol

From the document:

Command format of GPRS packets are as follows:

From server to tracker:
@@\r\n 

From tracker to server: 
$$\r\n

Note: 
Do NOT input ‘’ when writing a command. 
All multi-byte data complies with the following sequence: High byte prior to low byte. 
The size of a GPRS packet (including data) is about 100 bytes

Item        Specification
@@          2 bytes. It means the header of packet from server to tracker. 
            It is in ASCII code (Hex code: 0x40)

$$          2 bytes. It is the header of packet from tracker to server.
            It is in ASCII code (Hex code: 0x24)

L           2 bytes. It means the length of the whole packet including 
            the header and ending character and it is in hex code

ID          7 bytes, ID must be digit and not over 14 digits, the unused byte 
            will be stuffed by ‘f’ or ‘0xff’. It is in the format of hex code.
            For example, if ID is 13612345678, then it will be shown as 
            follows: 0x13, 0x61, 0x23, 0x45, 0x67, 0x8f, 0xff.
            If all 7 bytes are 0xff, it is a broadcasting command. ID is in hex code

command     2 bytes. The command code is in hex code. Please refer to the 
            command list below.

data        Min 0 byte and max 100 bytes. See Annex 1 for description of ‘data’.

checksum    2 bytes. It indicates CRC-CCITT (default is 0xffff) checksum of
            all data (not including CRC itself and the ending character).
            It is in hex code. 
            For example: 24 24 00 11 13 61 23 45 67 8f ff 50 00 05 d8 0d 0a
            0x05d8 = CRC-CCITT (24 24 00 11 13 61 23 45 67 8f ff 50 00)

\r\n        2 bytes. It is the ending character and in hex code 
            (0x0d,0x0a in hex code)

Update

With the answer from Anomie, i was able to piece this together

$arr = unpack('H4length/H14id/H4cmd/H4crc/H4end', mb_substr($data, 2, 11) . mb_substr($data, -4));

var_dump($arr);

This will out put something like

array(5) {
  ["length"]=>
  string(4) "0054"
  ["id"]=>
  string(14) "004512345678ff"
  ["cmd"]=>
  string(4) "9955"
  ["crc"]=>
  string(4) "c97e"
  ["end"]=>
  string(4) "0d0a"
}

解决方案

It sounds like you are needing to convert binary data to integers or strings. The most straightforward way is to use unpack.

For example, to extract the length you already know you can use

$length_bin = substr($string, 2, 2);

To convert it to an integer, you can use something like

$length = unpack('v', $length_bin); $length = $length[1];

The 'v' code will work for the length and the checksum; if you have a number stored as 4 bytes use 'V', and for the ID you can use 'H*' to get it as a string of hex digits. Other codes are listed in the documentation.


A somewhat less straightforward way is to do the bit manipulation manually, after using unpack with C* to get an array of all the byte values. For example,

$bytes = unpack('C*', $length_bin);
$length = ($bytes[0] << 8) | $bytes[1];

这篇关于将HEX转换为ASCII码,来自GPS追踪器的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆