holyhope
Posts: 6
Joined: Wed Feb 17, 2016 7:48 am

Can via C/C++ on Rasp Pi3

Thu Feb 09, 2017 3:25 pm

Hi to all,
I'm new to Raspberry Pi3, and I need to implement a CAN protocol developing with NetBeans 8.2
I know CAN protocol and I have already use it in several embedded uP (Pic16, Pic32,etc..)
I know a little of linux but I'm not an expert

I buy this board:
http://skpang.co.uk/catalog/pican2-canb ... -1475.html

And i configure CAN over SPI in this way:
http://skpang.co.uk/blog/archives/1165

I try to sniff several can packet (of other device) with wireshark, how write in this link:
http://skpang.co.uk/blog/archives/1141
and I sniff it very well!

So I try do download an example program and i found this:
http://skpang.co.uk/blog/archives/1199
it works and compile!

Now I'm creating a new "hello word" c program in CAN, starting from the example program downloaded before (that:
http://skpang.co.uk/blog/archives/1199 ) but I found several problems...

I add to my project only lib.h for compilem after that I Import all .h file for have the reference of the function inside my code. (I'm developing on NetBeans 8.2)
In the example code, references that are missing are this: setsockopt, ioctl, socket used in this way:

if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
socket(PF_CAN, SOCK_RAW, CAN_RAW))
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);

[Question-1] So the first question is where I can find them?!

So I can study this function and learn how to use it...

[Question-2] How to read a can message? In the example is not explaned (and no example provided for reading) how to read a can packet.... I have no idea how to do it with this library... I suppose I must set up an interrupt (or something similar...) where a new packet is aviable and a call to function (but I have no idea what function) for read packet.

Can someone help me?
Many thanks!!!!
Max

User avatar
PeterO
Posts: 5003
Joined: Sun Jul 22, 2012 4:14 pm

Re: Can via C/C++ on Rasp Pi3

Thu Feb 09, 2017 3:36 pm

Use the "man" command to look up the manual pages for these library functions. The pages tell you which header files need including to use the functions.

Code: Select all

IOCTL(2)                   Linux Programmer's Manual                  IOCTL(2)

NAME
       ioctl - control device

SYNOPSIS
       #include <sys/ioctl.h>        <---------------------  LOOK HERE 

       int ioctl(int d, int request, ...);

Code: Select all

SOCKET(2)                  Linux Programmer's Manual                 SOCKET(2)

NAME
       socket - create an endpoint for communication

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */             <---------------------  LOOK HERE 
       #include <sys/socket.h>                                     <---------------------  LOOK HERE 

       int socket(int domain, int type, int protocol);

Code: Select all

GETSOCKOPT(2)              Linux Programmer's Manual             GETSOCKOPT(2)

NAME
       getsockopt, setsockopt - get and set options on sockets

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */                      <---------------------  LOOK HERE 
       #include <sys/socket.h>                                              <---------------------  LOOK HERE 

       int getsockopt(int sockfd, int level, int optname,
                      void *optval, socklen_t *optlen);
       int setsockopt(int sockfd, int level, int optname,
                      const void *optval, socklen_t optlen);

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

holyhope
Posts: 6
Joined: Wed Feb 17, 2016 7:48 am

Re: Can via C/C++ on Rasp Pi3

Thu Feb 09, 2017 3:56 pm

Hi PeterO

Many thanks for your answer... I have not thinked that those instruction was system library...
Now on my "helloword.c" file in netbeans I have write that include but netbeans still not reference them with CTRL+D (navigate to definition, or to header , etc.)
Now I read the manual with the man command and search something usefull about it!!!

For "read" packet in can bus do you know any example using that library?

Very many thanks!
Max

User avatar
PeterO
Posts: 5003
Joined: Sun Jul 22, 2012 4:14 pm

Re: Can via C/C++ on Rasp Pi3

Thu Feb 09, 2017 4:09 pm

SKPang has links to examples on his web page for the PiCAN2 board
http://skpang.co.uk/catalog/pican2-canb ... -1475.html

I used the source code from linux-can-utils as the starting point for my own applications.
See http://skpang.co.uk/blog/archives/1199 for download instructions.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

holyhope
Posts: 6
Joined: Wed Feb 17, 2016 7:48 am

Re: Can via C/C++ on Rasp Pi3

Thu Feb 09, 2017 4:29 pm

Hi PeterO
Yes I started from it cantest.c file to build my own application!
what I not found on this example is read from can bus. Maybe I'm wrong: I will analyze more deeper all souce code.

I will keep you informed

many thanks and have a nice day!
Max

marcellatwin
Posts: 34
Joined: Sat Jan 27, 2018 6:41 pm
Location: USA

Re: Can via C/C++ on Rasp Pi3

Thu Jun 06, 2019 11:58 am

Not to resurrect an old thread, but in searching for a different but similar issue I came across this. Are you still having issues "reading" can bus messages? Cause I can totally help with that.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23631
Joined: Sat Jul 30, 2011 7:41 pm

Re: Can via C/C++ on Rasp Pi3

Thu Jun 06, 2019 12:57 pm

marcellatwin wrote:
Thu Jun 06, 2019 11:58 am
Not to resurrect an old thread, but in searching for a different but similar issue I came across this. Are you still having issues "reading" can bus messages? Cause I can totally help with that.
Except....you ressurected an old thread and the user in question hasn't logged in for two years...

Will leave open, just in case.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Return to “C/C++”