Unpack and Pack



  • Hi there, i want to pack a float in the node and send it over to the gateway. May i know is the format valid?

    _LORA_PKG_FORMAT = "BBf" #1byte for device_id, 1 byte for package size, 1 for float

    Here is the information that i want it to be pack as a float at Node

    D = 10**((Measured_power-rssi)/(10*n)
    pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), D) lora_sock.send(pkg)
    Here is the information that unpack at Gateway

    device_id, pkg_len, D = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
    I tried this format but i have no received anythings from the node and i tried the example of the Lora Nano-gateway in documentation and it work so i guess is not the firmware problem since i have all lopy updated to the same version.
    Any help would be appreciate. Thank you.



  • @mj SInce the for string for pack contains only two fields, it must be written as:

    _LORA_PKG_FORMAT = "Bf"
    pkg = struct.pack(_LORA_PKG_FORMAT, DEVICE_ID,  D) 
    

    But besides that, the receiver must contains a lora_sock.recv(64) call, in order to get something. Please have a look at the simple examples in the documentation on how the code structure has to be set up.



  • @robert-hh
    I have tried but still showing no response once i uploaded the code.

    On my Node :

    _LORA_PKG_FORMAT = "Bf"
    DEVICE_ID = 0x01
    lora = LoRa(mode=LoRa.LORA, tx_iq=True, frequency = 915000000)
    lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    lora_sock.setblocking(False)
    Measured_power = -37
    n = 3 
    d = (10**((Measured_power-rssi)/(10*n)))/1000
    
    pkg = struct.pack(_LORA_PKG_FORMAT, DEVICE_ID, 6, D)
    lora_sock.send(pkg)
    

    For Gateway :

    _LORA_PKG_FORMAT = "Bf"
    lora = LoRa(mode=LoRa.LORA, rx_iq=True, frequency = 915000000)
    lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    
    device_id, D = struct.unpack(_LORA_PKG_FORMAT, recv_pkg)
    

    Is there any mistake? Thank you.



  • @mj said in Unpack and Pack:

    pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), D) lora_sock.send(pkg)

    Since you have a fixed message length, you can simply use:
    struct.pack(FORMAT, DEVICE_ID, 6, D)
    and unpack with:
    device_id, pkg_len, D = struct.unpack(_LORA_PKG_FORMAT, recv_pkg)
    You can even omit sending the pkg_len and pack with:

    _LORA_PKG_FORMAT = "Bf" #1byte for device_id, 4 for float
    D = 10**((Measured_power-rssi)/(10*n)
    pkg = struct.pack(_LORA_PKG_FORMAT, DEVICE_ID,  D) lora_sock.send(pkg)
    

    and unpack with:

    device_id, D = struct.unpack(_LORA_PKG_FORMAT, recv_pkg)
    

Log in to reply
 

Pycom on Twitter