From 599a72b362fd287b6a19a1f5c7cb34c0e3bf4f90 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:14:05 +0200 Subject: [PATCH 01/17] Update MBusLowLevel.py Added ref to new send custom text func --- mbus/MBusLowLevel.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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] From 4bcca531268d4f6b6c460ab517408f831b188733 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:51:59 +0200 Subject: [PATCH 02/17] Try out the secondary addressing You need to set the secondary address inside the .py for this test --- examples/mbus-test-secondary.py | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/mbus-test-secondary.py diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary.py new file mode 100644 index 0000000..cacb36d --- /dev/null +++ b/examples/mbus-test-secondary.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# ------------------------------------------------------------------------------ +# Copyright (C) 2012, Robert Johansson , Raditex Control AB +# All rights reserved. +# ------------------------------------------------------------------------------ + +""" +mbus test: send a request frame and receive and parse the reply +""" + +from mbus.MBus import MBus + +debug = True +#address = 0 +address = 0xFD #secondary +ret = mbus.select_secondary_address("24134746FFFFFFFF") + + +#mbus = MBus(host="mbus-gw1", port=8888) +mbus = MBus(device="/dev/ttyUSB0", libpath="/home/ph/git/python-mbus/examples/libmbus.so") + +if debug: + print("mbus = " + str(mbus)) + +mbus.connect() + +if debug: + print("mbus = " + str(mbus)) + +mbus.serial_set_baudrate(2400) + +#res = mbus.send_ping_frame(0xFD, 1) +#print(res) +#res = mbus.send_ping_frame(0, 1) +#print(res) +# fairly sure these pings not needed, try + +mbus.send_request_frame(address) + +reply = mbus.recv_frame() + +if debug: + print("reply =", reply) + +reply_data = mbus.frame_data_parse(reply) + +if debug: + print("reply_data =", reply_data) + +xml_buff = mbus.frame_data_xml(reply_data) + +print("xml_buff =", xml_buff) + +mbus.frame_data_free(reply_data) + +mbus.disconnect() \ No newline at end of file From 9715d93489495629ed748dcafd0549309961ea4e Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:54:11 +0200 Subject: [PATCH 03/17] Update mbus-test-secondary.py --- examples/mbus-test-secondary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary.py index cacb36d..8519303 100644 --- a/examples/mbus-test-secondary.py +++ b/examples/mbus-test-secondary.py @@ -13,7 +13,6 @@ debug = True #address = 0 address = 0xFD #secondary -ret = mbus.select_secondary_address("24134746FFFFFFFF") #mbus = MBus(host="mbus-gw1", port=8888) @@ -28,6 +27,7 @@ print("mbus = " + str(mbus)) mbus.serial_set_baudrate(2400) +ret = mbus.select_secondary_address("24134746FFFFFFFF") #res = mbus.send_ping_frame(0xFD, 1) #print(res) @@ -53,4 +53,4 @@ mbus.frame_data_free(reply_data) -mbus.disconnect() \ No newline at end of file +mbus.disconnect() From 8c5660b64d56345074d6c7af4ab580955e189a17 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:57:01 +0200 Subject: [PATCH 04/17] Update mbus-test-secondary.py --- examples/mbus-test-secondary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary.py index 8519303..3d35d4e 100644 --- a/examples/mbus-test-secondary.py +++ b/examples/mbus-test-secondary.py @@ -16,7 +16,7 @@ #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/ttyUSB0", libpath="/home/hksaato/python-mbus/examples/libmbus.so") if debug: print("mbus = " + str(mbus)) From 5ba0f32053f33a7cd91d427182700aa95c60782f Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:58:13 +0200 Subject: [PATCH 05/17] Update mbus-test-secondary.py --- examples/mbus-test-secondary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary.py index 3d35d4e..9a52e65 100644 --- a/examples/mbus-test-secondary.py +++ b/examples/mbus-test-secondary.py @@ -16,7 +16,7 @@ #mbus = MBus(host="mbus-gw1", port=8888) -mbus = MBus(device="/dev/ttyUSB0", libpath="/home/hksaato/python-mbus/examples/libmbus.so") +mbus = MBus(device="/dev/ttyUSB0", libpath="/usr/local/lib/libmbus.so") if debug: print("mbus = " + str(mbus)) From 40c342ced897254eee1bf97bf9a1c1cabd0e3aeb Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:59:29 +0200 Subject: [PATCH 06/17] Update mbus-test-secondary.py Finally working, I guess I should have better git management --- examples/mbus-test-secondary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary.py index 9a52e65..59a8d49 100644 --- a/examples/mbus-test-secondary.py +++ b/examples/mbus-test-secondary.py @@ -16,7 +16,7 @@ #mbus = MBus(host="mbus-gw1", port=8888) -mbus = MBus(device="/dev/ttyUSB0", libpath="/usr/local/lib/libmbus.so") +mbus = MBus(device="/dev/ttyUSB1", libpath="/usr/local/lib/libmbus.so") if debug: print("mbus = " + str(mbus)) From 10df79d295aaa1d988fb0b9d93d8382bac06d3f1 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:11:18 +0200 Subject: [PATCH 07/17] Update MBus.py add send_custom_text, probably not important but I will use it a lot so I want it there with the rest of them --- mbus/MBus.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mbus/MBus.py b/mbus/MBus.py index d707353..668cb68 100644 --- a/mbus/MBus.py +++ b/mbus/MBus.py @@ -221,3 +221,14 @@ 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 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") + + From c20b287ebab8248471337e1ffaab075e6d575d35 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:24:16 +0200 Subject: [PATCH 08/17] Update MBus.py Byte conversion added, now you can input normal string to send_custom_text --- mbus/MBus.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mbus/MBus.py b/mbus/MBus.py index 668cb68..d2b867d 100644 --- a/mbus/MBus.py +++ b/mbus/MBus.py @@ -225,6 +225,9 @@ 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") From eb07493c58ba9b3522a694aff6ee6baf5cbc898e Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:28:53 +0200 Subject: [PATCH 09/17] Update and rename mbus-test-secondary.py to mbus-test-secondary-send-text.py Test for new send custom text function --- ...-secondary.py => mbus-test-secondary-send-text.py} | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) rename examples/{mbus-test-secondary.py => mbus-test-secondary-send-text.py} (63%) diff --git a/examples/mbus-test-secondary.py b/examples/mbus-test-secondary-send-text.py similarity index 63% rename from examples/mbus-test-secondary.py rename to examples/mbus-test-secondary-send-text.py index 59a8d49..78931e2 100644 --- a/examples/mbus-test-secondary.py +++ b/examples/mbus-test-secondary-send-text.py @@ -5,7 +5,12 @@ # ------------------------------------------------------------------------------ """ -mbus test: send a request frame and receive and parse the reply +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 @@ -34,8 +39,8 @@ #res = mbus.send_ping_frame(0, 1) #print(res) # fairly sure these pings not needed, try - -mbus.send_request_frame(address) +text = "Test" +mbus.send_custom_text(address, text) reply = mbus.recv_frame() From 3c89d16227b19831d243e1dfe94bc817c586f45d Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:35:44 +0200 Subject: [PATCH 10/17] Update mbus-test-secondary-send-text.py Remove the parse xml stuff because it doesn't work on an E5 reply --- examples/mbus-test-secondary-send-text.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/examples/mbus-test-secondary-send-text.py b/examples/mbus-test-secondary-send-text.py index 78931e2..8c364ca 100644 --- a/examples/mbus-test-secondary-send-text.py +++ b/examples/mbus-test-secondary-send-text.py @@ -38,7 +38,7 @@ #print(res) #res = mbus.send_ping_frame(0, 1) #print(res) -# fairly sure these pings not needed, try +# fairly sure these pings not needed text = "Test" mbus.send_custom_text(address, text) @@ -47,15 +47,6 @@ if debug: print("reply =", reply) -reply_data = mbus.frame_data_parse(reply) - -if debug: - print("reply_data =", reply_data) - -xml_buff = mbus.frame_data_xml(reply_data) - -print("xml_buff =", xml_buff) - -mbus.frame_data_free(reply_data) +#in reality I should get E5 hex as reply but I don't know how to receive it in python yet. It'd be nice to print it out to know that the device said yes mbus.disconnect() From 6d0a3ff7a451be222a7c5cc1381cfe00b143c951 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:41:05 +0200 Subject: [PATCH 11/17] Update mbus-test-secondary-send-text.py Found a way to except a wrong address / device offline --- examples/mbus-test-secondary-send-text.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/mbus-test-secondary-send-text.py b/examples/mbus-test-secondary-send-text.py index 8c364ca..ed9f39c 100644 --- a/examples/mbus-test-secondary-send-text.py +++ b/examples/mbus-test-secondary-send-text.py @@ -42,7 +42,15 @@ text = "Test" mbus.send_custom_text(address, text) -reply = mbus.recv_frame() +try: + reply = mbus.recv_frame() +except Exception as e: + if "libmbus.mbus_recv_frame failed" in str(e): + print("Device did not respond.") + # Handle the specific case of the device not responding here + else: + print(f"Unexpected error: {e}") + # Handle other exceptions here if debug: print("reply =", reply) From 551ef572de7ae4f19fb268cd786e0e9a7329d40f Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:43:27 +0200 Subject: [PATCH 12/17] Update mbus-test-secondary-send-text.py One more fix to the reply --- examples/mbus-test-secondary-send-text.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/mbus-test-secondary-send-text.py b/examples/mbus-test-secondary-send-text.py index ed9f39c..fb70ab0 100644 --- a/examples/mbus-test-secondary-send-text.py +++ b/examples/mbus-test-secondary-send-text.py @@ -46,15 +46,11 @@ reply = mbus.recv_frame() except Exception as e: if "libmbus.mbus_recv_frame failed" in str(e): - print("Device did not respond.") - # Handle the specific case of the device not responding here + reply = "Device did not respond." else: - print(f"Unexpected error: {e}") - # Handle other exceptions here + reply = f"Unexpected error: {e}" if debug: print("reply =", reply) -#in reality I should get E5 hex as reply but I don't know how to receive it in python yet. It'd be nice to print it out to know that the device said yes - mbus.disconnect() From 27101ad47db1cbb4d9c6038b664df2f38026a625 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:01:13 +0200 Subject: [PATCH 13/17] Update README.md Info about send_custom_text --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4d35172..6e7816a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ python-mbus =========== +Added send_custom_text and tests for that and secondary addressing. Needs my fork of libmbus to work. I used sensenode instead of master because it seems to have addressed some issues with the original, and the original is abandoned. + Python wrapper for [libmbus](http://www.rscada.se/libmbus) ([source](https://github.com/rscada/libmbus)) * installation: run ```python setup.py install``` From e6e240e7cba3fe0e2af40313239360f761ef5810 Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:01:26 +0200 Subject: [PATCH 14/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e7816a..6dba980 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ python-mbus =========== -Added send_custom_text and tests for that and secondary addressing. Needs my fork of libmbus to work. I used sensenode instead of master because it seems to have addressed some issues with the original, and the original is abandoned. +Added send_custom_text and tests for that and secondary addressing. Needs my fork of libmbus to work. I used sensenode instead of master because it seems to have addressed some issues with the original, and the original is abandoned. // Jouzer Python wrapper for [libmbus](http://www.rscada.se/libmbus) ([source](https://github.com/rscada/libmbus)) From 43008aae90861eae61cb23fd4d9f1d18693ff00e Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:01:59 +0200 Subject: [PATCH 15/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dba980..62787c7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ python-mbus =========== -Added send_custom_text and tests for that and secondary addressing. Needs my fork of libmbus to work. I used sensenode instead of master because it seems to have addressed some issues with the original, and the original is abandoned. // Jouzer +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 Python wrapper for [libmbus](http://www.rscada.se/libmbus) ([source](https://github.com/rscada/libmbus)) From 10907a8fdb0cea15e247f6a860ab7c59af47222e Mon Sep 17 00:00:00 2001 From: Jouzer <32982491+Jouzer@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:03:03 +0200 Subject: [PATCH 16/17] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 62787c7..4c539c3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ 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``` From f06f67e29a69bd2c89e9ef686ab125c5ecc620fc Mon Sep 17 00:00:00 2001 From: Jouzer Date: Thu, 7 Mar 2024 13:13:25 +0000 Subject: [PATCH 17/17] fixed example memory leak (I hope) --- examples/mbus-test-1.py | 4 ++-- examples/mbus-test-secondary-send-text.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) mode change 100644 => 100755 examples/mbus-test-secondary-send-text.py 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 old mode 100644 new mode 100755 index fb70ab0..dee67fe --- a/examples/mbus-test-secondary-send-text.py +++ b/examples/mbus-test-secondary-send-text.py @@ -21,7 +21,7 @@ #mbus = MBus(host="mbus-gw1", port=8888) -mbus = MBus(device="/dev/ttyUSB1", libpath="/usr/local/lib/libmbus.so") +mbus = MBus(device="/dev/ttyUSB0", libpath="/usr/local/lib/libmbus.so") if debug: print("mbus = " + str(mbus)) @@ -32,18 +32,19 @@ print("mbus = " + str(mbus)) mbus.serial_set_baudrate(2400) -ret = mbus.select_secondary_address("24134746FFFFFFFF") +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 = "Test" +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."