如何在Python中从其他类调用类方法?

swmcdonnell

我有一个Raspberry Pi:

  • 通过Xbee到Arduino的串行接口
  • 网页的WebSocket服务器界面(html / javascript)

我写了一个Python脚本来协调通信(我是Python的新手)。它包含:

  • 串行接口类
  • WebSocket接口类

我正在尝试在网页和Arduino之间中继数据。具体来说,我想打电话从串行类WebSocket的类“送”的方法,并从网页套接字类所谓的“send”方法在串行类

我花了很多时间阅读和研究,但是我无法弄清楚。这是我现在的代码。我得到的具体错误是“ sendSocket()恰好接受2个参数(给定3个)。

# server.py

DEBUG = True;

import serial
import time
import sys
import json
import os
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from twisted.internet import reactor
from twisted.internet import task

# I think I need these for the instances?
global socket, serial

# ---------------------------------
# WebSocket Interface
# ---------------------------------
class HaSocketProtocol(WebSocketServerProtocol):
    # I'm not sure of any other way to incorporate the instance
    global serial

    def __init__(self):
        pass

    def onConnect(self, request):
        debug("Client connected")

    def onOpen(self):
        debug("WebSocket Server Open")

    def onMessage(self, payload, isBinary):
        if not isBinary:
            payload = payload.decode('utf8')
            debug("Incoming socket message: {} -- Writing as-is to xbee".format(payload))
            # Here is where I'm trying to send the data I received out the serial port using the SerialInterface class method
            self.sendSerial(serial, payload)

    def onClose(self, wasClean, code, reason):
        debug("WebSocket connection closed - {}".format(reason))

    # Socket server sending over socket    
    def send(self, message):
        self.sendMessage(message)

    # Here is where I'm trying to link them 
    @classmethod
    def sendSerial(SerialInterface, message):
        SerialInterface.send(message)

# ---------------------------------
# Serial Interface
# ---------------------------------
class SerialInterface:
    xbee = None
    # trying to iunclude the instance
    global socket

    def __init__(self, devname, baud, to):
        self.xbee = serial.Serial(devname, baud, timeout=to)
        debug(self.xbee)

    def check(self):
        incoming = None
        # will timeout if there's no data to be read
        incoming = self.xbee.readline().strip()
        if incoming != None and incoming != "":
            self.handle(incoming)

    # serial send over serial
    def send(self, message):
        self.xbee.write(message)
        self.xbee.write("\n")

    # here is where i'm trying to link them    
    @classmethod
    def sendSocket(HaSocketProtocol, message):
        HaSocketProtocol.send(message)        

    def handle(self, incoming):
        debug("Incoming serial: {}".format(incoming))

        # ...
        # my logic is in here that leads to several instances where I
        # create a command string that I send over the serial port
        # ...

        socketMessage = "ArduinoCommand"
        self.sendSocket(socket, socketMessage)

# ------------------------------
# main
# ------------------------------
def main():
    global socket, serial

    serial = SerialInterface('/dev/ttyUSB0', 9600, 1)
    socket = HaSocketProtocol()

    factory = WebSocketServerFactory("ws://localhost:9000", debug=False)
    factory.protocol = socket

    # check Serial port for data every 1 second
    ser = task.LoopingCall(serial.check)
    ser.start(1.0)

    reactor.listenTCP(9000, factory)
    reactor.run()

def debug(msg):
    if DEBUG:
        print "{}\n".format(msg)    


main()
swmcdonnell

事实证明,这个问题实际上是有关如何实现Autobahn框架的,而不是有关如何使用Python编程的问题。对我来说,用JavaScript重写并将其放到Node.js上更容易。运行得很好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Python的子类中调用父类的方法?

如何在调用其他方法的基类上创建Java方法?

如何模拟调用同一类中存在的其他方法的方法

如何重用其他类中的方法

在单元测试中调用其他类方法

如何在.html中调用其他类的静态方法(不在.ts中)?

如何在Flutter(DART)中访问其他类的方法?

从python中的其他类调用静态方法

如何从其他类的类中调用方法?无法读取属性“测试” JS

如何在python中调用父类方法?

在python中从__new __()调用其他类方法

如何从静态类中调用其他类中的方法

在Python 2中如何在不继承的情况下重用其他类的方法?

如何在其他类中从MainActivity调用(和使用)onOptionsItemSelected

如何用out参数调用其他类方法

如何从其他Objective-C类调用方法?

从MATLAB中的其他脚本调用类的方法?

从Android中的其他类调用主类方法

如何在python中调用类的方法?

如何在python中调用类方法

如何在异步方法中使用其他类中的其他函数

如何从其他类中调用变量和方法?

如何在JAVA的同一包中调用方法或访问其他类中的字段

如何从Android中的其他类调用动画方法?

Javascript调用其他函数中的类方法

Python 类调用其他方法

调用类实例(类中的函数)上的方法,该方法调用该类中的其他函数

如何从其他类调用方法

创建新的类实例时,如何在 Python 中的任何其他类方法之前自动运行类的一个方法