Testing if ubinascii.unhexlify exists ...

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Testing if ubinascii.unhexlify exists ...

Post by davef » Thu Aug 27, 2020 7:34 pm

Following another thread I tried:

Code: Select all

rshell -d -p /dev/ttyUSB0 --editor nano --buffer-size=30 -a
and got:

Code: Select all

Trying to connect to REPL .
Stalled, so I then tried:

Code: Select all

sudo screen /dev/ttyUSB0 115200
and got:

Code: Select all

FAT filesystem appears to be corrupted. If you had important data there, you
may want to make a flash snapshot to try to recover it. Otherwise, perform
factory reprogramming of MicroPython firmware (completely erase flash, followed
by firmware programming).
The program I was running has a while loop, is there a problem at which point you break out of a while loop when re-booting? It could have been writing to a file at that point in time.

Appreciate any suggestions.
Dave

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Testing if ubinascii.unhexlify exists ...

Post by dhylands » Fri Aug 28, 2020 1:46 am

rshell sends 20 Control-C's (one every 100 msec) and looks for the >>> prompt. If it doesn't find it, then it gives up.

You need to fix the:

Code: Select all

FAT filesystem appears to be corrupted. If you had important data there, you
may want to make a flash snapshot to try to recover it. Otherwise, perform
factory reprogramming of MicroPython firmware (completely erase flash, followed
by firmware programming).
before rshell will connect properly.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Testing if ubinascii.unhexlify exists ...

Post by davef » Fri Aug 28, 2020 4:51 am

I have fixed that.

Before the ESP32 ends up with a corrupted file system again when I do a re-boot when my while loop is running, I wondered if there was a proper method of closing the files before shutting it down.

I guess something like <shutdown -h> on a RaspberryPi.

Thanks,
Dave

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Testing if ubinascii.unhexlify exists ...

Post by pythoncoder » Fri Aug 28, 2020 7:25 am

If you have a loop that writes to a file you need to use exception trapping to close the file. Alternatively use a context manager.

Code: Select all

with open(myfile, 'w') as f:
   while True:
       f.write(something)  # write to file
Then if a KeyboardInterrupt occurs the context manager will close the file.

The same thing can be done with try...finally.
Peter Hinch
Index to my micropython libraries.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Testing if ubinascii.unhexlify exists ...

Post by davef » Sat Aug 29, 2020 4:35 am

Hi Peter,

I always use:

Code: Select all

 try:
      with open ... 
for reading and writing files. I haven't had any issues using Crtl-C to stop execution.

Perhaps the smart thing to do is to do a Ctrl-C before pushing the reset button!

Thanks,
Dave

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Testing if ubinascii.unhexlify exists ...

Post by pythoncoder » Mon Aug 31, 2020 2:17 pm

That will work with a finally clause that closes the file, which I believe is exactly what a context manager does.

The reset button is kinda fatal ;)
Peter Hinch
Index to my micropython libraries.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Testing if ubinascii.unhexlify exists ...

Post by davef » Mon Aug 31, 2020 7:15 pm

I didn't put in the full coding for a typical file read statement. Following an example I found somewhere:

Code: Select all

try:
    with open('/batt_voltage_values', 'r') as infile:
        batt_voltage_values_lines = infile.readlines()
except IOError:
    print('oops!')
I re-read https://realpython.com/read-write-files-python/ and it suggests either a try ... finally or a with open

Hopefully, one can put something more useful in the

Code: Select all

except IOError:
statement.

Post Reply