Need to bring up CANbus interface before Qt can detect it?
-
wrote on 30 Dec 2022, 17:33 last edited by
I am trying to get my PEAK CAN device working under linux (Fedora 34). The device shows in Linux:
[root@fedora ~]# dmesg | grep -i peak [ 1.349528] usb 1-1: Manufacturer: PEAK-System Technik GmbH [ 10.739841] peak_usb 1-1:1.0: PEAK-System PCAN-USB FD v1 fw v3.4.3 (1 channels) [ 10.748044] peak_usb 1-1:1.0 can0: attached to PCAN-USB FD channel 0 (device 0) [ 10.748081] usbcore: registered new interface driver peak_usb [root@fedora ~]# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:a2:02:e9 brd ff:ff:ff:ff:ff:ff altname enp11s0 3: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT group default qlen 10 link/can 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000 link/ether 52:54:00:d5:66:57 brd ff:ff:ff:ff:ff:ff
But my code always always sets devices to an empty list:
QString errorString; const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices( QStringLiteral("socketcan"), &errorString); if (devices.count()==0) qDebug() << "No devices found";
However, if I manually bring up the device from Linux first like this, then a can device is found in the above code:
[root@fedora ~]# ip link set can0 type can bitrate 125000 [root@fedora ~]# ip link set up can0
Is this normal behavior? I want my program to do everything without user intervention (including controlling the can0 interface). And my program will set the bitrate in later code. Why would the user have to do this in Linux command line first?
-
It is a normal behavior. To initialize the CAN interface you need to write a some udev-rules which are will configure your CAN automatically when it been attached to the system.
It is similar as, e.g. the
Network Manager
does for the ETH interfaces and so on. Or, how the USB devices (flash drives) are mounted automatically when they are plugged in.UPD: AFAIK, the qt-serialbus module supports the configuring the CAN bus, but for the
SocketCan
driver. So, you can try to use your PEACK CAN device with the Socket CAN driver, instead of the vendor-specific PEACK CAN driver (if I understand it correctly). How to do it you can search in an internet. ;) -
wrote on 30 Dec 2022, 19:21 last edited by ocgltd
I want to make my code generic for all CAN interfaces (not just Peak) - so I want to use the socketcan driver.
Ok I will bring up the CAN0 interface separately then. However, something else isn't clear: since I must set the can0 bitrate from the commandline before bringing up the interface, what is the point of the of Qt's set configuration parameter for bitrate? For example:
m_device->setConfigurationParameter(QCanBusDevice::BitRateKey, m_bitrateBus);
Does this simple re-set the bitrate, or is this ignored?
-
To configure the bitrate programmatically, you need the libsocketcan library installed. Also you need a root rights.
In other cases it been ignored, just check the console warning messages.
Also, you can read an official documentation about the SocketCAN driver support in QtSerialBus module.
-
1/4