Need help debugging with REPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
BillG
Posts: 9
Joined: Fri Feb 05, 2016 4:12 pm

Need help debugging with REPL

Post by BillG » Fri Apr 01, 2016 3:12 pm

This is my first post. By way of introduction, I'm a longtime programmer currently using E-Lab Pascal (from Germany) for embedded projects with Atmel AVR chips, and Labview for data acquisition and analysis. I also used Ada for about 8 years. I've never used Python and am just beginning to work with Micropython. I work at a university and have a prototyping shop where I develop software and hardware for research projects.

I want to set up a wireless connection with Peter Hinch's radio_fast. I haven't gotten that to work yet; it won't communicate with the Sparkfun nRF24l01+ module. But that's kind of beside the point. Where I really need help is getting the traceback from REPL when a program dies. I'm using PuTTY on a Windows machine. I connect my Pyboard ver. 1.1 via USB. I'm almost never able to see the traceback.

Let's say I press the RESET button on the Pyboard. At that point the COM port connection to putty is broken and then restarted as the Pyboard boots and the program runs. If it's a broken program, it will immediately break out to REPL and print a traceback. Even if I had putty running before the reset, I can't get the traceback to show up. Pressing Ctrl-C doesn't help. So, how do people keep a COM port open in order to see the traceback when it occurs?

I've also tried using pyb.repl_uart() to get output sent to someplace other than the USB COM port, but that doesn't work either. Maybe I'm not using it correctly - an example would be welcome.

Thanks for any help.

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

Re: Need help debugging with REPL

Post by dhylands » Fri Apr 01, 2016 3:31 pm

When I'm debugging stuff, I tend to not call my program main.py but call it something else.

Then when I get to the REPL, I'll import whatever I called my main program.

Under linux, I often use a little python program I wrote called usb-ser-mon.py which detects USB disconnects and reconnects, and it will automatically open and close the tty. Since its a program it can do it fast enough that it will catch that startup stuff.
https://github.com/dhylands/usb-ser-mon

BillG
Posts: 9
Joined: Fri Feb 05, 2016 4:12 pm

Re: Need help debugging with REPL

Post by BillG » Fri Apr 01, 2016 5:54 pm

Any idea if this will work under Windows?

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

Re: Need help debugging with REPL

Post by dhylands » Fri Apr 01, 2016 11:16 pm

I don't see why not. I do it using the linux version all the time.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Need help debugging with REPL

Post by Roberthh » Sat Apr 02, 2016 6:19 am

Hello Dave,
usb-ser-mon seems to me a useful tool. I tried it and ran into a bug. Here is the screen content:

Code: Select all

robert@DELL-HH:~/Downloads/MicroPython/usb-ser-mon$ ./usb-ser-mon.py -n Micro
USB Serial device with vendor 'MicroPython' serial '347A35663333' connected @/dev/ttyACM0
Use Control-X to exit.
Traceback (most recent call last):
                                    File "./usb-ser-mon.py", line 310, in main
                                                                                  debug=args.debug, echo=args.echo)
                 File "./usb-ser-mon.py", line 192, in usb_serial_mon
                                                                         serial_port.write(data)
                                                                                                  File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 491, in write
                                                                                         d = to_bytes(data)
         File "/usr/local/lib/python3.4/dist-packages/serial/serialutil.py", line 76, in to_bytes
                                                                                                     b.append(item)  # this one handles int and str for our emulation and ints for Python 3.x
                                                                                         TypeError: an integer is required
                      robert@DELL-HH:~/Downloads/MicroPython/usb-ser-mon$ 

I'm using debian Jessie, Python 3.4
Regards, Robert

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

Re: Need help debugging with REPL

Post by dhylands » Sat Apr 02, 2016 6:39 am

That's actually an error in pyserial (Edit: Hmm. maybe not).

A new version of pyserial was released a while ago. So the first thing I'd try is to run:

Code: Select all

pip3 install --upgrade pyserial
You may need to use sudo to install globally.

If that doesn't fix it, then I think that there is a way to work around in my code.

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

Re: Need help debugging with REPL

Post by pythoncoder » Sat Apr 02, 2016 6:40 am

@BillG Regarding my radio_fast driver, I'm assuming you have a Pyboard with Sparkfun radio at each end. After copying the driver I'd start out by copying tests.py to both Pyboards. Ensure you've edited config.py to provide master_config and slave_config values to match your wiring and copy that to both Pyboards. At the REPL you should be able to issue

Code: Select all

import tests
tests.test_master()
at one end, and

Code: Select all

import tests
tests.test_slave()
at the other. Note that, as suggested by @dhylands, this assumes a main.py which does nothing - the Pyboards take you to a REPL and you run your tests from there.
Peter Hinch
Index to my micropython libraries.

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

Re: Need help debugging with REPL

Post by dhylands » Sat Apr 02, 2016 7:06 am

@Roberthh - I just pushed a change to usb-ser-mon.py that should address the particular crash you saw above.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Need help debugging with REPL

Post by Roberthh » Sat Apr 02, 2016 10:32 am

Hi Dave,
thanks for the swift reply. That change did change the error message.

Code: Select all

robert@DELL-HH:~/Downloads/MicroPython/usb-ser-mon$ ./usb-ser-mon.py -n Micro
USB Serial device with vendor 'MicroPython' serial '347A35663333' connected @/dev/ttyACM0
Use Control-X to exit.
Traceback (most recent call last):
                                    File "./usb-ser-mon.py", line 312, in main
                                                                                  debug=args.debug, echo=args.echo)
                 File "./usb-ser-mon.py", line 194, in usb_serial_mon
                                                                         serial_port.write(data)
                                                                                                  File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 518, in write
                                                                                         d = to_bytes(data)
         File "/usr/local/lib/python3.4/dist-packages/serial/serialutil.py", line 58, in to_bytes
                                                                                                     raise TypeError('unicode strings are not supported, please encode to bytes: %r' % (seq,))
                                                                                          TypeError: unicode strings are not supported, please encode to bytes: '\r'
                                                                robert@DELL-HH:~/Downloads/MicroPython/usb-ser-mon$ 
But it may also be that my system is a little bit messy. I have also change the first line of the script to call python3, otherwise python2 is chosen.
Robert

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

Re: Need help debugging with REPL

Post by dhylands » Sat Apr 02, 2016 2:33 pm

That particular script was written for python 2, and never tested with 3.

Post Reply