rfm9x.receive()- Python LoRa接收方法删除前4个字符

flmtquvp  于 2023-10-16  发布在  Python
关注(0)|答案(1)|浏览(67)

我计划建立一个本地LoRa网络,它有一个接收器(在Raspberry Pi Zero上运行Python)和几个发射器(在Arduino上运行C代码)。
为了开始这一点,我测试了Adafruit和其他几个在线教程的一些草图,并可以使用RFM 95模块获得一个发射器和接收器进行通信。
但问题是,当Python代码读取LoRa数据包时,它会从源发送的数据包中删除前4个字符。
我们能做些什么来解决这个问题?我怀疑这可能是一个字节/字符串转换问题,但我还不能让它工作。
发送器代码(Arduino上的C):

....
void loop() {
  Serial.print("Sending - Packet:");
  Serial.println(msgCount);
  LoRa.beginPacket();
  LoRa.print("Packet:");
  LoRa.print(msgCount);
  LoRa.endPacket();
  msgCount++;
  delay(5000);
}
.......

接收器代码(PiZero上的Python):

.......
while True:
    packet = None
    packet = rfm9x.receive()
    if packet is None:
        print('Waiting for PKT')
    else:
        prev_packet = packet
        packet_text = str(prev_packet, "utf-8")
        print(packet_text)
        time.sleep(1)
    time.sleep(0.1)
........

接收器的预期输出:

Packet:1
Packet:2
Packet:3
.......
Packet:10
Packet:11
........

接收器的实际输出:

et:1
et:2
et:3
.......
et:10
et:11
.......

我尝试了以下方法,但没有成功:

packet_text = prev_packet.decode("ascii", 'ignore')
packet_text = prev_packet.decode("utf-8", 'ignore')

感谢您的帮助!谢谢你,谢谢

flvlnr44

flvlnr441#

您似乎错过了RFM9x.receive()的文档: `
receive(*, keep_listening: bool = True, with_header: bool = False, with_ack: bool = False, timeout: float | None = None) → bytearray | None

Wait to receive a packet from the receiver. If a packet is found the payload bytes are returned, otherwise None is returned (which indicates the timeout elapsed with no reception). If keep_listening is True (the default) the chip will immediately enter listening mode after reception of a packet, otherwise it will fall back to idle mode and ignore any future reception. All packets must have a 4-byte header for compatibility with the RadioHead library. The header consists of 4 bytes (To,From,ID,Flags). The default setting will strip the header before returning the packet to the caller. If with_header is True then the 4 byte header will be returned with the packet. The payload then begins at packet[4]. If with_ack is True, send an ACK after receipt (Reliable Datagram mode)
我们能做些什么来解决这个问题? 可以发送一个header或使用with_header = True`。

相关问题