CAN communication write() function

Hi all,

I am trying to send data from CAN0 to CAN1 in NVIDIA Jetson TX2. I have followed the following steps:

  1. sudo modprobe can
  2. sudo modprobe can_raw
  3. sudo modprobe can_dev
  4. sudo modprobe mttcan
  5. ifconfig

This shows CAN0 and CAN1 interfaces are up. Data is transferred successfully by terminal using command
cansend can0 123#abcdabcd. Now, I have written a small program .c program for same as follows:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>

void main()
{ char buf[20];
size_t nbytes;
ssize_t bytes_written;
int fd;

strcpy(buf, “This is a test\n”);
nbytes = strlen(buf);

bytes_written = write(fd, buf, nbytes);

printf(“%d”,bytes_written);
}

OUTPUT : -1

I don’t want the write function to return -1. what could go wrong?

This post really belongs on one of the programming boards, but we’d recommend creating the file descriptor and setting ‘fd’ with this value before attempting to use it in write()