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

    REPL limitations? SD mount works on board but not via REPL

    Scheduled Pinned Locked Moved Software Tools
    7 Posts 3 Posters 363 Views 4 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.
    • M Offline
      mk
      last edited by

      Are there limitations to what can be run via REPL vs actually uploaded to the device?

      I'm running Visual Studio Code with Pymakr 1.1.6 connecting to a LoPy4 (1.20.2.rc6) on a Pysense board via USB to my Win10 machine.

      I've got a fairly basic SD card script that fails on os.mount(sd, '/sd') when run through REPL, but it works when uploaded to the board.

      I normally click the "> Run" button on the VSC taskbar to "run the current file".

      Am I doing something wrong? Is there a better way? Any ideas of why this SD card isn't mounting via REPL (it's a 32GB level 4 card)?

      from machine import SD
      import os
      print('Start')
      gc.enable()
      sd = SD()
      print('Code point 1')
      os.mount(sd, '/sd')
      print('Code point 2')
      f = open('/sd/test.txt', 'a')
      f.write('Testing SD card write operations')
      f.write('\n')
      f.close()
      

      Error:

      >>>
      >>>
      Start
      Code point 1
      Traceback (most recent call last):
        File "<stdin>", line 8, in <module>
      OSError: [Errno 1] EPERM
      >
      
      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @mk
        last edited by

        @mk Is the SD card not already mounted?

        1 Reply Last reply Reply Quote 0
        • M Offline
          mk
          last edited by

          @jcaron great point! You are correct, have just wrapped the mount in a try catch and then moved on to the the file write either way and it works fine. I didn't think to do that!

          It did occur to me the other day that it might need unmounting first, but the unmount was failing too - the unmount docs are a bit vague, so I think I was doing it wrong...

          from: pycom uos docs

          uos.mount(block_device, mount_point, * , readonly=False)
          Mounts a block device (like an SD object) in the specified mount point. Example:
          
          os.mount(sd, '/sd')
          uos.unmount(path)
          
          Unmounts a previously mounted block device from the given path.
          

          I tried:

          uos.unmount(sd)
          uos.unmount('/sd')
          

          and they both failed with errors. Do you know what path should be?

          R J 2 Replies Last reply Reply Quote 0
          • R Offline
            railmonitor @mk
            last edited by

            @mk I would guess the same path as when mounting.

            uos.unmount(sd, '/sd')
            
            1 Reply Last reply Reply Quote 0
            • J Offline
              jcaron @mk
              last edited by

              @mk what error are you getting? Do you have any open files on that fs?

              1 Reply Last reply Reply Quote 0
              • M Offline
                mk
                last edited by

                @jcaron @railmonitor I've done some more research, and because I didn't find this info anywhere else, here's what I found (please correct me if you see anything wrong).

                If you run os.mount(sd, '/sd') when it is already mounted you get this error: OSError: [Errno 1] EPERM

                When there is no SD card physically in the slot, the mount doesn't throw an error, but when you try and open a file
                f = open('/sd/file.txt', 'a') you'll get error: OSError: [Errno 19] ENODEV

                Trying to unmount: using the command specified in the pycom docs:
                https://docs.pycom.io/firmwareapi/micropython/uos/#uos-mount-block-device-mount-point-readonly-false uos.unmount(sd) or uos.unmount('/sd') or even uos.unmount() the error is AttributeError: 'module' object has no attribute 'unmount'

                Obviously this is because unmount isn't right, but the micropython docs set me straight - it should be umount, then umount('/sd') works as expected. I will submit a pull request on the pycom docs now to fix them.

                If you try and umount when there's nothing mounted you'll get this error: OSError: [Errno 22] EINVAL

                So, a try/catch around the mount to see if it's already mounted, a try/catch around the file open to see if there's an SD card, and a try/catch around any unmounts just in case there's nothing mounted there will catch all of these scenarios.

                Heaps of extra info on this reply I know, hopefully it helps with anyone in the future searching.

                1 Reply Last reply Reply Quote 2
                • R Offline
                  railmonitor
                  last edited by railmonitor

                  Great that it worked out. For anyone seeing this thread, but not finding what they need in the pycom documentation, there are also the possibility to delete items on the sd card using the command:

                  uos.remove(path)
                  

                  path could be something like:

                  '/sd/log.txt'
                  
                  1 Reply Last reply Reply Quote 0

                  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