我使用 pyNRF 中的软件包为 NRF24 无线电模块创建了一个小脚本。
我按照 GitHub 页面上的示例进行了连接。
当我启动 simple_recever 和 simple_sender 脚本时,一切正常。我收到消息并得到正确的输出。
但是对于我的脚本来说,它只是收到的第一条消息。然后接收器卡住了。 nrf.data_ready 函数始终为 true,并且不会更改接收到的有效负载。并且收到的消息中的第一个字节将不正确。但是
我的脚本发送端。
while True:
if not self.sending_data.empty():
for _ in range(self.sending_data.qsize()-1):
send=self.sending_data.get_nowait()
if not send:
continue
if send[0][9] and send[0][10]:
pass
else:
payload = struct.pack("<B"+"?"*13+"f"*6+"h"*2,
0x01,
*send[0].values(),
*send[1].values(),
send[2][0][0],
send[2][0][1])
nrf.reset_packages_lost()
nrf.send(payload)
print("<B"+"?"*13+"f"*6+"h"*2)
print(not send)
print(len(send[0].values()))
print(len(send[1].values()))
print(payload)
# Send the payload to the address specified above.
try:
if payload:
nrf.wait_until_sent()
print("sented")
except TimeoutError:
print("Timed out")
time.sleep(0.2)
continue
if nrf.get_packages_lost() == 0:
print(f"Success: lost={nrf.get_packages_lost()}, retries={nrf.get_retries()}")
else:
print(f"Error: lost={nrf.get_packages_lost()}, retries={nrf.get_retries()}")
time.sleep(1)
我在接收端的代码:
while True:
while nrf.data_ready():
# Count message and record time of reception.
count += 1
now = datetime.now()
# Read pipe and payload for message.
pipe = nrf.data_pipe()
payload = nrf.get_payload()
print(payload)
# If the length of the message is 9 bytes and the first byte is 0x01, then we try to interpret the bytes
# sent as an example message holding a temperature and humidity sent from the "simple-sender.py" program.
protocol = payload[0] if len(payload) > 0 else -1
hex = ':'.join(f'{i:02x}' for i in payload)
# Show message received as hex.
print(f"{now:%Y-%m-%d %H:%M:%S.%f}: pipe: {pipe}, len: {len(payload)}, bytes: {hex}, count: {count}, protocol: {protocol}")
nrf.show_registers()
if payload[0] == 0x01:
for i in payload:
print(i)
print("Payload rx: " + str(struct.unpack("<B"+"?"*13+"f"*6+"h"*2, payload)))
values = struct.unpack("<B"+"?"*13+"f"*6+"h"*2, payload)
self.queue.put_nowait(values)
time.sleep(0.1)
除了数据速率为 2MB 之外,设置与示例程序中相同。
唯一改变的部分是有效负载:
payload = struct.pack("<Bff", 0x01, temperature, humidity)
到
payload = struct.pack("<B"+"?"*13+"f"*6+"h"*2,
0x01,
*send[0].values(),
*send[1].values(),
send[2][0][0],
send[2][0][1])
为什么这不起作用以及如何解决?
在阅读了一些数据表之后。我发现最大Buffer是32Bytes...
所以在这种情况下我必须编写一个协议来在多个垃圾中传输正确的数据......