Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
python-mbus
===========

Added send_custom_text and tests for that and secondary addressing. Needs my fork of libmbus to work. I used sensenode fork instead of master because it seems to have addressed some issues with the original, and the original is abandoned. // Jouzer

The stuff below is the original, you install this as so and then you can find the tests from /examples/ , edit the .py with your dev and sec.addr etc.

Python wrapper for [libmbus](http://www.rscada.se/libmbus) ([source](https://github.com/rscada/libmbus))

* installation: run ```python setup.py install```
Expand Down
4 changes: 2 additions & 2 deletions examples/mbus-test-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from mbus.MBus import MBus

debug = True
address = 0
address = 1

#mbus = MBus(host="mbus-gw1", port=8888)
mbus = MBus(device="/dev/ttyUSB0", libpath="/home/ph/git/python-mbus/examples/libmbus.so")
mbus = MBus(device="/dev/ttyUSB1", libpath="/usr/local/lib/libmbus.so")

if debug:
print("mbus = " + str(mbus))
Expand Down
57 changes: 57 additions & 0 deletions examples/mbus-test-secondary-send-text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# ------------------------------------------------------------------------------
# Copyright (C) 2012, Robert Johansson <rob@raditex.nu>, Raditex Control AB
# All rights reserved.
# ------------------------------------------------------------------------------

"""
mbus test: send custom text to secondary address
Only works with jouzer/python-mbus fork, need libmbus and python-mbus. If you reinstall,
you probably need to sudo make clean, sudo make install in libmbus,
and then sudo python3 setup.py install again in python-mbus
You can check that the .so file has the new function mbus-send-custom-text with nm -D /path/to/libmbus.so , obviously change your dir there
Also change the select_secondary_address to your Elvaco CMa10 device and device to your rs232 device (or whatever you use)
"""

from mbus.MBus import MBus

debug = True
#address = 0
address = 0xFD #secondary


#mbus = MBus(host="mbus-gw1", port=8888)
mbus = MBus(device="/dev/ttyUSB0", libpath="/usr/local/lib/libmbus.so")

if debug:
print("mbus = " + str(mbus))

mbus.connect()

if debug:
print("mbus = " + str(mbus))

mbus.serial_set_baudrate(2400)
ret = mbus.select_secondary_address("24134667FFFFFFFF")

#res = mbus.send_ping_frame(0xFD, 1)
#print(res)
#res = mbus.send_ping_frame(0, 1)
#print(res)
# fairly sure these pings not needed
text = "PYTHONpython"
mbus.send_custom_text(address, text)

try:
reply = mbus.recv_frame()
mbus.frame_free(reply)
except Exception as e:
if "libmbus.mbus_recv_frame failed" in str(e):
reply = "Device did not respond."
else:
reply = f"Unexpected error: {e}"

if debug:
print("reply =", reply)

mbus.disconnect()
14 changes: 14 additions & 0 deletions mbus/MBus.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,17 @@ def send_ping_frame(self, address, purge_response):
else:
raise Exception("Handle object not configure")

def send_custom_text(self, address, text):
"""
Send custom text to Elvaco CMa10
"""
if not isinstance(text, bytes):
text = text.encode('utf-8')

if self.handle:
if self._libmbus.send_custom_text(self.handle, address, text) == -1:
raise Exception("libmbus.mbus_send_custom_text failed")
else:
raise Exception("Handle object not configure")


7 changes: 6 additions & 1 deletion mbus/MBusLowLevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ def __init__(self, libpath=None):
self.probe_secondary_address.argtypes = [mbus_handle_p, c_char_p,
c_char_p]
self.probe_secondary_address.restype = c_int


self.send_custom_text = lib.mbus_send_custom_text
self.send_custom_text.argtypes = [mbus_handle_p, c_int,
c_char_p]
self.send_custom_text.restype = c_int

self.read_slave = lib.mbus_read_slave
self.read_slave.argtypes = [mbus_handle_p, mbus_address_p,
mbus_frame_p]
Expand Down