【嵌入式开发基础】D-Bus 基于Python的实例讲解

本篇文章主要是基于dbus-python中提供的例程来简单讲解下D-Bus的通信过程

例程的地址和相关资料
运行环境确认
  • Ubuntu16.04 及以上版本
  • dbus-daemon版本确认 1.12.2 及以上版本
➜  dbus-python git:(master) dbus-daemon --version
D-Bus Message Bus Daemon 1.12.2
Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • 确认dbus服务是否起来, 打开终端执行,service --status-all, 出现 [ + ] dbus代码dbus服务运行正常
  • python版本: python2.7, 这里需要注意对于python2.x和python3.x 是有较大差异的,dbus-python主要是基于python2.7编写的,如果希望使用python3.x 请修改下代码,针对example主要是修正print的使用方式.
➜  dbus-python git:(master) service --status-all 
 [ + ]  acpid
 [ - ]  alsa-utils
 [ - ]  anacron
 [ + ]  apparmor
 [ + ]  apport
 [ + ]  avahi-daemon
 [ + ]  binfmt-support
 [ + ]  bluetooth
 [ - ]  console-setup.sh
 [ + ]  cron
 [ + ]  cups
 [ + ]  cups-browsed
 [ + ]  dbus
 [ - ]  dns-clean
实例运行

首先需要下载dbus-python的源码,然后打开终端, 进度源码文件的顶层目录,
运行python2.7 example-service.py

➜  examples git:(master) python2.7 example-service.py
Running example service.
Usage:
python example-service.py &
python example-client.py
python example-async-client.py
python example-client.py --exit-service

然后打开另一个终端,运行: python2.7 example-client.py

➜  examples git:(master) python2.7 example-client.py 
dbus.Array([dbus.String(u'Hello'), dbus.String(u' from example-service.py'), dbus.String(u'with unique name'), dbus.String(u':1.76')], signature=dbus.Signature('s'))
dbus.Struct((dbus.String(u'Hello Tuple'), dbus.String(u' from example-service.py')), signature=None)
dbus.Dictionary({dbus.String(u'second'): dbus.String(u' from example-service.py'), dbus.String(u'first'): dbus.String(u'Hello Dict')}, signature=dbus.Signature('ss'))
com.example.DemoException: The RaiseException method does what you might expect
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/SomeObject">
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg direction="out" type="s" />
    </method>
  </interface>
  <interface name="com.example.SampleInterface">
    <method name="GetDict">
      <arg direction="out" type="a{ss}" />
    </method>
    <method name="Exit">
    </method>
    <method name="RaiseException">
    </method>
    <method name="GetTuple">
      <arg direction="out" type="(ss)" />
    </method>
    <method name="HelloWorld">
      <arg direction="in"  type="s" name="hello_message" />
      <arg direction="out" type="as" />
    </method>
  </interface>
</node>

最后在运行了python2.7 example-service.py的终端接收到了client通过dbus 发送的数据->
“Hello from example-client.py!”

➜  examples git:(master) python2.7 example-service.py
Running example service.
Usage:
python example-service.py &
python example-client.py
python example-async-client.py
python example-client.py --exit-service

Hello from example-client.py!

以上步骤简单演示了下基于dbus-python下的example, example-service.py和example-client.py 演示了基于dbus的进程间通信

总结

基于dbus-python提供的例程和dbus-python的官方文档,帮助我们更好地理解的了d-bus的工作原理和使用方式.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值