我目前有一个从串行设备获取提要的脚本。
feed.py
from serial import Serial
ser = Serial('COM27', 9600, timeout=5)
def scrape():
while True:
raw = ser.readline()
raw = raw.decode('utf-8')
if raw == "":
pass
else:
print(raw)
#print (raw.decode('utf-8'))
scrape()
我现在想做的是从其他 python 脚本访问该提要。 我确实尝试使用 SimpleXMLRPCServer 但无法获得输出
feed.py
from serial import Serial
from xmlrpc.server import SimpleXMLRPCServer
ser = Serial('COM27', 9600, timeout=5)
def scrape():
while True:
raw = ser.readline()
raw = raw.decode('utf-8')
if raw == "":
pass
else:
print(raw)
try:
server = SimpleXMLRPCServer(("localhost", 8000), allow_none=True)
server.register_function(scrape)
server.serve_forever()
except Exception as e:
print(e)
监听器.py
import xmlrpc.client
feed = xmlrpc.client.ServerProxy('http://localhost:8000')
print(feed.scrape())
我没有从监听器脚本中得到任何输出
最佳答案
当一个函数被注册时,预计该函数会返回一个信息,而不仅仅是打印它,所以这就是逻辑失败的原因。
在这种情况下,最好注册 Serial 对象:
feed.py
from serial import Serial
from xmlrpc.server import SimpleXMLRPCServer
ser = Serial("COM27", 9600, timeout=5)
try:
server = SimpleXMLRPCServer(("localhost", 8000), allow_none=True)
server.register_instance(ser)
server.serve_forever()
except Exception as e:
print(e)
listener.py
import xmlrpc.client
ser = xmlrpc.client.ServerProxy("http://localhost:8000")
while True:
raw = ser.readline()
if raw:
print(raw)
关于Python 使串行输出可供其他脚本访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447597/