PCAN-USB on Linux

This forum covers PCAN-Linux and Linux development issues concerning our products
Post Reply
Kenny1967
Posts: 3
Joined: Mon 25. Nov 2024, 08:43

PCAN-USB on Linux

Post by Kenny1967 » Wed 9. Apr 2025, 02:05

Hi,

I have two PCAN-USB devices attached to each other to test Python code to send CAN packets (code below). I use `candump can0` and `candump can1` in separate shells and use a third shell to send my test frames. I'm using `socketcan` with `python-can 4.2.2`.

If I send on either can0 or can1 I see the packets printed by `candump` but I get a mysterious frame with ID 0x200. Then then the interface apparently errors out and goes down. In the case below, I send a single packet to 0x0F0.

$ candump can0
can0 0F0 [8] 00 19 00 01 03 01 04 00
can0 200 [8] 00 08 00 00 00 00 60 00

$ ip link
7: can1: <NO-CARRIER,NOARP,UP,ECHO> mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
link/can
8: can0: <NO-CARRIER,NOARP,UP,ECHO> mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
link/can

$ ip -details -statistics link show can0
8: can0: <NO-CARRIER,NOARP,UP,ECHO> mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0
can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
bitrate 50000 sample-point 0.875
tq 1250 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 brp 10
pcan_usb: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp_inc 1
clock 8000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 1 0 1 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus usb parentdev 1-3:1.0
RX: bytes packets errors dropped missed mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
8 1 0 1 0 0

Code: Select all

RECEIVER_ID = 0x0F0
TRANSMITTER_ID = 0x0F1
FILTERS_0 = [
    {"can_id": RECEIVER_ID, "can_mask": 0x7FF, "extended": False},
]
FILTERS_1 = [
    {"can_id": TRANSMITTER_ID, "can_mask": 0x7FF, "extended": False},
]
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="CAN Prototype")
    parser.add_argument(
        "--receive",
        "-r",
        action="store_true",
    )
    parser.add_argument(
        "--bus",
        "-b",
        type=int,
        default=0,
        choices=(0, 1),
    )
    parser.add_argument(
        "--count",
        "-c",
        type=int,
        default=1,
    )
    arguments = parser.parse_args()
    if arguments.receive:
        print("Receiving")
        with can.Bus("can0", "socketcan", can_filters=FILTERS_0) as bus:
            while True:
                message = bus.recv()
                print(message.data)
    else:
        print("Transmitting")
        with can.Bus(f"can{arguments.bus}", "socketcan", can_filters=FILTERS_1) as bus:
            for i in range(arguments.count):
                message = can.Message(
                    arbitration_id=RECEIVER_ID,
                    data=[0, 25, 0, 1, 3, 1, 4, i],
                    is_extended_id=False
                )
                try:
                    bus.send(message)
                    print(f"Message {i} sent on {bus.channel_info}")
                except can.CanError:
                    print("Message NOT sent")
                time.sleep(1)


$ uname -a
Linux bobby 6.11.0-17-generic #17~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 20 22:48:29 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
$ lsmod | grep peak
peak_usb 65536 0
can_dev 53248 1 peak_usb

M.Maidhof
Support
Support
Posts: 1751
Joined: Wed 22. Sep 2010, 14:00

Re: PCAN-USB on Linux

Post by M.Maidhof » Wed 9. Apr 2025, 10:06

Hi,

did you use a terminated CAN cable with 120 Ohms on both ends between CAN-H and CAN-L to connect both PCAN-USBs?

regards

Michael

Kenny1967
Posts: 3
Joined: Mon 25. Nov 2024, 08:43

Re: PCAN-USB on Linux

Post by Kenny1967 » Thu 10. Apr 2025, 01:10

Hi Michael,

Yes. Both sides are terminated.

Kenny

Kenny1967
Posts: 3
Joined: Mon 25. Nov 2024, 08:43

Re: PCAN-USB on Linux

Post by Kenny1967 » Thu 10. Apr 2025, 07:25

It seems that two dongles don't like to be plugged into the same machine? I'm curious if the device IDs must be different. In any case, I plugged each PCAN-USB into a separate Linux box and had no issues. Couldn't get the two talking to each other on a Windows box either. It's not a use case that matters to me--I just wanted to test a loop-back between nodes on the same machine to verify the drivers and Python library were working--so I have a path forward.

M.Maidhof
Support
Support
Posts: 1751
Joined: Wed 22. Sep 2010, 14:00

Re: PCAN-USB on Linux

Post by M.Maidhof » Thu 10. Apr 2025, 09:42

Hi,

no, it is no problem to use two PCAN-USBs on the same PC. Check your CAN cable again, and do the same test under Windows, just to be sure both devices are working correctly.
As a test you can also install our peak-linux-driver in chardev and pcanview, to test the communication under Linux.

regards

Michael

Post Reply