CAN communication CAN0 to CAN1

Hi all,

I am executing the below CAN communication program in NVIDIA Jetson TX2 and getting error in read command. Kind Request to help me:

/*************************************************************************************************************/
#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;

const 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;
}

frame.can_id  = 0x123;
frame.can_dlc = 2;
frame.data[0] = 0x11;
frame.data[1] = 0x22;

nbytes = write(s, &frame, sizeof(struct can_frame));

printf("Wrote %d bytes\n", nbytes);
    
    nbytes = read(s, &frame, sizeof(struct can_frame));
    printf("%d", nbytes);

return 0;

}

OUTPUT:
can0 at index 6
Wrote 16 bytes

/***********************************************************************************************************/
Above is the OUTPUT and after that the program just stops, probably there is some error in read() function?

Hi rakshit,
I would like to know which all connections did you make in Jetson TX2? Lokkslike you are writing to can0 and reading from can0 only. After knowing, I would be able to help you further.

Thanks,
Shubhi

did you fix it ?

I am new to this arena, but well experienced in CAN.
I can see your program is sending and not waiting at all.
the transfer from the Can buffer to the wire takes some time, and you are not waiting for the message to finish sending.

once is it totally sent, the receiver should have it instantly.
but you wont be receiving into Can0, unless you have some 'echo; system externally, which is unusual.
can you wire in the Can1 receiver ?

how did you connect the Canbus to your terminal ? do you have a dongle? I use the CanDo unit.
assuming you have a 120R terminator on the wire, it is necessary for Canbus.

You should work toward a more readable code. Please adjust the ‘s’(Socket) to CanB0Socket makes it more readable.