如何在python中解压缩stxetx数据 [英] how to unpack stxetx data in python

查看:181
本文介绍了如何在python中解压缩stxetx数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接收从RS485级串行总线发送到ESeye的数据包

I'm receiving a data packet sent from a RS485 level serial bus to a ESeye

Hammerkop发送器,然后URL对数据进行编码并将其发送到我的服务器.该请求如下所示:

Hammerkop transmitter which then URL encodes the data and sends it to my server. The request looks like the following:

at=info method=GET path="/write/?type=stxetx&packet=ArcYX%01%00%00%00%00%00%00%03%00%F0%00%06%00%F0%008%0E%B0%27%00%00%E85&localpackettime=2016-12-20+16%3A00%3A27&serial=868324023356343&packettime=2016-12-20+16%3A00%3A27&receivetime=2016-12-20+16%3A00%3A28&timezone=UTC" host=dry-hollows-46605.herokuapp.com request_id=4d5bec23-0676-4e32-ae66-af26e26f0394 fwd="81.94.198.91" dyno=web.1 connect=1ms service=232ms status=500 bytes=63266

我使用Django 1.9作为框架,并获取我使用的数据包数据:

I'm using Django 1.9 as my framework and to get the packet data I use:

packet_data = request.GET.getlist('packet')   

我收到的数据如下:

packet_data = [u"A\x90cYX\x01\x00\x00\x00\x00\x00\x00\x03\x00\xf0\x00\x06\x00\xf0\x008\x0e\xb0'\x00\x00\xe85"]

然后我必须将其拆分为字节,第一个字母为ascii字母,该字母很简单,可以忽略,其余为字节.我试图将此unicode字符串拆分成我需要的值,例如'A'之后的前4个字节是字节int时间戳.

I then must split this into bytes, the first letter is an ascii letter which is straight forward and can be ignored, the rest are bytes. I'm trying to split out this unicode string into the values i need for example the first 4 bytes after the 'A' are a byte int timestamp.

我尝试对值进行编码,例如:

I've tried encoding the values, for example:

a = packet_data[1].encode('utf-8')
b = packet_data[2].encode('utf-8')
c = packet_data[3].encode('utf-8')
d = packet_data[4].encode('utf-8')

将它们转换为str而不是unicode 然后打开它们的包装:

which converts them to str rather than unicode and then I unpack them:

timestamp = unpack('i',a+b+c+d)

但是这仅在某些时间有效,有时我会收到错误消息:

however this only works some of the time and sometimes I get the error:

error: unpack requires a string argument of length 4

例如,这些时间戳值出现错误:

I got the error for these timestamp values for example:

before encoding u'\x90', u'c', u'Y', u'X'

after encoding '\xc2\x90', 'c', 'Y', 'X'

和建议会非常有帮助,我已经为此花了好几天的时间.

and suggestions would be so helpful I've been going round for days on this.

我唯一的想法是我没有正确打包/解压缩数据,也许我错过了正确的填充,但是我尝试了不同的[填充类型,它们似乎也无济于事.有人接下来会尝试什么?

My only thought is that I'm not packing/unpacking the data correctly, maybe I'm missing out the correct padding but I've tried different [padding types and they don't seem to help either. What would someone try next?

推荐答案

Django毫无帮助地将GET参数解码为文本而不是二进制数据.您需要自己解析request.META['QUERY_STRING']的内容.

Django has unhelpfully decoded the GET parameter as text instead of binary data. You will need to parse the contents of request.META['QUERY_STRING'] yourself.

>>> import urlparse
>>> urlparse.parse_qs('...&packet=ArcYX%01%00%00%00%00%00%00%03%00%F0%00%06%00%F0%008%0E%B0%27%00%00%E85&...')['packet'][0]
"ArcYX\x01\x00\x00\x00\x00\x00\x00\x03\x00\xf0\x00\x06\x00\xf0\x008\x0e\xb0'\x00\x00\xe85"

这篇关于如何在python中解压缩stxetx数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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