OpenBCI_Python OSX FTDI driver freeze

edited June 2015 in Software
Today i fixed a nasty bug in the FTDI driver that occurred often when debugging my application based on the OpenBCI_Python interface.
The bug occured only on my OSX machine (10.9.4, Mid 2010 MBP), and usually forced me to do a powercycle, always risking data loss. :(

It seems that when the OpenBCI is streaming data, the incoming data absolutely must be read from the serial port, or otherwise the whole input system freezes. While the system seems to be running fine, all input devices fail, and even new USB devices, like an external keyboard, won't be detected or powered on. So something in the FTDI driver seems to halt the whole IOKit subsystem of the OSX kernel.

Luckily, i found a workaround for this problem. :)

The OpenBCI driver needs a run in a separate thread (which it probably should anyway, if you plan to use it with a GUI).

In my case, i am using Qt (with the excellent PyQtGraph library ;) , so i can use the signal-slot-mechanism to send the
data out of the driver thread without locking. In case anything unexpected happens, exceptions are handled by entering a endless loop that reads the serial port.
So no more freezes, hooray! \o/

This is my driver adapter code:

from PySide import QtCore, QtGui
from open_bci_v3 import OpenBCIBoard

class OpenBCIThread(QtCore.QThread):
newPacket = QtCore.Signal(object)

def __init__(self, ttyPath = '/dev/ttyUSB0'):
super(OpenBCIThread, self).__init__()

self.board = OpenBCIBoard(ttyPath)

def handlePacket(self, sample):
self.newPacket.emit(sample.channel_data)
QtGui.QApplication.instance().processEvents()

def run(self):
try:
self.board.print_register_settings()
self.board.start_streaming(self.handlePacket)

# Important workaround for OSX: If the input is not kept on being read, the whole (input sub-)system freezes
except Exception, e:
print "Exception", e
while True:
self.board.ser.read()

You can find the rest of my code on GitHub:
http://github.com/strfry/OpenNFB



Best Regards,
Jonathan
Sign In or Register to comment.