diff --git a/README.md b/README.md index 4d35172..4c539c3 100644 --- a/README.md +++ b/README.md @@ -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``` diff --git a/examples/mbus-test-1.py b/examples/mbus-test-1.py index 7fd3eee..d207d86 100755 --- a/examples/mbus-test-1.py +++ b/examples/mbus-test-1.py @@ -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)) diff --git a/examples/mbus-test-secondary-send-text.py b/examples/mbus-test-secondary-send-text.py new file mode 100755 index 0000000..dee67fe --- /dev/null +++ b/examples/mbus-test-secondary-send-text.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# ------------------------------------------------------------------------------ +# Copyright (C) 2012, Robert Johansson , 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() diff --git a/mbus/MBus.py b/mbus/MBus.py index d707353..d2b867d 100644 --- a/mbus/MBus.py +++ b/mbus/MBus.py @@ -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") + + diff --git a/mbus/MBusLowLevel.py b/mbus/MBusLowLevel.py index 735372f..cf3d1d8 100644 --- a/mbus/MBusLowLevel.py +++ b/mbus/MBusLowLevel.py @@ -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]