kvaser leaf and SocketCAN
Hello,
I'm trying to connect to a Kvaser Leaf Light V2 (product_id=15) adapter on linux from python3.4, and for that purpose I've planned to use SocketCAN.
I have downloaded and compiled the kvaser drivers v.5.9, my kernel is a 3.13.0-49-generic, and according to the information I've fund so far, the system requirements should all be met.
And in fact, when I connect the adapter, the leaf kernel module is correctly loaded (I see it in lsmod) and the /usr/sbin/leaf.sh is started (I desume this from the fact that if I try to start it manually it tells me it has already been started), and also the /dev/leaf0 device is created.
So far so good. But what's next? What I have understood is that at this point there should be the kvaser_usb module that comes into play for mapping the device to a network interface that can be brought up and used by SocketCAN.
But the kvaser_usb module is not loaded automatically, and if I modprobe it manually, nothing seems to happen.
Here is what goes on in the kernel log when connecting the adapter:
[46464.671385] usb 3-2: new high-speed USB device number 4 using xhci_hcd [46464.689616] usb 3-2: New USB device found, idVendor=0bfd, idProduct=0120 [46464.689627] usb 3-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [46464.689633] usb 3-2: Product: Kvaser Leaf Light v2 [46467.738639] usbcore: registered new interface driver leaf
And this when I modprobe the kvaser_usb module:
[46545.271520] usbcore: registered new interface driver kvaser_usb
If I invert the order of the two operations, it is exactly the same. I've also tryed with and without an actual can device connected to the other end of the adapter, but this doesn't change anything either.
So, it looks I am still missing something... Can anyone tell me what further steps are necessary to finally get a can0 interface to show up?
Thank's a lot for any hint pointing me to the right direction...
Max
Marc Kleine-Budde
maintainerQuoted 6 lines · max
I'm trying to connect to a Kvaser Leaf Light V2 (product_id=15) adapter on linux from python3.4, and for that purpose I've planned to use SocketCAN.
I have downloaded and compiled the kvaser drivers v.5.9, my kernel is a 3.13.0-49-generic, and according to the information I've fund so far, the system requirements should all be met.
Where do you download the driver from? I suggest to use a recent mainline kernel. Support for the Kvaser Leaf v2 was added in v3.16 and a _lot_ of bugfixes were added in later versions. Switching to a recent ubuntu kernel (v3.19) is probably a good idea.
Quoted 4 lines
And in fact, when I connect the adapter, the leaf kernel module is correctly loaded (I see it in lsmod) and the
/usr/sbin/leaf.shis started (I desume this from the fact that if I try to start it manually it tells me it has already been started), and also the/dev/leaf0device is created.
A /dev/leaf0 is not created by the mainline driver...
Quoted 21 lines
So far so good. But what's next? What I have understood is that at this point there should be the
kvaser_usbmodule that comes into play for mapping the device to a network interface that can be brought up and used by SocketCAN.But the
kvaser_usbmodule is not loaded automatically, and if I modprobe it manually, nothing seems to happen.Here is what goes on in the kernel log when connecting the adapter:
[46464.671385] usb 3-2: new high-speed USB device number 4 using xhci_hcd [46464.689616] usb 3-2: New USB device found, idVendor=0bfd, idProduct=0120 [46464.689627] usb 3-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [46464.689633] usb 3-2: Product: Kvaser Leaf Light v2 [46467.738639] usbcore: registered new interface driver leafAnd this when I modprobe the
kvaser_usbmodule:[46545.271520] usbcore: registered new interface driver kvaser_usb
Quoted 8 lines
If I invert the order of the two operations, it is exactly the same. I've also tryed with and without an actual can device connected to the other end of the adapter, but this doesn't change anything either.
So, it looks I am still missing something... Can anyone tell me what further steps are necessary to finally get a can0 interface to show up?
Thank's a lot for any hint pointing me to the right direction...
Use a recent mainline kernel, then everything should work out of the box, without any external drivers and manual module loading.
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Quoted 3 lines · Marc Kleine-Budde <mkl <at> pengutronix.de> writes:
Quoted 1 line · max
I'm trying to connect to a Kvaser Leaf Light V2 (product_id=15) adapter
on
Quoted 1 line
Quoted 1 line
linux from python3.4, and for that purpose I've planned to use
SocketCAN.
Quoted 3 lines
Quoted 3 lines
I have downloaded and compiled the kvaser drivers v.5.9, my kernel is a 3.13.0-49-generic, and according to the information I've fund so far,
the
Quoted 9 lines
Quoted 1 line
system requirements should all be met.
Where do you download the driver from? I suggest to use a recent mainline kernel. Support for the Kvaser Leaf v2 was added in v3.16 and a _lot_ of bugfixes were added in later versions. Switching to a recent ubuntu kernel (v3.19) is probably a good idea.
Quoted 2 lines
And in fact, when I connect the adapter, the leaf kernel module is correctly loaded (I see it in lsmod) and the
/usr/sbin/leaf.shis
started
Quoted 1 line
Quoted 1 line
(I desume this from the fact that if I try to start it manually it
tells me
Quoted 1 line
Quoted 1 line
it has already been started), and also the
/dev/leaf0device is
created.
Quoted 6 lines
A
/dev/leaf0is not created by the mainline driver...Quoted 3 lines
So far so good. But what's next? What I have understood is that at this point there should be the
kvaser_usbmodule that comes into play for mapping the device to a network interface that can be brought up and
used
Quoted 3 lines
Quoted 3 lines
by SocketCAN.
But the
kvaser_usbmodule is not loaded automatically, and if I
modprobe it
Quoted 5 lines
Quoted 5 lines
manually, nothing seems to happen.
Here is what goes on in the kernel log when connecting the adapter:
[46464.671385] usb 3-2: new high-speed USB device number 4 using
xhci_hcd
Quoted 1 line
Quoted 1 line
[46464.689616] usb 3-2: New USB device found, idVendor=0bfd,
idProduct=0120
Quoted 10 lines
Quoted 10 lines
[46464.689627] usb 3-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [46464.689633] usb 3-2: Product: Kvaser Leaf Light v2 [46467.738639] usbcore: registered new interface driver leafAnd this when I modprobe the
kvaser_usbmodule:[46545.271520] usbcore: registered new interface driver kvaser_usbIf I invert the order of the two operations, it is exactly the same.
I've
Quoted 1 line
Quoted 1 line
also tryed with and without an actual can device connected to the other
end
Quoted 11 lines
Quoted 6 lines
of the adapter, but this doesn't change anything either.
So, it looks I am still missing something... Can anyone tell me what further steps are necessary to finally get a can0 interface to show up?
Thank's a lot for any hint pointing me to the right direction...
Use a recent mainline kernel, then everything should work out of the box, without any external drivers and manual module loading.
Marc
Hi Marc and Max,
I am also trying to use this device on Ubuntu 14.04 with Linux kernel 3.19.0-031900-generic.
When I connect the device I have the kernel log as follow
[ 3506.638843] usb 1-3: USB disconnect, device number 6 [ 3508.818400] usb 1-3: new high-speed USB device number 7 using xhci_hcd [ 3508.949596] usb 1-3: New USB device found, idVendor=0bfd, idProduct=0120 [ 3508.949603] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [ 3508.949607] usb 1-3: Product: Kvaser Leaf Light HS
I have installed the drivers from Kvaser for linux therefore I have the modules :
kvaser_usb 22385 0 can_dev 21659 1 kvaser_usb
However nothing appears in /dev folder
I tried the SocketCan example code from this wiki page :
http://en.wikipedia.org/wiki/SocketCAN
But I get this error :
vcan0 at index -571419056
Error in socket bind: No such device
Thanks in advance for any help.
Leo
Quoted 17 lines · Leo Stan
When I connect the device I have the kernel log as follow
[ 3506.638843] usb 1-3: USB disconnect, device number 6 [ 3508.818400] usb 1-3: new high-speed USB device number 7 using xhci_hcd [ 3508.949596] usb 1-3: New USB device found, idVendor=0bfd, idProduct=0120 [ 3508.949603] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [ 3508.949607] usb 1-3: Product: Kvaser Leaf Light HSI have installed the drivers from Kvaser for linux therefore I have the modules :
kvaser_usb 22385 0 can_dev 21659 1 kvaser_usbHowever nothing appears in /dev folder
Quoted 8 lines
I tried the SocketCan example code from this wiki page :
http://en.wikipedia.org/wiki/SocketCAN
But I get this error :
vcan0 at index -571419056
Error in socket bind: No such device
You won't see anything in /dev/ if socketcan is working correctly. Try ifconfig -a to see the interface. From one of my machines (with a SJA1000 from Peak):
$ ifconfig -a
can0 Link encap:UNSPEC HWaddr
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:18Austin
Quoted 11 lines · Austin Schuh <austin <at> peloton-tech.com> writes:
$ ifconfig -a
can0 Link encap:UNSPEC HWaddr
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:18Quoted 7 lines
Austin
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to majordomo <at> vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Austin, thanks for your reply.
My ifconfig -a returns :
eth0 Link encap:Ethernet HWaddr 44:8a:5b:6d:50:8d
inet addr:131.181.70.9 Bcast:131.181.70.255 Mask:255.255.255.0 inet6 addr: fe80::468a:5bff:fe6d:508d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3869 errors:0 dropped:0 overruns:0 frame:0 TX packets:1783 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:1268663 (1.2 MB) TX bytes:481256 (481.2 KB) Interrupt:19
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:228 errors:0 dropped:0 overruns:0 frame:0 TX packets:228 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
RX bytes:37579 (37.5 KB) TX bytes:37579 (37.5 KB)
wlan0 Link encap:Ethernet HWaddr e8:2a:ea:de:3b:ae
UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
So I don't think it detected the Leaf...
Now that I check my /dev/ folder there is actually leaf0 in it. Maybe installing the drivers from Kvaser prevents me from using SocketCan?
Also I have all of these modules running that a related to the leaf :
can_raw 17159 0 can 45043 1 can_raw kvaser_usb 22385 0 can_dev 21659 1 kvaser_usb leaf 43136 1
Thank you for your help
Leo
Marc Kleine-Budde
maintainerQuoted 30 lines · Leo Stan
Hi Austin, thanks for your reply.
My ifconfig -a returns :
eth0 Link encap:Ethernet HWaddr 44:8a:5b:6d:50:8d
inet addr:131.181.70.9 Bcast:131.181.70.255 Mask:255.255.255.0 inet6 addr: fe80::468a:5bff:fe6d:508d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3869 errors:0 dropped:0 overruns:0 frame:0 TX packets:1783 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:1268663 (1.2 MB) TX bytes:481256 (481.2 KB) Interrupt:19lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:228 errors:0 dropped:0 overruns:0 frame:0 TX packets:228 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
RX bytes:37579 (37.5 KB) TX bytes:37579 (37.5 KB)wlan0 Link encap:Ethernet HWaddr e8:2a:ea:de:3b:ae
UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Quoted 4 lines
So I don't think it detected the Leaf...
Now that I check my
/dev/folder there is actually leaf0 in it. Maybe installing the drivers from Kvaser prevents me from using SocketCan?
This can be the problem. Please try with the mainline drivers only.
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Quoted 8 lines · Marc Kleine-Budde <mkl <at> pengutronix.de> writes:
Quoted 6 lines · Leo Stan
Hi Austin, thanks for your reply.
My ifconfig -a returns :
eth0 Link encap:Ethernet HWaddr 44:8a:5b:6d:50:8d
inet addr:131.181.70.9 Bcast:131.181.70.255
Mask:255.255.255.0
Quoted 24 lines
Quoted 24 lines
inet6 addr: fe80::468a:5bff:fe6d:508d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3869 errors:0 dropped:0 overruns:0 frame:0 TX packets:1783 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:1268663 (1.2 MB) TX bytes:481256 (481.2 KB) Interrupt:19lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:228 errors:0 dropped:0 overruns:0 frame:0 TX packets:228 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
RX bytes:37579 (37.5 KB) TX bytes:37579 (37.5 KB)wlan0 Link encap:Ethernet HWaddr e8:2a:ea:de:3b:ae
UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Quoted 9 lines
Quoted 4 lines
So I don't think it detected the Leaf...
Now that I check my
/dev/folder there is actually leaf0 in it. Maybe installing the drivers from Kvaser prevents me from using SocketCan?This can be the problem. Please try with the mainline drivers only.
Marc
Hello Marc,
I uninstalled the driver and now :
- I don't have the leaf in /dev/ when I plug it, which is a good thing
- I have those modules running :
can_raw 17159 0 can 45043 1 can_raw kvaser_usb 22385 0 can_dev 21659 1 kvaser_usb
- ifconfig -a shows the CAN :
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- My kernel log shows :
usb 1-3: new high-speed USB device number 4 using xhci_hcd
[ 1393.767114] usb 1-3: New USB device found, idVendor=0bfd, idProduct=0120 [ 1393.767124] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [ 1393.767128] usb 1-3: Product: Kvaser Leaf Light HS [ 1393.815801] CAN device driver interface [ 1393.857896] usbcore: registered new interface driver kvaser_usb
- When I try to run the example from http://en.wikipedia.org/wiki/SocketCAN I get this error :
vcan0 at index 1866752400
Error in socket bind: No such device
Thanks a lot for helping me.
Hi Leo,
Quoted 3 lines · Leo Stan
- ifconfig -a shows the CAN :
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-
^^^^
Compare this with...
Quoted 17 lines
00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)- My kernel log shows :
usb 1-3: new high-speed USB device number 4 using
xhci_hcd[ 1393.767114] usb 1-3: New USB device found, idVendor=0bfd, idProduct=0120 [ 1393.767124] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [ 1393.767128] usb 1-3: Product: Kvaser Leaf Light HS [ 1393.815801] CAN device driver interface [ 1393.857896] usbcore: registered new interface driver kvaser_usb
Quoted 3 lines
- When I try to run the example from http://en.wikipedia.org/wiki/SocketCAN I get this error :
vcan0 at index 1866752400
^^^^^
...this. Notice the difference?
You also might want to use https://github.com/linux-can/can-utils/
HTH
Alexander
Signature · 12 lines
Dipl.-Inf. Alexander Stein SYS TEC electronic GmbH Am Windrad 2 08468 Heinsdorfergrund Tel.: 03765 38600-1156 Fax: 03765 38600-4100 Email: [email protected] Website: www.systec-electronic.com Managing Director: Dipl.-Phys. Siegmar Schmidt Commercial registry: Amtsgericht Chemnitz, HRB 28082
Marc Kleine-Budde
maintainerQuoted 5 lines · Leo Stan
I uninstalled the driver and now :
- I don't have the leaf in
/dev/when I plug it, which is a good thing
- I have those modules running :
Quoted 8 lines
can_raw 17159 0 can 45043 1 can_raw kvaser_usb 22385 0 can_dev 21659 1 kvaser_usb- ifconfig -a shows the CAN :
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-
^^^^
Quoted 6 lines
00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
\o/
Quoted 10 lines
- My kernel log shows :
usb 1-3: new high-speed USB device number 4 using
xhci_hcd[ 1393.767114] usb 1-3: New USB device found, idVendor=0bfd, idProduct=0120 [ 1393.767124] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0 [ 1393.767128] usb 1-3: Product: Kvaser Leaf Light HS [ 1393.815801] CAN device driver interface [ 1393.857896] usbcore: registered new interface driver kvaser_usb
Quoted 3 lines
- When I try to run the example from http://en.wikipedia.org/wiki/SocketCAN I get this error :
vcan0 at index 1866752400
^^^^^
Quoted 1 line
Error in socket bind: No such device
You're using "can0" - the example is for virtual CAN device "vcan0". Please change the interface in the Code.
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Quoted 31 lines · Marc Kleine-Budde
Quoted 30 lines · Leo Stan
Hi Austin, thanks for your reply.
My ifconfig -a returns :
eth0 Link encap:Ethernet HWaddr 44:8a:5b:6d:50:8d
inet addr:131.181.70.9 Bcast:131.181.70.255 Mask:255.255.255.0 inet6 addr: fe80::468a:5bff:fe6d:508d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3869 errors:0 dropped:0 overruns:0 frame:0 TX packets:1783 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:1268663 (1.2 MB) TX bytes:481256 (481.2 KB) Interrupt:19lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:228 errors:0 dropped:0 overruns:0 frame:0 TX packets:228 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
RX bytes:37579 (37.5 KB) TX bytes:37579 (37.5 KB)wlan0 Link encap:Ethernet HWaddr e8:2a:ea:de:3b:ae
UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Quoted 4 lines
Quoted 4 lines
So I don't think it detected the Leaf...
Now that I check my
/dev/folder there is actually leaf0 in it. Maybe installing the drivers from Kvaser prevents me from using SocketCan?
Yes, per their release notes, the Kvaser out-of-tree driver scripts blacklists the in-tree SocketCAN driver to prevent it from auto-loading:
http://www.kvaser.com/software/7330130980754/relnotes/relnotes_linuxcan.txt
Quoted 3 lines
This can be the problem. Please try with the mainline drivers only.
Exactly.
Regards,
Darwish
Thanks a lot everyone for your help, it finally works!
The example code output :
can0 at index 4
Wrote -1 bytes
I wish you all the best,
Leo
Marc Kleine-Budde
maintainerQuoted 7 lines · Leo Stan
Thanks a lot everyone for your help, it finally works!
The example code output :
can0 at index 4
Wrote -1 bytes
^^
This doens't look that good. When write() returns -1, something went wrong. Have a look at "errno".
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Actually I spoke too soon...
When I try this simple code from the documentation :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int main(void)
{
int s;
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;
char *ifname = "can0";
if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Error while opening socket");
return -1;
}
strcpy(ifr.ifr_name, ifname);
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Error in socket bind");
return -2;
}
// Reading the CAN port
nbytes=read(s,&frame,sizeof(struct can_frame));
if (nbytes < 0) {
perror("can raw socket read");
return 1;
}
/* paranoid check ... */
if (nbytes < sizeof(struct can_frame)) {
fprintf(stderr, "read: incomplete CAN frame\n");
return 1;
}
printf("dlc = %d, data = %s, %d bytes read\n", frame.can_dlc,frame.data,nbytes);
close(s);
return 0;
}I get this flag :
can0 at index 3
can raw socket read: Network is down
Which means that nothing has been read from the CAN I guess, however I'm sure that data comes from it because I can see it using CANking software on Windows.
When I run "ifconfig -a" RX and TX are also empty:
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Here are the settings of my BUS on Canking :
No init access
Speed : 500000 Bit/s
Bit timing : Q=8, S1=5,S2=3,Sampling point= 62.5%, SJW=1 DLC = 8
Any help would be really appreciated
Thanks for your help Marc.
errno = 100 for both tests, when I try to write and when I try to read.
Marc Kleine-Budde
maintainerQuoted 71 lines · Leo Stan
Actually I spoke too soon...
When I try this simple code from the documentation :
#include <
stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>#include <
net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>#include <
linux/can.h>
#include <linux/can/raw.h>int main(void)
{ int s; int nbytes; struct sockaddr_can addr; struct can_frame frame; struct ifreq ifr; char *ifname = "can0"; if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("Error while opening socket"); return -1; } strcpy(ifr.ifr_name, ifname); ioctl(s, SIOCGIFINDEX, &ifr); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; printf("%s at index %d\n", ifname, ifr.ifr_ifindex); if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind"); return -2; } // Reading the CAN port nbytes=read(s,&frame,sizeof(struct can_frame)); if (nbytes < 0) { perror("can raw socket read"); return 1; } /* paranoid check ... */ if (nbytes < sizeof(struct can_frame)) { fprintf(stderr, "read: incomplete CAN frame\n"); return 1; } printf("dlc = %d, data = %s, %d bytes read\n",frame.can_dlc,frame.data,nbytes);
close(s); return 0; }
Quoted 4 lines
I get this flag :
can0 at index 3
can raw socket read: Network is down
^^^^^^^^^^^^^^^
You have to configure your CAN network device first, then bring it into an operational state, this means the network is "up".
ip link set can0 type can bitrate 500000
ip link set can0 up
See section "6.5.1 Netlink interface to set/get devices properties"
http://lxr.free-electrons.com/source/Documentation/networking/can.txt#L990
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Quoted 13 lines · Marc Kleine-Budde
Quoted 9 lines · Leo Stan
close(s); return 0; }I get this flag :
can0 at index 3
can raw socket read: Network is down^^^^^^^^^^^^^^^
Quoted 9 lines
You have to configure your CAN network device first, then bring it into an operational state, this means the network is "up".
ip link set can0 type can bitrate 500000
ip link set can0 upSee section "6.5.1 Netlink interface to set/get devices properties"
http://lxr.free-electrons.com/source/Documentation/networking/can.txt#L990
Yes.
Stan: please download
https://github.com/linux-can/can-utils
compile and install them (make && make install) or with autotools.
And use 'cansend' and 'candump' to check whether its working.
On recent Debian / Ubuntu you can try
apt-get install can-utils
Programming your own stuff makes sense when you have a working CAN setup.
But when you are fighting on the basic driver side *and* the userspace apps it becomes very hard.
There are also code examples in
https://github.com/linux-can/can-tests
which will help you more than the Wikipedia code sniplet.
And read
https://www.kernel.org/doc/Documentation/networking/can.txt
which Marc already suggested.
Best regards,
Oliver
Thank you all for your help,
I can now successfully read data coming from the Leaf.
I wish you all the best,
Leo