MrYsLab / PyMata

A Python client class library for Interaction with Standard Firmata
GNU Affero General Public License v3.0
95 stars 40 forks source link

Exceptions reported when rerunning an application #5

Closed MrYsLab closed 9 years ago

MrYsLab commented 9 years ago

On occasion the exceptions shown below are encountered when rerunning an application. It appears to occur mostly on Leonardo boards. Work around until a fix is provided is to remove the USB cable from the Arduino and then plug it back in.

File "C:\Python27\Lib\threading.py", line 783, in bootstrap self.bootstrap_inner() File "C:\Python27\Lib\threading.py", line 810, in __bootstrap_inner self.run() File "C:\Python27\Lib\site-packages\PyMata\pymata_command_handler.py", line 822, in run method(command_data) File "C:\Python27\Lib\site-packages\PyMata\pymata_command_handler.py", line 486, in digital_message prev_data = self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]

IndexError: list index out of range

MrYsLab commented 9 years ago

The cause of the problem was determined. Leonardo boards, unlike Uno boards continue sending update data after the serial port is closed. PyMata now sends a Firmata Reset command to the Arduino so silence it before closing the port. To handle the case of a user pressing control-c before the application finishes processing, a signal handler should be added to the application. PyMata handles the case where control-c is pressed before pyamata.init completes.

All examples will be updated to include this signal handler.

The signal handler that optionally can be added to applications to gracefully handle control-c is:

board = None

# create a PyMata instance
board = PyMata("/dev/ttyACM0")

def signal_handler(signal, frame):
    print('You pressed Ctrl+C!!!!')
    if board != None:
        board.reset()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
MrYsLab commented 9 years ago

Fixed with version 2.02