def device_init(self, rx_handle): """ 生成一些操作对象: 实例化TX 实例化RX :return: """ # 获取配置文件中的配置信息 -s canAppName, channels, bitrate = self.get_params_from_config() # 获取配置文件中的配置信息 -e # 回调,实例化RX VectorBus,接收总线上的报文 -s bus_rx = VectorBus(channel=channels[1], app_name=canAppName, bitrate=bitrate) listeners = [ rx_handle # rx的回调 ] notifier = can.Notifier(bus_rx, listeners) # 回调,实例化RX VectorBus,接收总线上的报文 -e # 实例化TX VectorBus(需要重新实例化) -s bus_tx = VectorBus(channel=channels[0], app_name=canAppName, bitrate=bitrate) bus_tx.reset() # activate channel # 实例化TX VectorBus(需要重新实例化) -e return bus_tx, bus_rx, notifier
def __init__(self): isotp_params = { 'stmin': 0, # Will request the sender to wait 32ms between consecutive frame. 0-127ms or 100-900ns with values from 0xF1-0xF9 'blocksize': 0, # Request the sender to send 8 consecutives frames before sending a new flow control message 'wftmax': 0, # Number of wait frame allowed before triggering an error #'ll_data_length' : 8, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) 'll_data_length': 16, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) 'tx_padding': 0xCC, # Will pad all transmitted CAN messages with byte 0x00. None means no padding 'rx_flowcontrol_timeout': 1000, # Triggers a timeout if a flow control is awaited for more than 1000 milliseconds 'rx_consecutive_frame_timeout': 1000, # Triggers a timeout if a consecutive frame is awaited for more than 1000 milliseconds 'squash_stmin_requirement': False, # When sending, respect the stmin requirement of the receiver. If set to True, go as fast as possible. 'can_fd': True, 'tx_data_min_length': 8, 'max_frame_size': 6000 } self.exit_requested = False #self.bus = VectorBus(channel='0', bitrate=500000) self.bus = VectorBus(channel='0', bitrate=500000, fd=True) addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0x10, target_address=0xF1) self.stack = isotp.CanStack(self.bus, address=addr, params=isotp_params, error_handler=self.my_error_handler)
class ThreadedApp: def __init__(self): isotp_params = { 'stmin': 0, 'blocksize': 0, 'wftmax': 0, 'll_data_length': 64, 'tx_padding': 0xCC, 'rx_flowcontrol_timeout': 1000, 'rx_consecutive_frame_timeout': 1000, 'squash_stmin_requirement': False, 'can_fd': True, 'tx_data_min_length': 8, 'max_frame_size': 65535 } self.exit_requested = False #self.bus = VectorBus(channel='0', bitrate=500000) self.bus = VectorBus(channel='0', bitrate=500000, fd=True) addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0xF1, target_address=0x10) self.stack = isotp.CanStack(self.bus, address=addr, params=isotp_params, error_handler=self.my_error_handler) def start(self): self.exit_requested = False self.thread = threading.Thread(target=self.thread_task) self.thread.start() def stop(self): self.exit_requested = True if self.thread.isAlive(): self.thread.join() def send(self, msg): self.stack.send(msg) def my_error_handler(self, error): logging.warning('IsoTp error happened : %s - %s' % (error.__class__.__name__, str(error))) def thread_task(self): while self.exit_requested == False: self.stack.process() # Non-blocking #time.sleep(self.stack.sleep_time()) # Variable sleep time based on state machine state time.sleep( 0.001) # Variable sleep time based on state machine state def shutdown(self): self.stop() self.bus.shutdown()
def __init__(self, channel): """Open connection with Vector hardware on selected channel, channel is 0 based""" xcp_rx_id = 0x1CE9F827 can_filters = [{"can_id": 0x1CE9F827, "can_mask": 0x0, "extended": True}] self.bus = VectorBus(channel=[channel], app_name='CANoe', can_filters=can_filters) # can_filters = [{"can_id": xcp_rx_id, "can_mask": 0xFFFFFFFF, "extended": True}] # self.bus.set_filters(can_filters) # self.reader = can.BufferedReader() self.notifier = can.Notifier(self.bus, [can.BufferedReader()])
def __init__(self, channel): conn = sqlite3.connect('main.db') self.c = conn.cursor() self.xcp_tx_id = 0xE927F8 # configure logging settings logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s') '''Open connection with Vector hardware, configure channel''' can.rc['interface'] = 'vector' can.rc['channel'] = 0 can.rc['bitrate'] = 500000 self.bus = VectorBus(channel=[channel], app_name='CANoe')
def __init__(self, parent): self.parent = parent self.bus = None self.Repro_numTotal = 0 self.Repro_num = 0 self.Repro_completeflag = False self.statusText = '' self.Func_addr = None self.Func_stack = None self.Func_conn = None self.Phys_addr = None self.Phys_stack = None self.Phys_conn = None # Vector Bus try: self.bus = VectorBus(channel=0, app_name='SANY SJB Reprogram', bitrate=250000) # Link Layer (CAN protocol) except: self.set_statusText( color='red', text='CAN Open fail. Check the vector Hardware Configuration.') else: udsoncan.setup_logging('logging.conf') self.set_statusText(color='green', text='CAN Open Success.') self.parent.canOpenSuccess()
def connect_bus(channel, filters=None): '''Open connect with Vector hardware, configure channel''' can.rc['interface'] = 'vector' can.rc['channel'] = 0 can.rc['bitrate'] = 500000 bus = VectorBus(channel=[channel], can_filters=filters, app_name='CANoe') logging.debug('Bus object created for ' + bus.channel_info) return bus
def __init__(self): isotp_params = { 'stmin' : 0, 'blocksize' : 4, 'wftmax' : 0, 'll_data_length' : 8, 'tx_padding' : 0xCC, 'rx_flowcontrol_timeout' : 1000, 'rx_consecutive_frame_timeout' : 1000, 'squash_stmin_requirement' : False, 'can_fd' : False, 'tx_data_min_length' : 8 } self.exit_requested = False #self.bus = VectorBus(channel='0', bitrate=500000) self.bus = VectorBus(channel='0', bitrate=500000, fd=True) addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0xF1, target_address=0x10) self.stack = isotp.CanStack(self.bus, address=addr, params=isotp_params, error_handler=self.my_error_handler)
def Send_p(self): load_layer('can') conf.contribs['CANSocket'] = {'use-python-can' : True} load_contrib('cansocket') from can.interfaces.vector import VectorBus socket = CANSocket(iface=VectorBus(0, bitrate=1000000)) packet = CAN(identifier=0x123, data=b'01020304') socket.send(packet) rx_packet = socket.recv() socket.sr1(packet)
class DoCan: def __init__(self, channel): """Open connection with Vector hardware on selected channel, channel is 0 based""" xcp_rx_id = 0x1CE9F827 can_filters = [{"can_id": 0x1CE9F827, "can_mask": 0x0, "extended": True}] self.bus = VectorBus(channel=[channel], app_name='CANoe', can_filters=can_filters) # can_filters = [{"can_id": xcp_rx_id, "can_mask": 0xFFFFFFFF, "extended": True}] # self.bus.set_filters(can_filters) # self.reader = can.BufferedReader() self.notifier = can.Notifier(self.bus, [can.BufferedReader()]) def connect(self): xcp_tx_id = 0xE927F8 xcp_connect = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xFF, 0, 0, 0, 0, 0, 0, 0]) self.bus.send(xcp_connect) def print_latest(self): msg = self.notifier.listeners[0].get_message() print('from latest {}'.format(msg)) def cyclic_thread(self, ID): my_lock = threading.Lock() pto_data = [0] pto_msg = can.Message(arbitration_id=ID, dlc=1, extended_id=True, data=pto_data) task = can.broadcastmanager.ThreadBasedCyclicSendTask(self.bus, my_lock, pto_msg, .01) task.start() return task @staticmethod def modify(thread): pto_data = [1] pto_msg = can.Message(arbitration_id=0x18fef0fe, dlc=1, extended_id=True, data=pto_data) thread.modify_data(pto_msg)
from can.interfaces.vector import VectorBus from udsoncan.connections import PythonIsoTpConnection from udsoncan.client import Client import isotp # Refer to isotp documentation for full details about parameters isotp_params = { 'stmin' : 32, # Will request the sender to wait 32ms between consecutive frame. 0-127ms or 100-900ns with values from 0xF1-0xF9 'blocksize' : 8, # Request the sender to send 8 consecutives frames before sending a new flow control message 'wftmax' : 0, # Number of wait frame allowed before triggering an error 'll_data_length' : 8, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) 'tx_padding' : 0, # Will pad all transmitted CAN messages with byte 0x00. None means no padding 'rx_flowcontrol_timeout' : 1000, # Triggers a timeout if a flow control is awaited for more than 1000 milliseconds 'rx_consecutive_frame_timeout' : 1000, # Triggers a timeout if a consecutive frame is awaited for more than 1000 milliseconds 'squash_stmin_requirement' : False # When sending, respect the stmin requirement of the receiver. If set to True, go as fast as possible. } bus = VectorBus(channel=0, bitrate=9600) # Link Layer (CAN protocol) tp_addr = isotp.Address(isotp.AddressingMode.Normal_11bits, txid=0x123, rxid=0x79D) # Network layer addressing scheme stack = isotp.CanStack(bus=bus, address=tp_addr, params=isotp_params) # Network/Transport layer (IsoTP protocol) conn = PythonIsoTpConnection(stack) # interface between Application and Transport layer with Client(conn, request_timeout=1) as client: # Application layer (UDS protocol) client.change_session(1) # ...
isotp_params = { #'stmin' : 0, # Will request the sender to wait 32ms between consecutive frame. 0-127ms or 100-900ns with values from 0xF1-0xF9 #'blocksize' : 0, # Request the sender to send 8 consecutives frames before sending a new flow control message #'wftmax' : 0, # Number of wait frame allowed before triggering an error #'ll_data_length' : 8, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) #'ll_data_length' : 64, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) #'tx_padding' : 0xCC, # Will pad all transmitted CAN messages with byte 0x00. None means no padding #'rx_flowcontrol_timeout' : 1000, # Triggers a timeout if a flow control is awaited for more than 1000 milliseconds #'rx_consecutive_frame_timeout' : 1000, # Triggers a timeout if a consecutive frame is awaited for more than 1000 milliseconds #'squash_stmin_requirement' : False, # When sending, respect the stmin requirement of the receiver. If set to True, go as fast as possible. #'can_fd' : True, #'tx_data_min_length' : 8 } bus = VectorBus(channel=0, bitrate=500000) addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0xF1, target_address=0x10) stack = isotp.CanStack(bus, address=addr, params=isotp_params) #stack.send(b'\x01\x02\x03\x04\x05\x06\x07') stack.send( b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10' ) while stack.transmitting(): stack.process() time.sleep(0.0001) bus.shutdown()
class ThreadedApp: def __init__(self): isotp_params = { 'stmin': 0, # Will request the sender to wait 32ms between consecutive frame. 0-127ms or 100-900ns with values from 0xF1-0xF9 'blocksize': 0, # Request the sender to send 8 consecutives frames before sending a new flow control message 'wftmax': 0, # Number of wait frame allowed before triggering an error #'ll_data_length' : 8, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) 'll_data_length': 16, # Link layer (CAN layer) works with 8 byte payload (CAN 2.0) 'tx_padding': 0xCC, # Will pad all transmitted CAN messages with byte 0x00. None means no padding 'rx_flowcontrol_timeout': 1000, # Triggers a timeout if a flow control is awaited for more than 1000 milliseconds 'rx_consecutive_frame_timeout': 1000, # Triggers a timeout if a consecutive frame is awaited for more than 1000 milliseconds 'squash_stmin_requirement': False, # When sending, respect the stmin requirement of the receiver. If set to True, go as fast as possible. 'can_fd': True, 'tx_data_min_length': 8, 'max_frame_size': 6000 } self.exit_requested = False #self.bus = VectorBus(channel='0', bitrate=500000) self.bus = VectorBus(channel='0', bitrate=500000, fd=True) addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0x10, target_address=0xF1) self.stack = isotp.CanStack(self.bus, address=addr, params=isotp_params, error_handler=self.my_error_handler) def start(self): self.exit_requested = False self.thread = threading.Thread(target=self.thread_task) self.thread.start() def stop(self): self.exit_requested = True if self.thread.isAlive(): self.thread.join() def send(self, msg): self.stack.send(msg) def my_error_handler(self, error): logging.warning('IsoTp error happened : %s - %s' % (error.__class__.__name__, str(error))) def thread_task(self): while self.exit_requested == False: self.stack.process() # Non-blocking #time.sleep(self.stack.sleep_time()) # Variable sleep time based on state machine state time.sleep( 0.001) # Variable sleep time based on state machine state def shutdown(self): self.stop() self.bus.shutdown()
'ignore_all_zero_dtc': True, 'dtc_snapshot_did_size': 2, # Not specified in standard. 2 bytes matches other services format. 'server_address_format': None, # 8,16,24,32,40 'server_memorysize_format': None, # 8,16,24,32,40 'data_identifiers': { 0x0200: MyCustomCodecThatShiftBy4, 0xD500: MyCustomCodecHex, 0xFE01: MyCustomCodecHex, 0xDF00: MyCustomCodecHex, 0xd472: MyCustomCodecHex }, 'input_output': {} } bus = VectorBus(channel=1, bitrate=500000) # Link Layer (CAN protocol) tp_addr = isotp.Address(isotp.AddressingMode.Normal_11bits, txid=0x7e1, rxid=0x782) # Network layer addressing scheme stack = isotp.CanStack( bus=bus, address=tp_addr, params=isotp_params) # Network/Transport layer (IsoTP protocol) conn = PythonIsoTpConnection( stack) # interface between Application and Transport layer with Client( conn, request_timeout=1, config=client_config) as client: # Application layer (UDS protocol) client.change_session(3) response = client.request_seed(1) #client.send_key(2, b'0001') response = client.read_data_by_identifier(0x0200)
class XcpCommunicator: def __init__(self, channel): conn = sqlite3.connect('main.db') self.c = conn.cursor() self.xcp_tx_id = 0xE927F8 # configure logging settings logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s') '''Open connection with Vector hardware, configure channel''' can.rc['interface'] = 'vector' can.rc['channel'] = 0 can.rc['bitrate'] = 500000 self.bus = VectorBus(channel=[channel], app_name='CANoe') def check_xcp_response(self): # slave to master: xcp_rx_id = 0x1CE9F827 # read response # NOTE: add more sophisticated XCP error reporting using error array for recvd_msg in self.bus: if recvd_msg.arbitration_id == xcp_rx_id: return recvd_msg def write_xcp(self, message): self.bus.send(message) response_message = self.check_xcp_response() command = hex(message.data[0]) if response_message.data[0] == 0xFF: # response indicates command succesful logging.debug('Command: {} Response: Success'.format(command)) elif response_message.data[0] == 0xFE: # response indicates error, report error error_code = response_message.data[1] self.c.execute("SELECT * FROM error_array WHERE error_code=?", (error_code, )) error_info = self.c.fetchone() if error_info is not None: logging.debug('Command: {} Response: {} {}'.format( command, error_info[1], error_info[2].strip())) else: logging.debug('Command: {} Response: {}'.format( hex(message.data[0]), hex(response_message.data[1]))) else: pass def send_connect(self): xcp_connect = can.Message(arbitration_id=self.xcp_tx_id, dlc=8, extended_id=True, data=[0xFF, 0, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_connect) def xcp_stop(self): xcp_stop_daq = can.Message(arbitration_id=self.xcp_tx_id, dlc=8, extended_id=True, data=[0xFE, 0, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_stop_daq) def setup_daq(self, address): xcp_tx_id = 0xE927F8 self.send_connect() # This command clears all DAQ lists xcp_free_daq = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xD6, 0, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_free_daq) # allocate daq lists daq_count = 1 alloc_daq_array = struct.pack('<Bxhxxxx', 0xD5, daq_count) xcp_alloc_daq = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=alloc_daq_array) self.write_xcp(xcp_alloc_daq) # allocate and assign ODT daq_list_number = 0 odt_count = 1 alloc_odt_array = struct.pack('<BxhBxxx', 0xD4, daq_list_number, odt_count) xcp_alloc_odt = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=alloc_odt_array) self.write_xcp(xcp_alloc_odt) # allocate entries and assign to specific ODT in DAQ list odt_number = 0 number_of_entries = 1 alloc_odt__entry_array = struct.pack('<BxhBBxx', 0xD3, daq_list_number, odt_number, number_of_entries) xcp_alloc_odt_entry = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=alloc_odt__entry_array) self.write_xcp(xcp_alloc_odt_entry) # configure DAQ lists # initializes the DAQ list pointer for a subsequent operation with WRITE_DAQ or READ_DAQ. xcp_set_daq_ptr = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xE2, 0, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_set_daq_ptr) # en_PscActivationState, SLONG length = 4, address = 0x20080CC bit_offset = 0xFF # normal data element element_size = 4 address_extension = 0 write_daq_data = struct.pack('<BBBBi', 0xE1, bit_offset, element_size, address_extension, address) xcp_write_daq = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=write_daq_data) self.write_xcp(xcp_write_daq) xcp_set_daq_list_mode = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xE0, 0, 0, 0, 1, 0, 2, 0]) self.write_xcp(xcp_set_daq_list_mode) xcp_start_stop_daq_list = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xDE, 2, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_start_stop_daq_list) xcp_start_daq = can.Message(arbitration_id=xcp_tx_id, dlc=8, extended_id=True, data=[0xDD, 1, 0, 0, 0, 0, 0, 0]) self.write_xcp(xcp_start_daq) def graph_response(self, time_delay): for i in range(time_delay): recvd_msg = self.check_xcp_response() y = struct.unpack('<xIxxx', recvd_msg.data) #y = (y[0] / 100000) # plt.scatter(i, y, '-') # plt.pause(.05) print(y) time.sleep(1)