Code Example for ConCAN?

Topics about the Software of Revolution Pi
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Code Example for ConCAN?

Post by KristinH »

Is there a code example for the Connect CAN expansion module.
The datasheet says "Accessible via SocketCAN of the Linux operating system" but under which device name?
User avatar
nicolaiB
KUNBUS
Posts: 1020
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: Code Example for ConCAN?

Post by nicolaiB »

The device is available as can0. You can list the connection parameters with the same tools you would use for an ethernet adapter (eg. ip link show, ...)

Nicolai
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Re: Code Example for ConCAN?

Post by KristinH »

What are the LEDs on the front of the ConCAN module?
Is there a network status LED? As at the moment all I'm getting is network down errors.
If they are user LEDs, what names do we use to access them?
User avatar
nicolaiB
KUNBUS
Posts: 1020
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: Code Example for ConCAN?

Post by nicolaiB »

The ConCan has just a power LED. The other LEDs are not populated (it's a standard enclosure for our extension modules thus all 6 leds are cut out).

How did you try to use the CAN interface?

Nicolai
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Re: Code Example for ConCAN?

Post by KristinH »

I am using these as example code:

https://github.com/craigpeacock/CAN-Examples

I am running my two RevPi's, one with the transmit code, one with the receive code, but both report network down.
When I add in can_get_state() from libsocketcan I get a state of CAN_STATE_STOPPED

Code: Select all

CAN Sockets Transmit Demo
Write: Network is down
bus state is STOPPED

Code: Select all

CAN Sockets Transmit Demo
Write: Network is down
bus state is STOPPED
User avatar
nicolaiB
KUNBUS
Posts: 1020
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: Code Example for ConCAN?

Post by nicolaiB »

This code wont work. It uses hsrd coded vcan adapter and no physical interface.

From the guthub readme:

Hardcoded to use the vcan0 virtual CAN device.
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Re: Code Example for ConCAN?

Post by KristinH »

Of course I have modified it to connect to the "can0" you mentioned above, which is why I asked. (line 29 in receive and line 28 in transmit)

I have now managed to send and receive over CAN by using a combination of command line to set the CAN to UP and the C code above. Having proven the connection I can move on, but it would be nice if there was some documentation for this module beyond how to wire it up.

For anyone else finding this thread:
https://blog.mbedded.ninja/programming/ ... -in-linux/

When I get it all working in C I'll update here.
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Re: Code Example for ConCAN?

Post by KristinH »

All examples I can find online use libsocketcan to interact with the can0 device from within C.

By using the following commands I can get the can0 connection to be seen as "UP" and then the example C code works:

Code: Select all

~$ sudo ip link set can0 type can bitrate 250000
~$ sudo ip link set up can0
However, if I restart the Revpi and attempt to access the equivalent using libsocketcan with C:

Code: Select all

	if (-1 == can_set_bitrate(def_CAN0, def_CAN_SPEED)) {
		perror("can_set_bitrate");
	}

	if (-1 == can_do_start(def_CAN0)) {
		perror("can_do_start");
	}

	if (-1 == can_get_state(def_CAN0, &state)) {
		perror("can_get_state");
	}
then I get the following errors:

Code: Select all

CAN Sockets Receive Demo
RTNETLINK answers: Operation not permitted
can_set_bitrate: Operation not permitted
RTNETLINK answers: Operation not permitted
can_do_start: Operation not permitted
User avatar
nicolaiB
KUNBUS
Posts: 1020
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: Code Example for ConCAN?

Post by nicolaiB »

The permission errors are probably due to missing privileges. Call the program with sudo.
KristinH
Posts: 20
Joined: 01 Nov 2024, 16:01

Re: Code Example for ConCAN?

Post by KristinH »

That explains it.. command line I'm using sudo, cross platform debugging using standard Pi login.

With root login for debugging, those commands now run without error. Still doesn't work yet, but getting closer.
Post Reply