From fb3350cebd4d621f6f21e9c47d4bd8dfebb05a9b Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Mon, 10 Aug 2020 17:29:33 -0700 Subject: [PATCH] added modbus communication and can-modbus coupling through pubsub --- c2m.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/c2m.py b/c2m.py index fb68de2..3f9d8dd 100644 --- a/c2m.py +++ b/c2m.py @@ -1,18 +1,36 @@ import can import cantools +from pubsub import pub + from WebRelay import WebRelay + +class BmsListener (can.Listener): + def __init__(self, parent): + self.parent = parent + + def on_message_received(self, msg): + relay_id = msg.arbitration_id + relay_state = msg.data + print (relay_id, relay_state) + if relay_id < 10: + if relay_state > 0: + pub.sendMessage("turn_relay_on", relay_id=relay_id) + else: + pub.sendMessage("turn_relay_off",relay_id=relay_id) + + class Model: def __init__(self): self.bus = None self.notifier = None - self.listen_bms = can.Printer() - #self.listen_bms = BmsListener(self) + self.printer = can.Printer() + self.listen_bms = BmsListener(self) def start_can(self): self.bus = can.Bus() - self.notifier = can.Notifier(self.bus, [self.listen_bms]) + self.notifier = can.Notifier(self.bus, [self.printer, self.listen_bms]) def stop_can(self): self.notifier.stop() @@ -32,6 +50,10 @@ if __name__ == "__main__": modbus = WebRelay() modbus.apply_network_configs(nc_dict) + modbus.connect_slave() + + pub.subscribe(modbus.turn_relay_on, "turn_relay_on") + pub.subscribe(modbus.turn_relay_off, "turn_relay_off") canbus = Model()