缺少pythonxbee库和树莓包

2024-09-28 12:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过2个Xbee S1模块将一些包从MSP发送到Raspberry pi3。你知道吗

Xbee被配置为带有转义帧的digimesh2.4,一个作为路由器,另一个作为协调器。在树莓的连接是与USB加密狗。你知道吗

我的代码,在MSP,每10us发送一个带有CTS流控制的包。当协调器插入我运行windows的PC时,我可以通过XCTU看到所有的包都到了,一切都很好!!!你知道吗

但是,当软件狗在raspberry运行Raspbian和下面的代码时,有些包无法到达。你知道吗

由于XCTU的所有功能都正常工作,问题就出在代码中,可能是处理串行端口或类似的东西。你知道吗

所以,任何帮助都将不胜感激!!!你知道吗

你知道吗开始.py地址:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# copyright: Thiago Cruz <thiagoalberto@gmail.com>

import sys
import os

from PyQt4 import QtGui
from middleware.QueueMiddleware import QueueMiddleware
from jobs.ScheduleJob import ScheduleJob


def startQueue():
    queue = QueueMiddleware()
    queue.start()

def assyncSchedule():
    schedule = ScheduleJob()
    schedule.run()

def runApp():
    startQueue()

    app = QtGui.QApplication(sys.argv) 


    sys.exit(app.exec_())


if __name__ == '__main__':
    runApp()

你知道吗队列中间件.py地址:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# copyright: Thiago Cruz <thiagoalberto@gmail.com>

import threading
import time
import serial
import Queue

from middleware.DataProcessorGear import DataProcessorGear
from xbee import ZigBee

minutes = 0

class QueueMiddleware(threading.Thread):
    __instance = None

    PORT = '/dev/ttyUSB0'
    BAUD_RATE = 9600

    # The XBee addresses I'm dealing with
    BROADCAST = '\x00\x00\x00\x00\x00\x00\xFF\xFF'
    UNKNOWN = '\xFF\xFE' # This is the 'I don't know' 16 bit address

    def __new__(cls):
        if QueueMiddleware.__instance is None:
            QueueMiddleware.__instance = super(QueueMiddleware, cls).__new__(cls)

        return QueueMiddleware.__instance

    def __init__(self):
        QueueMiddleware.__instance = self
        threading.Thread.__init__(self)
        self.dataPacketsQueue = Queue.Queue()
        # Create API object, which spawns a new thread
        self.ser = serial.Serial(
                                    port='/dev/ttyUSB0',
                                    baudrate = 9600,
                                    parity=serial.PARITY_NONE,
                                    stopbits=serial.STOPBITS_ONE,
                                    bytesize=serial.EIGHTBITS,
                                    timeout=1
                                )
        self.xbeeApi = ZigBee(self.ser, callback=self.message_received, escaped=True)
        print 'start queue'

    def __del__(self):
        # halt() must be called before closing the serial
        # port in order to ensure proper thread shutdown
        self.xbeeApi.halt()
        self.ser.close()
        self.processor = None

    def run(self):
        # Do other stuff in the main thread
        while True:
            try:
                time.sleep(1)
                #if self.dataPacketsQueue.qsize() > 0:
                #    lock = threading.Lock()
                #    processor = DataProcessorGear(self.dataPacketsQueue, lock)
                #    processor.start()
            except KeyboardInterrupt:
                break

    def message_received(self, data):
        global minutes

        minutes += 1
        print minutes
        self.dataPacketsQueue.put(data, block=True, timeout=None)

我已经试过改变时间。睡眠(),并已抑制后续线程的执行以“隔离”问题。你知道吗

我的控制台显示从~120到~170的值。MSP只发送200个数据包!!你知道吗

所以。。。有什么猜测吗??你知道吗

提前谢谢。你知道吗


Tags: instance代码fromimportselfnonedefsys
2条回答

一种解决方案……

在我的代码中尝试了不同的方法之后,甚至尝试了下面的脚本。。。你知道吗

#!/usr/bin/python
import serial

serialport = serial.Serial("/dev/ttyUSB0", 115200, timeout=None,rtscts=True,dsrdtr=True)

while True:
    serialport.flush()
    command = serialport.readline()
    print str(command).encode('hex')

通过将XBee MR参数(Mesh unicast retries-网络数据包传递尝试的最大次数)更改为最大值(0x7),我能够获得所需的行为,即使每个数据包传输之间的延迟为0s(零),也不会丢失任何数据包。你知道吗

也许,正如tomlogic所说,如果我在更快的PC上运行我的代码,我相信,我会得到我的包。你知道吗

当我做这个测试时,我会把结果贴在这里。你知道吗

谢谢。你知道吗

启用硬件流控制并将波特率从9600更改为115200。您必须为新的波特率更新XBee模块配置,但您将有机会使数据包通过。你知道吗

我想你的意思是说你每10毫秒发送一次数据包,而不是每10秒发送一次。以10毫秒/包的速度,你的速度是100包/秒。9600波特每秒仅约960个字符,而且您的数据包肯定大于9个字符,而且API开销很大。你知道吗

相关问题 更多 >

    热门问题