PySide, pySerial and threading causes segfault
-
I'm trying to use PySide and pySerial to make a cross-platform app that interacts with the serial port. I originally used Qtimers to poll the serial for data, but this put a large load on the cpu. So I've been attempting to use threads instead.
unfortunately using threads causes either Qt or pySerial to Segfault.
I've tried both python threads and QThreads, same problem, it happens on OSX, windows 8 and Ubuntu 12.04. using python 2.7 and Qt4
"This question seemed to have a similar problem,":http://stackoverflow.com/questions/18378536/using-a-qthread-in-pyqt-for-serial-communication-w-pyserial
"this thread also seems to be a similar problem":http://www.gossamer-threads.com/lists/python/python/1064621
below is a small app that recreates the problem
@
#! /usr/bin/python
import sys
import serial
import threading
from PySide import QtCore, QtGui, QtUiToolsclass serial_port_class(object):
def init(self):
self.connected = Falsedef __del__(self): self.disconnect_port() def connect_port(self): try: self.serial_port = serial.Serial("/dev/tty.usbmodem1451", 9600, timeout = None) self.connected = True except serial.SerialException, e: self.connected = False if self.connected: self.serial_thread = threading.Thread(target=self.recieve_port, args=(self)) self.serial_thread.start() def disconnect_port(self): self.connected = False self.serial_thread.join() self.serial_port.close() def recieve_port(self): while self.connected: try: text = self.serial_port.read(1) if text != '': print "text" except serial.SerialException, e: connected = False
class KeyPressEater(object):
def connect_port(self):
self.serial_thread = threading.Thread(target=self.eventFilter)
self.serial_thread.start()def eventFilter(self): global serial_port while serial_port.connected == True: ch = raw_input() if serial_port.connected == True: serial_port.serial_port.write(ch) return QtCore.QObject.eventFilter(self, obj, event)
def main():
global serial_port
serial_port = serial_port_class()
serial_port.connect_port()
heyeater = KeyPressEater()if serial_port.connected== True: heyeater.connect_port() heyeater.serial_thread.join()
if name == 'main':
main()@
sometimes it takes a bit of data IO to trigger the segfault, sometimes a single character will trigger it...
I'm not really sure how I would go about debugging this further....
All help is appreciated!!!!!!