我尝试通过XMLRPC服务器从我的计算机与Raspberry Pi的串行端口进行通信。我想知道写入串口工作但读取不起作用,read()函数显示超时。我用示波器验证了数据是通过串口发送和接收的。有什么提示可以解决这个问题吗?
XMLRCP服务器代码:
import socket
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import serial
if __name__ == '__main__':
ipaddress = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
port = 8000
server = SimpleXMLRPCServer((ipaddress, port), requestHandler=SimpleXMLRPCRequestHandler, allow_none=True)
server.register_introspection_functions()
server.register_instance(serial.Serial("/dev/serial0", 57600, timeout=1.0))
print("Server running on {}:{}".format(ipaddress, port))
server.serve_forever()
XMLRPC客户端代码:
from __future__ import print_function
import xmlrpclib
config = {
"ipaddress": "172.25.125.235",
"port": 8000
}
if __name__ == '__main__':
url = 'http://{ipaddress}:{port}'.format(**config)
print("Connecting to {}".format(url))
iface = xmlrpclib.ServerProxy(url, allow_none=True)
print(iface.system.listMethods())
print(iface.write("d1234" + chr(13)))
print(repr(iface.read()))
客户输出:
Connecting to http://172.25.125.235:8000
['applySettingsDict', 'apply_settings', 'cancel_read', 'cancel_write', 'close',
'fileno', 'flush', 'flushInput', 'flushOutput', 'getCD', 'getCTS', 'getDSR', 'ge
tRI', 'getSettingsDict', 'get_settings', 'inWaiting', 'iread_until', 'isOpen', '
isatty', 'next', 'nonblocking', 'open', 'read', 'read_all', 'read_until', 'reada
ble', 'readall', 'readinto', 'readline', 'readlines', 'reset_input_buffer', 'res
et_output_buffer', 'seek', 'seekable', 'sendBreak', 'send_break', 'setDTR', 'set
Port', 'setRTS', 'set_input_flow_control', 'set_output_flow_control', 'system.li
stMethods', 'system.methodHelp', 'system.methodSignature', 'tell', 'truncate', '
writable', 'write', 'writelines']
6
''