not able to write into opened file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
eduardo
Posts: 31
Joined: Mon Jan 05, 2015 6:48 pm

not able to write into opened file

Post by eduardo » Sun Feb 01, 2015 2:44 pm

In the following code I generate a file, but nothing is written to the file:

I tried this frome the console, and everything worked.

For sure its because I am stupid. but I dont know where.
Is anyone willing to give me a helpful hint?

Thanx very much

ed
--------------------------------------------
# main.py -- put your code here!

import pyb
import os

pyb.usb_mode('CDC+HID') # dont know If this is really needed

w1 = pyb.ADC('X19')
w2 = pyb.ADC('X20')

pyb.LED(3).on() # just to see it working

log=open('weightlog2.txt','w')

for i in range(10):
x1=w1.read()
x2=w2.read()
summe=x1+x2 #this is the value I want to write to the file
print(x1,x2, summe)
t = pyb.millis() # for test purpose
print(t) # for testpurpose
log.write('{}\n'.format(t)) # doesnt work as intended

pyb.LED(2).off()
pyb.delay(800)
pyb.LED(2).on()
pyb.delay(200)

log.close()

pyb.usb_mode('CDC+MSC') # setting back to mass storage so that I can look at the file.

pyb.LED(3).off()
print("Done!")

eduardo
Posts: 31
Joined: Mon Jan 05, 2015 6:48 pm

Re: not able to write into opened file

Post by eduardo » Sun Feb 01, 2015 2:48 pm

Sorry for not putting it into

Code: Select all

Code
But I am no longer allowed to delete or edit my old posting.

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

Re: not able to write into opened file

Post by dhylands » Sun Feb 01, 2015 7:56 pm

Try using a filename of '/flash/weightlog2.txt'

You can only modify pyb.usb_mode in boot.py. By the time main.py runs, its too late.

It also seems that you can't query pyb.usb_mode, so I created the following file, which you can use as boot.py, which writes the mode it used into /flash/usb_mode.py

Code: Select all

import pyb

sw_pressed = False

sw = pyb.Switch()
led = pyb.LED(1) # Red
for i in range(20): # 20 per second
    led.toggle()
    if sw():
        sw_pressed = True
        break
    pyb.delay(50)

led.off()
mode = 'CDC+HID'
if sw_pressed:
    mode = 'CDC+MSC'

with open('/flash/usb_mode.py', 'w') as mode_file:
    mode_file.write("mode = '{}'\n".format(mode))

pyb.usb_mode(mode)
I was then able to create a main.py that looks like this:

Code: Select all

import usb_mode
import sys
import pyb

def main():
    led = pyb.LED(3)
    while True:
        led.on()
        pyb.delay(100)
        led.off()
        pyb.delay(100)
        led.on()
        pyb.delay(100)
        led.off()
        pyb.delay(700)

if usb_mode.mode == 'CDC+MSC':
    print("CDC+MSC mode")
else:
    main()
Instead of calling main() you could also put your real code inside say program.py and then do:

Code: Select all

import usb_mode

if usb_mode.mode == 'CDC+MSC':
    print("CDC+MSC mode")
else:
    import program
and have program.py with:

Code: Select all

import pyb

led = pyb.LED(3)
while True:
    led.on()
    pyb.delay(100)
    led.off()
    pyb.delay(100)
    led.on()
    pyb.delay(100)
    led.off()
    pyb.delay(700)

Post Reply