def connect(self): """ Connects the user interface to the CAN-D hardware device. Returns: boolean -- True if successfully connected; False, otherwise. """ if self.candbus is not None: self.candbus.start_usb_polling() self.connected = True else: try: if self.isdemo: self.candbus = CannedBus(log_path=self.trace_location) elif self.isloopback: self.candbus = LoopbackBus() else: self.candbus = CanDBus() self.connected = True except NoBackendError: print("No hardware") return self.connected
class MainController(object): """ The main controller for the whole application, contains a singleton of all the other controllers in the application. :param `isdemo`: a boolean, True if the program is launched for the demo; False, otherwise. :param `trace_location`: a path to the trace file containing the data to be shown in the demo. :param `tracecontroller`: A TraceController holding the data for the Trace tab. :param `rxtxcontroller`: A RxTxController holding the data for the Receive/Transmit tab. """ def __init__(self, isdemo, isloopback, trace_location, tracecontroller=None, rxtxcontroller=None): self.isdemo = isdemo self.isloopback = isloopback self.trace_location = trace_location if tracecontroller is not None: self.tracecontroller = tracecontroller else: self.tracecontroller = TraceController() if rxtxcontroller is not None: self.rxtxcontroller = rxtxcontroller else: self.rxtxcontroller = RxtxController(self) self.polling = False self.connected = False self.candbus = None self.logging = False self.dbc = None self.open_file_path = "" def connect(self): """ Connects the user interface to the CAN-D hardware device. Returns: boolean -- True if successfully connected; False, otherwise. """ if self.candbus is not None: self.candbus.start_usb_polling() self.connected = True else: try: if self.isdemo: self.candbus = CannedBus(log_path=self.trace_location) elif self.isloopback: self.candbus = LoopbackBus() else: self.candbus = CanDBus() self.connected = True except NoBackendError: print("No hardware") return self.connected def disconnect(self): """ Disconnects the user interface from the CAN-D hardware device. Returns: boolean -- True if successfully disconnected; False, otherwise. """ self.connected = False self.candbus.stop_usb_polling() self.candbus.stop_log() self.polling = False self.logging = False self.rxtxcontroller.kill_all_tasks() return self.connected def startLog(self): """ Start logging data from the CAN-D device to the SD card. Returns: boolean -- True if the CAN-D device has started logging. """ if self.candbus is not None: self.logging = True self.candbus.start_log() return self.logging def stopLog(self): """ Stop logging data from the CAN-D device to the SD card. Returns: boolean -- True if the CAN-D device has stopped logging. """ if self.candbus is not None: self.logging = False self.candbus.stop_log() return self.logging def markLog(self): """ Marks the log in the logfile that happened in the same time instance this function is called. Returns: boolean -- True if marking the log is successful. False, otherwise. """ if self.candbus is not None: self.candbus.mark_log() return True return False
from candy_connector.CanDBus import CanDBus import candy_connector.proto.can_d_pb2 as pb from candy_connector.parsers import parse_line import logging from can import Message bus = CanDBus() with open("./data/can_trace_256.log.test", "r") as log_file: print("Starting Loopback test...") print("Sending Messages") for log_line in log_file.readlines(): frame_id, payload = parse_line(log_line) tx_msg = Message() tx_msg.arbitration_id = frame_id tx_msg.dlc = len(payload) tx_msg.data = bytes(payload) print(f"Send {tx_msg}") bus.send(tx_msg) rx_msg = bus.recv() assert ( rx_msg.arbitration_id == tx_msg.arbitration_id ), f"Id did not match. Sent: {tx_msg.arbitration_id}, got:{rx_msg.arbitration_id}" assert ( rx_msg.data == tx_msg.data ), f"Data did not match. Sent: {tx_msg.data}, got:{rx_msg.data}" print("All messages sent.") print("Done Loopback test") bus.stop_usb_polling()
import usb.core import usb.util from time import sleep from candy_connector.CanDBus import CanDBus bus = CanDBus() bus.start_log() bus.stop_usb_polling() while True: print("Sleep for 10s... ...zzz.") sleep(10) print("Send Stop Command.") bus.stop_log()
def send_start_log(): bus = CanDBus() bus.start_log() bus.stop_usb_polling()
def send_mark_log(): bus = CanDBus() bus.mark_log() bus.stop_usb_polling()
from candy_connector.CanDBus import CanDBus import candy_connector.proto.can_d_pb2 as pb print("Start") bus = CanDBus() print("Created Bus") while True: data = bus.recv() print(f"Read message: {data}")