added modbus communication and can-modbus coupling through pubsub
This commit is contained in:
parent
8481b607c5
commit
fb3350cebd
1 changed files with 25 additions and 3 deletions
28
c2m.py
28
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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue