Skip to content

can.viewer hangs when encountering CANFD data frames #1400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
n-92 opened this issue Sep 28, 2022 · 9 comments
Closed

can.viewer hangs when encountering CANFD data frames #1400

n-92 opened this issue Sep 28, 2022 · 9 comments
Labels

Comments

@n-92
Copy link

n-92 commented Sep 28, 2022

Description

can.viewer stops displaying incoming messages and the screen hangs upon encountering CAN messages that are sent using flexible data rate mode.

Expected behavior

The expected behavior should be that the can viewer must not crash or hang. It should display the CAN message even if DLC > 8

Additional context

OS and version: Windows 10 Enterprise
Python version: 3.8.10
python-can version: 4.0.0
python-can interface/s (if applicable): Vector VN1630A

While running can.viewer on one instance by executing as follows:

python -m can.viewer -c 1 -i vector -b 500000 --fd

I try to pump in some arbitrary messages using parameters for flexible data rate. The following is my simple script for replication.

import can
import time

print('\n\rCAN Tx test')

try:
	bus = can.interface.Bus(channel='1', bustype='vector', fd = True, bitrate=500000)
except Exception as e:
	print(e)
	exit()
	

# Main loop
while 1:
     msg = can.Message(arbitration_id=0x012,is_fd = True, bitrate_switch = True, data=[0,0,0,0,0,0x1e,0x21,0xfe, 0x80, 0, 0,0, 0],is_extended_id=True)
     bus.send(msg)
     time.sleep(0.2)

The can.viewer hangs upon receiving a frame whose DLC > 8.

@n-92 n-92 added the bug label Sep 28, 2022
@zariiii9003
Copy link
Collaborator

Have you tried the develop branch? The can.viewer works fine with CAN FD and long messages, i just tested it.

@j-c-cook
Copy link
Contributor

@o92 To install the develop branch, you can use pip install git+https://github.com/hardbyte/python-can@develop.

@n-92
Copy link
Author

n-92 commented Sep 29, 2022

@j-c-cook @zariiii9003 First of all, thank you very much for your response. I got myself the dev branch installed and tried out.

However, when I execute the script with > python -m can.viewer -c 1 -i vector, I can see the messages but they are truncated. The DLC indication is correct. Note that, I did NOT have to supply --fd flag. If I did that, I couldn't see the messages that I was sending with DLC> 8.

https://user-images.githubusercontent.com/8834529/193082331-c0920d00-b3dc-4b1a-afb1-71757de3c251.png

And when I try to print msg.data field on console, I get as follows:
bytearray(b'!!!!!\x1e!\xfe')

@zariiii9003
Copy link
Collaborator

Can you run this?

import curses
import random
import threading
import time

import can
from can.interfaces.vector import VectorBus
from can.viewer import CanViewer


def send_fd_messages(bus: VectorBus) -> None:
    while True:
        length = random.randint(1, 64)
        data = random.randbytes(length)
        msg = can.Message(
            arbitration_id=length,
            data=bytearray(data),
            is_fd=True,
            is_rx=False,
            bitrate_switch=True,
            dlc=length,
        )
        bus.send(msg)
        time.sleep(0.1)


if __name__ == '__main__':
    bus = VectorBus(
        serial=100,  # the serial number of your virtual bus. Can be 0 or 100
        channel=0,
        fd=True,
        bitrate=500_000,
        data_bitrate=1_000_000,
        receive_own_messages=True,
    )
    send_thread = threading.Thread(target=send_fd_messages, args=(bus,), daemon=True)
    send_thread.start()

    curses.wrapper(CanViewer, bus, None)

@n-92
Copy link
Author

n-92 commented Sep 30, 2022

Just an update:

I can now see the CAN FD messages from the ECU, but I cannot see the messages that I sent out. The script is as follows:

import random
import time
import os 
import can
from can.interfaces.vector import VectorBus



def send_fd_messages(bus: VectorBus) -> None:
    while True:
        length = random.randint(1, 12)
        data = os.urandom(length)
        msg = can.Message(
            arbitration_id=0,
            data=bytearray(data),
            is_fd=True,
            is_rx=False,
            bitrate_switch=True,
            dlc=length,
        )
        bus.send(msg)
        time.sleep(0.1)


if __name__ == '__main__':
    bus = VectorBus(
        app_name='python-can',  # the serial number of your virtual bus. Can be 0 or 100
        channel=1,
        fd=True,
        bitrate=500_000,
        data_bitrate=2_000_000,
        receive_own_messages=True,
    )
    send_fd_messages(bus)
    

@n-92
Copy link
Author

n-92 commented Sep 30, 2022

Also, the moment I initialize Kvaser device in code, it throws the following error.

import random
import time
import os 
import can
#from can.interfaces.vector import VectorBus
from can.interfaces.kvaser import KvaserBus


def send_fd_messages(bus: KvaserBus) -> None:
	while True:
            print(1)
		# length = random.randint(1,8)
		# data = os.urandom(length)
		# msg = can.Message(arbitration_id=0x18DBFFF9,data=bytearray(data),is_fd=False,is_rx=False,bitrate_switch=False,dlc=length)
		# print(msg)
		# bus.send(msg)
		# time.sleep(0.5)


if __name__ == '__main__':
    bus = KvaserBus(
        #app_name='python-can',  # the serial number of your virtual bus. Can be 0 or 100
        channel=1,
        fd=True,
        bitrate=500_000,
        data_bitrate=2_000_000,
        receive_own_messages=False,
        
    )
    send_fd_messages(bus)

Traceback

Traceback (most recent call last):
File ".\fd_tx_test.py", line 21, in
bus = KvaserBus(
File "C:\N92\python-can dev\lib\site-packages\can\interfaces\kvaser\canlib.py", line 453, in init
self._read_handle = canOpenChannel(channel, flags)
File "C:\N92\python-can dev\lib\site-packages\can\interfaces\kvaser\canlib.py", line 131, in __check_bus_handle_validity
raise CANLIBInitializationError(function, result, arguments)
can.interfaces.kvaser.canlib.CANLIBInitializationError: Function canOpenChannel failed - Specified device not found [Error Code -3]

Kvaser Device guide

image

@n-92
Copy link
Author

n-92 commented Oct 3, 2022

Hello,

Thank you both @j-c-cook @zariiii9003 for your help. I have managed to found out that the Kvaser VCI device that I have, only supports CAN and not CAN FD. As for the Vector device, our ECU has some special set up that only can communicate with specific peripheral devices. When I connected such a peripheral unit, I could read and send CAN FD messages directly on the CAN bus. And it works with both the older version of can.viewer as well as the dev branch.

I will close this case as it has been resolved for me.

@n-92 n-92 closed this as completed Oct 3, 2022
@j-c-cook
Copy link
Contributor

j-c-cook commented Oct 3, 2022

Thank you for the update and closing your issue now that it's resolved. Glad to know the develop branch is functional for your application. 🚀

@zariiii9003
Copy link
Collaborator

Hello,

As for the Vector device, our ECU has some special set up that only can communicate with specific peripheral devices. When I connected such a peripheral unit, I could read and send CAN FD messages directly on the CAN bus.

That doesn't sound plausible to me. I assume that you set your bit timings correctly.
Could it be that you are using the non ISO CANFD version? You could add a new line here to try that:

canfd_conf = xlclass.XLcanFdConf()

        canfd_conf = xlclass.XLcanFdConf()
        canfd_conf.options = xldefine.XL_CANFD_ConfigOptions.CANFD_CONFOPT_NO_ISO.value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants