def pack_command(voltage: float, current: float, enable: bool) -> Message: msg = Message() if voltage > 0: if current > 0: v = int(voltage * 10) i = int(voltage * 10) msg.data = bytearray(5) msg.data[0] = (v >> 8) & 0xff msg.data[1] = (v) & 0xff msg.data[2] = (i >> 8) & 0xff msg.data[3] = (i) & 0xff if enable: msg.data[4] = 1 else: msg.data[4] = 0 #No idea what these are pf=6 r=0 dp=0 priority=6 msg.id = pack_elcon_id(priority, r, dp, pf, elcon_charger_id, elcon_manager_id) msg.is_extended_id = True msg.dlc = 5 return msg raise ValueError("positive voltage and current")
def openFile(self): """ Opens a CAN-D Trace file and loads its data. """ self.controller.open_file_path = QFileDialog.getOpenFileName( self, 'Open File', './', filter= "CAN-D Trace Files(*.cand);;All Files(*.*);;Text Files(*.txt)")[0] if self.controller.open_file_path is not '': try: file = open(self.controller.open_file_path, "r") lines = file.readlines() for l in lines: frame_id, payload = parse_line(l) msg = Message() msg.arbitration_id = frame_id msg.dlc = len(payload) msg.data = bytes(payload) self.insertTrace(msg) except: popup = QtWidgets.QMessageBox.critical( self, "Error", "Failed to open CAN-D trace file. \n\nFile must be of type .cand or .txt" ) return
def transmitMessage(self, transmit_message): """ Transmits the message to the CAN device and populates the transmit table. Arguments: transmit_message {[Message]} -- A message object containing the CAN data. """ # Build the Message object message = Message() message.arbitration_id = int(transmit_message.can_id, 16) message.dlc = transmit_message.dlc try: message.data = bytes.fromhex(transmit_message.data) except: return "Data field is not in valid hexadecimal form." # If there is a task for a given can_id, cancel that task if transmit_message.can_id in self.tasks: self.tasks[transmit_message.can_id].cancel() loop = asyncio.get_event_loop() if transmit_message.cycle_time is "" or transmit_message.cycle_time is None: loop.create_task( self.transmit_and_append(message, transmit_message)) else: # Calculate the cycle time for transmitting messages in ms cycle_time = int(transmit_message.cycle_time) / 1000 # Create a task, which is re-scheduled every cycle_time ms self.tasks[transmit_message.can_id] = loop.create_task( do_stuff_every_x_seconds( cycle_time, lambda: self.transmit_and_append( message, transmit_message)))
async def _send_data(self, log_path, message_sleep_time=0.00): with open(log_path, "r") as log_file: for log_line in log_file.readlines(): frame_id, payload = parse_line(log_line) msg = Message() msg.arbitration_id = frame_id msg.dlc = len(payload) msg.data = bytes(payload) self.data_queue.put_nowait(msg) await asyncio.sleep(message_sleep_time)
def test_send(channel): bus = ZL_motor_control(channel, 115200, 115200, {1}) bus.open_bus() bus.bus.ser.setDTR(False) msg = Message(extended_id=False) msg.arbitration_id = 0x01 msg.dlc = 8 msg.data = [0x00, 0xfa, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0f] bus.send(msg) bus.logger.debug("send finished!") bus.close_bus() bus.logger.debug("\n\n")
def test_recv(channel, id_list): bus = ZL_motor_control(channel, 115200, 115200, id_list) bus.open_bus() bus.bus.ser.setDTR(False) msg = Message(extended_id=False) msg.arbitration_id = 0x01 msg.dlc = 8 msg.data = [0x00, 0xfa, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0f] bus.send(msg) recv_msg = bus.recv() bus.logger.debug("test_recv收到的消息是") bus.logger.debug(recv_msg) bus.close_bus() bus.logger.debug("\n")
def pack_command(self, pkt_dest: int, voltage: float, current: float, enable: bool) -> Message: """ Pack a command to the charger to set its output voltage and current and enable flag. Alternately, pack the charger's current output and state to the rest of the world. The message is sent from the class's ID, and uses the class's current flags (as packed by `pack_elcon_id` above). """ if voltage == 0: print("Not sending a message - voltage must be positive") return None msg = Message() v = int(voltage * 10) i = int(current * 10) msg.dlc = 5 # Send a message from us (source) to the destination msg.arbitration_id = self.pack_elcon_id(self.our_id, pkt_dest) flags = 0 if pkt_dest == elcon_charger_id: # Message to charger flags = 1 if enable else 0 else: # Message from charger to rest of world if self.hardware_failure: flags |= 0x01 if self.over_temperature: flags |= 0x02 if self.input_voltage: flags |= 0x04 if self.no_battery: flags |= 0x08 if self.timeout: flags |= 0x10 msg.data = pack(">HHB", v, i, flags) msg.is_extended_id = True return msg
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()