I just use USB to USB C to connect my jetson nano and arduino due and it works perfectly, just had to make the ttyACM0 linked to arduino by using udev rules files but you shouldn’t need that :D
Be sure to do : sudo apt-get install libusb-1.0
You can use : dmesg | grep ttyACM
or usb-devices
to find your arduino if wired on USB.
Here is my code (I tried a lot of ways and this is the most robust for nano<->arduino UART) :
Initialization :
tcgetattr(due, &port_options); // Get the current attributes of the Serial port
due = open("/dev/ttyACM0", O_RDWR | O_NONBLOCK);//O_NOCTTY);//
tcflush(due, TCIFLUSH);
tcflush(due, TCIOFLUSH);
if (due == -1){perror("Could not open Arduino");ArduinoSerial=false;getdue::PreDue();}
else{
ArduinoSerial=true;
port_options.c_cflag &= ~PARENB; // Disables the Parity Enable bit(PARENB),So No Parity
port_options.c_cflag &= ~CSTOPB; // CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit
port_options.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
port_options.c_cflag |= CS8; // Set the data bits = 8
port_options.c_cflag &= ~CRTSCTS; // No Hardware flow Control
port_options.c_cflag |= CREAD | CLOCAL; // Enable receiver,Ignore Modem Control lines
port_options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable XON/XOFF flow control both input & output
port_options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Non Cannonical mode
port_options.c_oflag &= ~OPOST; // No Output Processing
port_options.c_lflag = 0; // enable raw input instead of canonical,
port_options.c_cc[VMIN] = VMINX; // Read at least 1 character
port_options.c_cc[VTIME] = 0; // Wait indefinetly
cfsetispeed(&port_options,BAUDRATE); // Set Read Speed
cfsetospeed(&port_options,BAUDRATE); // Set Write Speed
int att = tcsetattr(due, TCSANOW, &port_options);
if (att != 0 ){printf("ERROR in Setting Arduino port attributes");}
else{printf("SERIAL DUE Port Good to Go.\n");}
tcflush(due, TCIFLUSH);
tcflush(due, TCIOFLUSH);
//getdue::ReadDue();
}
To read from arduino :
memset(serial_message, 0, 255);
tcflush(due, TCIOFLUSH);
rx_length = read(due, (void*)rx_buffer, VMINX);
serial_message[nread] = rx_buffer[0];
printf("Event %d, rx_length=%d, Read=%s\n", nread+1, rx_length, rx_buffer );
To write to arduino :
p_tx_buffer = &tx_buffer[0];
for(int i=0;i<(int)arduinoIn.length();i++){
*p_tx_buffer++ = arduinoIn[i];
}
if (due != -1){
count = write(due, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0){perror("Arduino UART TX error\n");}
}
Hope I could save you some time :)
Regards,
Planktos