将 HEX 转换为 ASCII,来自 GPS 跟踪器的数据

发布于 2024-10-31 21:23:42 字数 2758 浏览 1 评论 0原文

我刚买了一个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á

不幸的是,我不知道如何复制粘贴十六进制部分

现在我如何获取 ID 部分?我已经尝试过 echo hexdec(mb_substr($data, 4, 7));

数据如下 此协议

来自文档:

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)

更新

有了 Anomie 的答案,我能够拼凑这在一起

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

var_dump($arr);

这会输出类似的东西

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

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"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-11-07 21:23:42

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

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

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

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

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

'v' 代码将用于长度和校验和;如果您有一个存储为 4 个字节的数字,请使用 'V',对于 ID,您可以使用 'H*' 将其获取为十六进制数字字符串。其他代码在文档中列出。


一种不太直接的方法是在使用 C* 解包后手动执行位操作以获取所有字节值的数组。例如,

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

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];
贱贱哒 2024-11-07 21:23:42

您需要知道将从设备接收的消息的格式。您可以从制造商处获取此信息。然后,根据情况,您必须在服务器端创建适当的侦听器。

我一直在使用类似的设备,通常您必须在服务器中创建一个进程,以使用套接字(或类似的)侦听端口。您可能还需要一个身份验证过程来区分设备(您可以拥有多个设备)。之后,您只需从设备获取数据,解析并存储它。根据设备,您还可以发送请求或配置。

希望这有帮助

You need to know the format of the messages you are going to receive from the device. You can get this info from the manufacturer. Then, depending on that, you have to create a proper listener in the server side.

I've been working with several devices like that and normally you have to create a process in the server listening to the port with a Socket (or similar). You may have an authentication process also to differentiate between devices (you can have more than one). After that, you simply get the data from the device, you parse it and you store it. Depending on the device you can also send requests or configurations.

Hope this helps

一桥轻雨一伞开 2024-11-07 21:23:42

*4 月 26 日编辑:* 我稍微改变了问题,因此这似乎不合适。最初的问题更多是关于如何从 TCP 读取数据。

我发现了一些关于用 PHP 编写 TCP/套接字服务器的精彩文章(/me 用一条大鳟鱼来攻击 PHP)

http://devzone.zend.com/article/1086
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

可以吗等不及要开始了:)

*Edit 26 April:* I have changed the question a bit, thus this seems out of place. Initial question was more on how to read the data from TCP.

I found some great articles on writing a TCP/socket server in PHP (/me slaps PHP around a bit with a large trout)

http://devzone.zend.com/article/1086
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

Can't wait to get this going :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文