pyboard.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zs6mdh
Posts: 9
Joined: Fri Mar 07, 2014 4:58 am

pyboard.py

Post by zs6mdh » Sun Oct 19, 2014 10:36 am

This script is awesome, well it looks that way. I think Im either doing something wrong or missing something. I like the way I can develop without having to copy the file to the board and reset every time, its especially annoying when I have to keep changing the boot.py to enable and disable the "hard drive".

I tried the exact thing shown in the video, https://www.youtube.com/watch?v=5LbgyDmRu9s#t=288
My code is as follows:

Code: Select all

#test py

import pyb

pyb.Led(1).On()
print ("hello ")
When I run ,

Code: Select all

python3 pyboard.py test.py
It works and switches the light on and gives the hello output

Now lets say I have the following code:

Code: Select all

#test.py
import pyb
print("starting")

sw = pyb.Switch()
rtc = pyb.RTC()
rtc.datetime((2014, 9, 1, 4, 13, 0, 0, 0))
print(rtc.datetime())
T = rtc.datetime()

while True:
    T = rtc.datetime()
    if T[7] < 127:
        S = "%04d/%02d/%02d %02d:%02d" % (T[0],T[1],T[2],T[4],T[5])
    else:
        S = "%04d/%02d/%02d %02d %02d" % (T[0],T[1],T[2],T[4],T[5])

    print (S)
    pyb.LED(1).toggle()
    pyb.delay(1000)

There is a permanent loop in this one and I get no output. Is there a way I can do this and still get the live output?

Im trying to develop with writing to files and the only other way I can see is as follows
1. I copy the py file to the device
2. reset it without hard drive access from the pc
3. check on repl for the debug output
4. modify the file on the pc
5. reset the pyboard again with hard drive access
6. start from 1 again.

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

Re: pyboard.py

Post by pythoncoder » Sun Oct 19, 2014 2:57 pm

Your code runs fine here. I'd consider updating your firmware if you're getting no output.
Regards, Pete
Peter Hinch
Index to my micropython libraries.

zs6mdh
Posts: 9
Joined: Fri Mar 07, 2014 4:58 am

Re: pyboard.py

Post by zs6mdh » Mon Oct 20, 2014 5:26 pm

Ok I upgraded to pybv10-2014-10-20-v1.3.3-99-g072bd07.dfu and it still does the same thing.

The program runs, and the led flashes
No output happens when you loop
Another thing if I use Ctrl-C I get the command line but the program carries on running.

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

Re: pyboard.py

Post by dhylands » Mon Oct 20, 2014 6:18 pm

It looks like pyboard.py collects the output and doesn't actually print it out until the script finishes.

If you edit pyboard.py and add the following 2 lines after the else: here https://github.com/micropython/micropyt ... py#L49-L52

Code: Select all

                print(str(data, encoding='ascii'), end='')
                data = b''
so the read_until function will look like:

Code: Select all

    def read_until(self, min_num_bytes, ending, timeout=10):
        data = self.serial.read(min_num_bytes)
        timeout_count = 0
        while True:
            if self.serial.inWaiting() > 0:
                data = data + self.serial.read(self.serial.inWaiting())
                time.sleep(0.01)
                timeout_count = 0
            elif data.endswith(ending):
                break
            else:
                print(str(data, encoding='ascii'), end='')
                data = b''
                timeout_count += 1
                if timeout_count >= 10 * timeout:
                    break
                time.sleep(0.1)
        return data
then it will print results as it goes along. This is just a hack and probably breaks the original functionality of pyboard.py.

Also make sure that you've disconnected your terminal software (that was connected to the debug output) before running pyboard.py

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

Re: pyboard.py

Post by dhylands » Mon Oct 20, 2014 6:23 pm

Currently the Control-C gets intercepted by the host side script and isn't forwarded (which is why the script keeps running)

zs6mdh
Posts: 9
Joined: Fri Mar 07, 2014 4:58 am

Re: pyboard.py

Post by zs6mdh » Tue Oct 21, 2014 2:41 pm

Thank you thank you thank you. Thats sooo awesome. It works very well now. This should speed things up.

Thank you so much for all your help.

As soon as I have some code that can be used by others I will post it.

zs6mdh
Posts: 9
Joined: Fri Mar 07, 2014 4:58 am

Re: pyboard.py

Post by zs6mdh » Tue Oct 21, 2014 6:20 pm

hehe, I see what you mean. there might be a problem with the code now. Its not affecting me really but the following does happen every so often.

After a random time I get the following error.
Traceback (most recent call last):
File "pyboard.py", line 181, in <module>
main()
File "pyboard.py", line 178, in main
execfile(file, device=args.device)
File "pyboard.py", line 105, in execfile
output = pyb.execfile(filename)
File "pyboard.py", line 96, in execfile
return self.exec(pyfile)
File "pyboard.py", line 87, in exec
raise PyboardError('timeout waiting for EOF reception')
__main__.PyboardError: timeout waiting for EOF reception

zs6mdh
Posts: 9
Joined: Fri Mar 07, 2014 4:58 am

Re: pyboard.py

Post by zs6mdh » Tue Oct 21, 2014 6:57 pm

Ok it was annoying me and I decided to take a closer look. It was much easier than I thought to "fix"

I change the one line from

Code: Select all

data = self.read_until(2, b'\x04>')
to

Code: Select all

data = self.read_until(5, b'\x04>')
That makes the timeout a bit higher, I also made my program send regular print statements and no more timeouts :)

For reference the whole procedure is as follows now

Code: Select all

    def exec(self, command):
        command_bytes = bytes(command, encoding='ascii')
        for i in range(0, len(command_bytes), 32):
            self.serial.write(command_bytes[i:min(i+32, len(command_bytes))])
            time.sleep(0.01)
        self.serial.write(b'\x04')
        data = self.serial.read(2)
        if data != b'OK':
            raise PyboardError('could not exec command')
        data = self.read_until(5, b'\x04>')
        if not data.endswith(b'\x04>'):
            print(data)
            raise PyboardError('timeout waiting for EOF reception')
        if data.startswith(b'Traceback') or data.startswith(b'  File '):
            print(data)
            raise PyboardError('command failed')
        return data[:-2]
As an added bonus I worked out a few cool things.
Pyboard has a raw rpl which makes this possible.
Debugging is soo much faster than copying source code across
I basically have the source on my pc and run "python3 pyboard.py main.py" everytime
You can unmount the usb drive and then run your program instead of having to change the boot.py every-time.
Im stupid when it comes to python and have lots to learn

I hope the above helps someone else as well.

Post Reply