如何在pyserial中使用流量控制

时间:2014-01-22 23:11:46

标签: python raspberry-pi wireless pyserial flow-control

我需要帮助是使用串口,​​我有3DR无线电遥测连接到我的python和我的Windows PC的另一面,我有小python代码,不断将数据写入串口并读取,读取可能不是问题,或者它可能是后来的......

问题是我担心太多的写入可能会导致一些缓冲区溢出,每次我搜索解决方案是启用rts / cts流量控制,我不知道如何使用它?如果我设置这些会发生什么然后pyserial会做什么,我怎么能控制我的写?它确实令人困惑..

硬件流程ccontol,我不确定它是否可行,因为我刚刚连接rx tx接地和电源到我的覆盆子pi,即使尝试将其他流量控制引脚连接到pi,我不确定它是否有效或由3dr无线电遥测支持..我相信软件流控制将是现在的好的和简单的解决方案。

这是我的代码..

for channel in list(self.__channelDict.values()):

            # Addition for channel priority later
            # We check if the channels in the list is active
            if channel.getChannelActive() is True:
                # Check if we have reached the max count
                if (messageCount >= (self.__NoOfMessagesInUARTStream - 1)) or UARTForceSend:
                    self.sendUARTStream(UARTCacheBuffer, messageCount, UARTStreamCRC)
                    # Reset
                    messageCount  = 0
                    UARTStreamCRC = 0
                    UARTCacheBuffer.emptyBuffer()

                message = channel.RetriveMessage(queueType = 1, raw = True)
                # # there is no TX message in this channel
                if message is None:
                    continue # continue with next channel
                else:
                    UARTStreamCRC = binascii.crc32(message, UARTStreamCRC)
                    UARTCacheBuffer.append(message, raw = True)
                    messageCount +=1

和写入串口的功能

def sendUARTStream(self, UARTCacheBuffer, messageCount, UARTStreamCRC):
    # retrieve all the data from the buffer and create a stream packet
    UARTFrame        = None       # Used to forward the data
    UARTStreamHeader = None

    # Create the message header
    if messageCount == 0:
        # looks like all channels are empty 
        return 0
    else:
        messageArray     = UARTCacheBuffer.getBuffer()
        print(messageArray)
        print('messageCount = ' + str(messageCount) +  'crc = ' + str(UARTStreamCRC))

        UARTFrame[:self.__UARTStreamHeaderFormat.size] = self.createHeader(messageCount, UARTStreamCRC)
        UARTFrame[self.__UARTStreamHeaderFormat.size : self.__UARTStreamHeaderFormat.size + self.__messageFormat * messageCount] =  messageArray
        # Its time to finally send the data
        print('UARTFrame  = ##' + str(UARTFrame))
        self.__txPort.write(UARTFrame)
    return messageCount

0 个答案:

没有答案