Python串行写入在一段时间后变慢

时间:2014-03-13 15:25:29

标签: python python-2.7 serial-port pyserial

Windows 8 x64上的Python 2.7程序使用pySerial不断写入串行COM端口(USB端口)。它会写入大约30行,然后休眠2-3秒并再次写入。从一个帖子中调用write()

问题:写入约2分钟后,串行写入开始减慢到每秒约3-4次写入。

def write(self):
    print 'checkpoint A'
    payload = str(self.number) + 'H\n'

    print 'checkpoint B'
    self.serial.write(payload)

    print 'checkpoint C'

使用print命令,我们注意到self.serial.write()开始需要一些时间才能完成。

为什么写入会在一段时间后开始变慢?


class CookingPart(object):

    def __init__(self, number, serial):

        self.number = number
        self.serial = serial

        self.switch = 0     # Off by default


    def toggle(self):

        if self.switch:
            self.close()

        else:
            self.open()


    def open(self):
        self.switch = 1
        payload = str(self.number) + 'H\n'
        self.serial.write(payload)


    def close(self):
        self.switch = 0
        payload = str(self.number) + 'L\n'
        self.serial.write(payload)



class Cooker(object):

    def __init__(self, screen):
        self.screen = screen
        self.active = False


    def toggle(self):
        if not self.active:
            self.active = True
            self.run()
        else:
            self.active = False
            self.recipeThread.stop()


    def run(self):
        # Start Thread
        self.recipeThread = RecipeThread(recipe, self.screen)
        self.recipeThread.connect(self.recipeThread, QtCore.SIGNAL("update(float)"), self.updateProgress)
        self.recipeThread.start()



class RecipeThread(QtCore.QThread):

    def __init__(self, recipe, screen):
        super(RecipeThread, self).__init__()
        self.recipe = recipe
        self.screen = screen
        self.stopThread = False

    def run(self):

        # EXECUTE
        for layer in self.recipe['layers']:

            for i in range(layer['loop']):

                for ingredient in layer['ingredients']:
                    for i in range(ingredient['loop']):
                        for step in ingredient['steps']:

                            # Check if it should be stopped
                            if self.stopThread:
                                print 'Stopping'
                                self.stop = True    # reset flag
                                return

                            # Open/Close Part
                            for cookingPartNumber in cookingPartNumbers:
                                if cookingPart in cookingPartsToUse:
                                    cookingParts[cookingPartNumber].open()
                                else:
                                    cookingParts[cookingPartNumber].close()

                            # Wait
                            time.sleep(float(duration))

                            # Update Runtime
                            self.updateProgress(duration)

                print 'Cycle done.'


    def stop(self):
        self.stopThread = True


    def updateProgress(self, duration):
        # Emit SIGNAL
        self.emit(QtCore.SIGNAL('update(float)'), self.runTime)

0 个答案:

没有答案