OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python Script with Multithreading and CAN Bus Communication Does Not Terminate

  • Thread starter Thread starter Mageo
  • Start date Start date
M

Mageo

Guest
I am working on a Python script that sets up communication between multiple Electronic Control Units (ECU1 and ECU2) using the python-can library. The script is designed to read messages from a BLF file, filter them based on specific IDs, and handle them using separate threads for each ECU. However, the script does not terminate as expected and seems to get stuck after printing "main_ECU done","filtering done","ECU1 Thread has finished".

script flow:

  1. main_ecu fucntion - reads the message and send to virtual bus
  2. ecu_filter function - filter the messages based on ID and stop when ID 'oxDE0Q is Received
  3. 'main' function - create virtual bus, use threading as shown below

Code:
def main():
    bus = can.interface.Bus(interface='virtual', channel='0', bitrate=500000,receive_own_messages=True)

    can_id_filter_ecu1 = [0xKEREE, 0xWJKL0, oxDE0Q]
    can_id_filter_ecu_2 = [0xIOBF, oxDE0Q]

    main_ECU(bus, "input.blf")
    #two seperate threads for ecu 1 and ecu 2
    ecu_thread_1 = threading.Thread(target=ecu_filter,args= (bus,can_id_filter_ecu1,"File_1_msg.asc",))
    ecu_thread_2 = threading.Thread(target=ecu_filter, args=(bus,can_id_filter_ecu_2,"File_2_msg.asc",))

    ecu_thread_1.start()
    ecu_thread_2.start()

    ecu_thread_1.join()
    print("ECU1 Thread has finished.")
    ecu_thread_2.join()
    print("ECU2 Thread has finished.")
    bus.shutdown()

update
now the script runs and there is a value error and donot terminate: ValueError: byte must be in range(0, 256) for the line(added below) included in the fucntion ecu_filter
response_msg = can.Message(arbitration_id=msg_id,data= bytearray([count]), is_extended_id=False)

a snippet of ecu_filter fucntion is as below:

Code:
def ecu_filter(bus, filter_ids, asc_file):
    stop_break = False
    log_ecu = can.ASCWriter(asc_file)
    msg_count = {}

    while not stop_break:
        mesg = bus.recv(1)
        print(mesg)  # Debugging output
        if mesg is not None and mesg.arbitration_id in filter_ids:
            log_ecu.log_event(mesg)
            if mesg.arbitration_id == oxDE0Q:
                stop_break = True
            elif mesg.arbitration_id in msg_count:
                msg_count[mesg.arbitration_id] += 1
            else:
                msg_count[mesg.arbitration_id] = 1
        for msg_id, count in msg_count.items():
            response_msg = can.Message(arbitration_id=msg_id, data=bytearray([count]), is_extended_id=False)
            bus.send(response_msg)
    log_ecu.stop()

output
main_ECU done
filtering done
ECU1 Thread has finished.
<p>I am working on a Python script that sets up communication between multiple Electronic Control Units (ECU1 and ECU2) using the python-can library. The script is designed to read messages from a BLF file, filter them based on specific IDs, and handle them using separate threads for each ECU. However, the script does not terminate as expected and seems to get stuck after printing "main_ECU done","filtering done","ECU1 Thread has finished".</p>
<p><strong>script flow</strong>:<br/></p>
<ol>
<li>main_ecu fucntion - reads the message and send to virtual bus</li>
<li>ecu_filter function - filter the messages based on ID and stop when ID 'oxDE0Q is Received</li>
<li>'main' function - create virtual bus, use threading as shown below</li>
</ol>
<pre><code>def main():
bus = can.interface.Bus(interface='virtual', channel='0', bitrate=500000,receive_own_messages=True)

can_id_filter_ecu1 = [0xKEREE, 0xWJKL0, oxDE0Q]
can_id_filter_ecu_2 = [0xIOBF, oxDE0Q]

main_ECU(bus, "input.blf")
#two seperate threads for ecu 1 and ecu 2
ecu_thread_1 = threading.Thread(target=ecu_filter,args= (bus,can_id_filter_ecu1,"File_1_msg.asc",))
ecu_thread_2 = threading.Thread(target=ecu_filter, args=(bus,can_id_filter_ecu_2,"File_2_msg.asc",))

ecu_thread_1.start()
ecu_thread_2.start()

ecu_thread_1.join()
print("ECU1 Thread has finished.")
ecu_thread_2.join()
print("ECU2 Thread has finished.")
bus.shutdown()
</code></pre>
<p><strong>update</strong><br />
now the script runs and there is a value error and donot terminate: <em>ValueError: byte must be in range(0, 256) for the line(added below) included in the fucntion ecu_filter</em><br />
<code>response_msg = can.Message(arbitration_id=msg_id,data= bytearray([count]), is_extended_id=False)</code></p>
<p>a snippet of ecu_filter fucntion is as below:</p>
<pre><code>def ecu_filter(bus, filter_ids, asc_file):
stop_break = False
log_ecu = can.ASCWriter(asc_file)
msg_count = {}

while not stop_break:
mesg = bus.recv(1)
print(mesg) # Debugging output
if mesg is not None and mesg.arbitration_id in filter_ids:
log_ecu.log_event(mesg)
if mesg.arbitration_id == oxDE0Q:
stop_break = True
elif mesg.arbitration_id in msg_count:
msg_count[mesg.arbitration_id] += 1
else:
msg_count[mesg.arbitration_id] = 1
for msg_id, count in msg_count.items():
response_msg = can.Message(arbitration_id=msg_id, data=bytearray([count]), is_extended_id=False)
bus.send(response_msg)
log_ecu.stop()

</code></pre>
<p><strong>output</strong><br />
main_ECU done<br />
filtering done<br />
ECU1 Thread has finished.</p>
 

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top