Write file via Serial / REPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Write file via Serial / REPL

Post by yllumi » Sat May 02, 2015 9:34 am

I have tried this code from main.py and run the pyboard,

Code: Select all

f = open('/flash/thefile.txt', 'w')
f.write('Hello World\n')
f.close()
and the file shown after I reset the pyboard.

However if I run the code from REPL the code run as well and after I call os.listdir() the file is in the list. But after I reset the pyboard and check the /flash from file manager, the file doesn't exist. I assume that when we run program from REPL, the program run in memory and not actually written to the /flash. Is that true? Then how can we write file from REPL?

I ask this because I want to save code to main.py via serial port. I am currently developing web application for micropython as you can see in this thread http://forum.micropython.org/viewtopic.php?f=5&t=637. The app can now write file but via filesystem API. I think it would be easier if it can save code to file via Serial.

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

Re: Write file via Serial / REPL

Post by dhylands » Sat May 02, 2015 4:16 pm

Writing from the REPL should be fine. You'll want to call os.sync() to ensure the buffers actually get written. With internal flash, the writes are delayed a bit compared to using an external sdcard.

Also, if you're using USB Mass Storage, then its possible for the host to completely overwrite your changes. The way USB Mass Storage was designed, it assume that the host has exclusive access. Basically if you're going to have the pyboard write to the file system, you should disable USB Mass Storage. This can be done by putting the line:

pyb.usb_mode('VCP') in your boot.py file (this will enable USB serial). By default, it uses pyb.usb_mode('VCP+MSC') which enables USB serial and USB Mass Storage.

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Write file via Serial / REPL

Post by yllumi » Sun May 03, 2015 5:50 am

I run this code on REPL:

Code: Select all

>>> import os
>>> f = open('/flash/main.py', 'w')
>>> f.write('while True:\n')
12
>>> f.write('  pyb.LED(3).toggle()\n')
22
>>> f.write('  pyb.delay(1000)\n')
18
>>> f.close()
>>> os.sync()
and the code run successfuly after I press Ctrl + D. But if I check the main.py via mass storage, the code isn't synced. But it is no big deal.

and btw, is pyb.usb_mode('VCP') same as pyb.usb_mode('CDC')?

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

Re: Write file via Serial / REPL

Post by dhylands » Sun May 03, 2015 6:23 am

yllumi wrote:I run this code on REPL:

Code: Select all

>>> import os
>>> f = open('/flash/main.py', 'w')
>>> f.write('while True:\n')
12
>>> f.write('  pyb.LED(3).toggle()\n')
22
>>> f.write('  pyb.delay(1000)\n')
18
>>> f.close()
>>> os.sync()
and the code run successfuly after I press Ctrl + D. But if I check the main.py via mass storage, the code isn't synced. But it is no big deal.
That's expected. The host thinks it has exclusive ownership of the flash drive and won't see anything added by the pyboard until its been unmounted and remounted on the host side.

Control-D doesn't bring down the USB link, so the host doesn't know that the pyboard did a soft-reset.
yllumi wrote:and btw, is pyb.usb_mode('VCP') same as pyb.usb_mode('CDC')?
Yes - VCP is the new way, but CDC is supported for backwards compatability.

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Write file via Serial / REPL

Post by yllumi » Sun May 03, 2015 6:28 am

Thank you very much, as always :D

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Write file via Serial / REPL

Post by yllumi » Sun May 03, 2015 6:39 am

Ah, one more thing.
I tried to add pyb.usb_mode('VCP') and then write the code to main.py via REPL like above. But when I go to safe mode to open mass storage, the file isn't in utf8 format or in other words, the file can't be opened.
Image
How we set the file to UTF-8 format?

Update:
Sorry, after I try it once more, the file can be opened. I don't know why but sometimes it happen. I have experienced it twice.

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

Re: Write file via Serial / REPL

Post by dhylands » Sun May 03, 2015 7:10 am

The funny y with two dots is the 0xFF character which corresponds to erased flash.

That means something in the file system got hooped.

I tried your steps and everything worked for me.

What I did was to edit boot.py on the host to contain the usb_mode command. Then I uplugged the USB cable and plugged it back in again. You can't just do a Control-D here. You need to RESET the pyboard or unplug it, otherwise the usb_mode command won't actually take effect. Control-D doesn't reinitialize USB, so usb_mode will have no effect. I'm pretty sure that the usb_mode command needs to be in boot.py as well (main.py is too late)

Then I did the REPL thing of writing out main.py, did Control-D, saw the LED flashing, pressed USR and RESET and let go of RESET with the orange LED flashing (went into safe mode) and main.py contained:

Code: Select all

2110 >cat /media/dhylands/PYBFLASH/main.py 
while True:
  pyb.LED(3).toggle()
  pyb.delay(1000)

Post Reply