LoPy micropython commands



  • Hi,
    below I've posted my script which I've based on examples in the documentation.

    I have some questions:

    1. What does the following command do exactly, in terms a beginner can understand?
    s.setblocking(True)
    
    1. Same for this one?
    s.setblocking(False)
    
    1. Does the following command read 64 bytes of data from the RX buffer and once read clear the buffer? If not, what does it do?
    data = s.recv(64)
    

    Full script:

    lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, sf=10, frequency=869850000, bandwidth=LoRa.BW_250KHZ, public=False, preamble=8, coding_rate=LoRa.CODING_4_5, tx_iq=False, rx_iq=False, power_mode=LoRa.ALWAYS_ON)
    
    # create a raw LoRa socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    
    while True:
        # send some data
        s.setblocking(True)             # <---- what does this command do?
        s.send('Hello from base...')    # 18 bytes of data
         blink_led(0xFFA500)            # flash LED orange on TX data
        print('TX: Hello from base...count: {}'.format(counter))
        
        # get any data received...
        s.setblocking(False)            # <---- what does this command do?
        data = s.recv(64)               # # <---- does this clear the RX buffer?
    
        if len(data) == 0:
            print('RX: nothing...\n')
            
        else:
            print('RX: data received')
            print('Data length: {}'.format(len(data)))
            print('RX: {} RX count: {}'.format(str(data), rx_counter))
            blink_led(0xff00)   # flash LED green on RX data
    
        time.sleep(2)
    

    Thanks,

    Paul



  • @pkilcoyne said in LoPy micropython commands:

    So am I right about the following command just reading 64 bytes and clearing the buffer?

    More or less ;)

    It takes up to 64 bytes from the buffer. If the buffer contains less bytes you will get less.

    I don't know the implementation details. I assume a ring buffer. So it will not clear the buffer it will only move a pointer and simply overwrite old data without clearing. Wikipedia ring buffer description.



  • @crumble Okay, I understand the logic now, thanks :)

    So am I right about the following command just reading 64 bytes and clearing the buffer?

    data = s.recv(64)
    

    Thanks,

    Paul



  • @pkilcoyne said in LoPy micropython commands:

    s.setblocking(True)
    
    1. Same for this one?
    s.setblocking(False)
    

    Blocking means that the execution of your skript is blocked until a message is sent or received. If blocking is set to False, it does not wait and executes directly the next line.

    If you want to wait for a message it is better to set blocking(True). Calling the receive method with setblocking(False) in a loop will cost you a lot of power. Using sleep to reduce it may let you miss a message.

    If you use a sent method with setblocking(False) you cannot use deepsleep directly after sent statement. You have to guess the duration and and a sleep statement to ensure that the message is sent before the power down.

    Why do you want setblocking(False) at all. You may not whish to wait for ever. A timeout parameter exist for the max wait time, but you may want to calculate something in between or react on an input pin. So wainting for some seconds may be a bad idea.

    If you left beginner state you may want to have a look on multi threading. So you can use network i/o with blocking and handle calculations and pin reaction in parallel.


Log in to reply
 

Pycom on Twitter