如何找到消息中的大端密钥?
我正在尝试使用代理从 ESP32 读取二进制消息;我写了一个 phyton 脚本,在其中订阅了该主题。我实际收到的消息是:
b'\x00\x00\x00?'
这是一个浮点二进制小端消息,但我不知道解码它的密钥。有没有办法根据这些数据找到解码密钥? 这是我的 python 代码:
import paho.mqtt.client as mqtt
def on_connect1(client1, userdata1, flags1, rc1):
client1.subscribe("ESP32DevKit123/mytopic")
def on_message1(client1, userdata1, msg1):
print(msg1.topic+" "+ "TESTENZA: "+str(msg1.payload))
client1 = mqtt.Client()
client1.username_pw_set(username="myuser",password="mypassword")
client1.on_connect = on_connect1
client1.on_message = on_message1
client1.connect("linkclient", portnumber, 60)
def twosComplement_hex(hexval):
bits = 16 # Number of bits in a hexadecimal number format
on_message1 = int(hexval, bits)
if on_message1 & (1 << (bits-1)):
on_message1 -= 1 << bits
return on_message1
client1.loop_forever()
它还在 on_message1 -= 1 << 行中给了我一个错误位;错误显示:预期的预期块 pylance。有什么解决办法吗?
I am trying to read a binary message from an ESP32 using a broker; i wrote a phyton script where I subscribe the topic. the message that i actually receive is:
b'\x00\x00\x00?'
this is a float binary little endian message but I don't the key to decode it. Is there a way to find the decode key based on this data?
This is my python code:
import paho.mqtt.client as mqtt
def on_connect1(client1, userdata1, flags1, rc1):
client1.subscribe("ESP32DevKit123/mytopic")
def on_message1(client1, userdata1, msg1):
print(msg1.topic+" "+ "TESTENZA: "+str(msg1.payload))
client1 = mqtt.Client()
client1.username_pw_set(username="myuser",password="mypassword")
client1.on_connect = on_connect1
client1.on_message = on_message1
client1.connect("linkclient", portnumber, 60)
def twosComplement_hex(hexval):
bits = 16 # Number of bits in a hexadecimal number format
on_message1 = int(hexval, bits)
if on_message1 & (1 << (bits-1)):
on_message1 -= 1 << bits
return on_message1
client1.loop_forever()
It also gives me an error in the line on_message1 -= 1 << bits; the error says: Expected intended block pylance. Any solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的数据是
b'\x00\x00\x00?'
- 我假设这是0000003f
(请使用msg1.txt 输出十六进制) Payload.hex()
)。我还假设“浮动二进制小端”指的是大端浮点(IEE754) - 请注意,这与您正在使用的算法不匹配(两人恭维)。将此输入插入在线工具表明预期结果(“Float - Big Endian (ABCD)”)是
8.82818e-44
(值得用这个工具检查;有时编码可能不是你想象的那样 是!)。让我们使用 python 解压它(有关更多信息,请参阅 struct 文档)
: :
[0]
之所以存在,是因为unpack
返回一个数组(您可以从输入中解压多个项目)>f
->
表示big-endian 和f
float(标准大小 = 4 字节)您的原始代码给出错误“预期预期块”的原因是由于
on_message1 -= 行中缺少缩进1<<;位
(因为它位于if
后面,因此需要缩进)。该算法似乎与您的任务无关(但我可能遗漏了一些细节)。The data you provided is
b'\x00\x00\x00?'
- I'm going to assume that this is0000003f
(please output hex withmsg1.payload.hex()
).I'll also assume that by "float binary little endian" you mean a big endian floating point (IEE754) - note that this does not match up with the algorithm you are using (twos compliment). Plugging this input into an online tool indicates that the expected result ("Float - Big Endian (ABCD)") is
8.82818e-44
(it's worth checking with this tool; sometimes the encoding may not be what you think it is!).Lets unpack this using python (see the struct docs for more information):
Notes:
[0]
is there becauseunpack
returns an array (you can unpack more than one item from the input)>f
- the>
means big-endian and thef
float (standard size = 4 bytes)The reason your original code gives the error "Expected intended block" is due to the lack of indentation in the line
on_message1 -= 1 << bits
(as it follows anif
it needs to be indented). The algorithm does not appear relevant to your task (but there may be details I'm missing).