0

我是 python 新手,我正在一个线程中实现一个简单的串行采集。我可以使用一个类来获取数据

class CaptureAngles(threading.Thread, port)
    def __init__(self):
        threading.Thread.__init__(self)
        self.port_name = port
        ...

    def run():
        self.connect(self.port_name)
        ...

但是,为了更好地使用特征库与图形界面集成,我编写了如下代码,但不再有效。我无法定义从特征开始的线程的属性,我做错了什么?

这是报错

AttributeError:“CaptureAngles”对象没有属性“port_name”

这是完整的代码:

from threading import Thread
from time import sleep
from enthought.traits.api import *
from enthought.traits.ui.api import View, Item, ButtonEditor
from Queue import Queue

class TextDisplay(HasTraits):
    string =  String()

    view= View( Item('string',show_label=False, springy=True, style='custom' ))


class CaptureAngles(Thread):
    self.port_name = String('COM5') 

    def connect(self, port_name):
        self.port = serial.Serial(
            port = port_name,
            baudrate = 9600,
            )   
        self.display.string='Opening Serial Port...' + self.display.string
        self.port.close()
        self.port.open()

    def run(self):
        self.connect(self.port_name)
        self.display.string = 'Arduino started\n' + self.display.string
        self.port.flushInput()
        self.port.flushOutput()
        self.port.readline() # Discard first package (can be corrupt)
        while not self.wants_abort:
            rcv = self.port.readline() # Read the data and split into words 
            angle = int(rcv)
            self.display.string = '%d angle captured\n' % n_img \
                                                    + self.display.string
            self.close()

    def close(self):
        self.port.close()
        self.display.string='...Serial Port Closed!' + self.display.string


class Arduino(HasTraits):
    start_stop_capture = Button()
    display = Instance(TextDisplay)
    capture_angles = Instance(CaptureAngles)
    capture_angles.port_name = 'COM5'
    view = View(Item('start_stop_capture', show_label=False ))

    def _start_stop_capture_fired(self):
        if self.capture_angles and self.capture_angles.isAlive():
            self.capture_angles.wants_abort = True
        else:
            self.capture_angles = CaptureAngles()
            self.capture_angles.wants_abort = False
            self.capture_angles.display = self.display
            self.capture_angles.start()

class MainWindow(HasTraits):
    display = Instance(TextDisplay, ())

    arduino = Instance(Arduino)

    def _arduino_default(self):
        return Arduino(display=self.display)

    view = View('arduino','display', style="custom", resizable=True)

if __name__ == '__main__':
    MainWindow().configure_traits()
4

1 回答 1

0

好的,我明白了:我在创建实例之前添加了属性 port_name。

class Arduino(HasTraits):
    start_stop_capture = Button()
    display = Instance(TextDisplay)
    capture_angles = Instance(CaptureAngles)
    capture_angles.port_name = 'COM5' # <-- wrong: the object is not created yet
    ...

代替:

def _start_stop_capture_fired(self):
    if self.capture_angles and self.capture_angles.isAlive():
        self.capture_angles.wants_abort = True
    else:
        self.capture_angles = CaptureAngles()
        self.capture_angles.port_name = 'COM5' # <-- correct
    ...
于 2013-09-02T20:43:17.320 回答