如何在同一个Python程序中侦听HTTP请求和串行通道?

如何在同一个Python程序中侦听HTTP请求和串行通道?,python,webserver,serial-port,arduino,xml-rpc,Python,Webserver,Serial Port,Arduino,Xml Rpc,我有一个Arduino板连接到电脑的串行端口。主板向PC发送数据,但也可以通过同一通道接收订单。但是,发送的数据和订单之间没有关系或顺序 在PC中,我创建了一个监听串行端口的守护进程。为了向Arduino发送数据,我考虑在守护进程中建立一个XML RPC服务器。问题是我不知道如何在同一个程序中侦听串行端口和处理XML RPC请求 起初,我的脑海里有这样的想法: while true if there is incoming data in the serial channel

我有一个Arduino板连接到电脑的串行端口。主板向PC发送数据,但也可以通过同一通道接收订单。但是,发送的数据和订单之间没有关系或顺序

在PC中,我创建了一个监听串行端口的守护进程。为了向Arduino发送数据,我考虑在守护进程中建立一个XML RPC服务器。问题是我不知道如何在同一个程序中侦听串行端口和处理XML RPC请求

起初,我的脑海里有这样的想法:

while true
    if there is incoming data in the serial channel
       blah blah

    if there are HTTP requests
        start attending the XML RPC request
我一直在关注的服务器(SimpleXMLRPCServer,web.py)一直在监听和处理请求。我想告诉我的XML RPC服务器“等等,如果没有请求,我们将检查串行通道”


我如何才能做到这一点或类似的事情?或者我应该改变我的设计?如果在另一个进程中使用XML RPC服务器,我想我需要某种进程间通信,这就是我开始使用XML RPC的原因。

您可以使用所谓的带线程的程序。它允许并行执行多个操作

如果您使用Python,这里将提供更多信息:

然而,螺纹可能很难绘制和正确使用。有几个关于Python线程的教程,请使用搜索引擎查看

最重要的想法是这样做:

import threading

def processCommands():
    while True:
        #wait and process commands

def readSerial():
    while True:
        #read serial

serialThread = threading.Thread(target=readSerial)
commandThread = threading.Thread(target=processCommands)
serialThread.start()
commandThread.start()

#Wait for the command thread to exit,
#otherwise the programs would immediately exit
commandThread.join()
from twisted.web import server, resource
from twisted.internet import protocol, reactor
from twisted.internet.serialport import SerialPort

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class HttpResource(resource.Resource):
    isLeaf = True

    def render_GET(self, request):
        request.setHeader("content-type", "text/plain")
        return "Hello there\n"

reactor.listenTCP(8080, server.Site(HttpResource()))
SerialPort(Echo(), <your serial port>, reactor, baudrate=9600)

reactor.run()
另一种(可能更好)方法是将应用程序迁移到异步。例如,您可以使用twisted框架(如,)在两个单独的协议处理程序中同时异步处理和串行通信

诸如此类:

import threading

def processCommands():
    while True:
        #wait and process commands

def readSerial():
    while True:
        #read serial

serialThread = threading.Thread(target=readSerial)
commandThread = threading.Thread(target=processCommands)
serialThread.start()
commandThread.start()

#Wait for the command thread to exit,
#otherwise the programs would immediately exit
commandThread.join()
from twisted.web import server, resource
from twisted.internet import protocol, reactor
from twisted.internet.serialport import SerialPort

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class HttpResource(resource.Resource):
    isLeaf = True

    def render_GET(self, request):
        request.setHeader("content-type", "text/plain")
        return "Hello there\n"

reactor.listenTCP(8080, server.Site(HttpResource()))
SerialPort(Echo(), <your serial port>, reactor, baudrate=9600)

reactor.run()
从twisted.web导入服务器,资源
来自twisted.internet导入协议,reactor
从twisted.internet.serialport导入serialport
类Echo(protocol.protocol):
接收到def数据(自身、数据):
self.transport.write(数据)
类HttpResource(resource.resource):
isLeaf=True
def render_GET(自我,请求):
request.setHeader(“内容类型”、“文本/普通”)
返回“你好\n”
reactor.listenTCP(8080,server.Site(HttpResource()))
串行端口(Echo(),反应器,波特率=9600)
反应堆运行()

+1对于Twisted,这显然是为此目的创建的框架。我使用这种方法是因为它更通用。下次我将给twisted一个镜头,因为我听到了很多关于它的好东西。如果您感兴趣,代码如下: