Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy micropython commands

    Scheduled Pinned Locked Moved LoPy
    4 Posts 2 Posters 300 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      pkilcoyne
      last edited by

      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

      C 1 Reply Last reply Reply Quote 0
      • C Offline
        crumble @pkilcoyne
        last edited by

        @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.

        P 1 Reply Last reply Reply Quote 1
        • P Offline
          pkilcoyne @crumble
          last edited by

          @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

          C 1 Reply Last reply Reply Quote 0
          • C Offline
            crumble @pkilcoyne
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom