Arduino Boards

From RobolaboWiki
Jump to: navigation, search

Serial ports on the Arduino Due

Native port Serial USB

The Native USB port is connected directly to the SAM3X MCU.

The USB connector of the Native port is directly connected to the USB host pins of the SAM3X. Using the Native port enables you to use the Due as a client USB peripheral (acting as a mouse or a keyboard connected to the computer) or as a USB host device so that devices can be connected to the Due (like a mouse, keyboard, or an Android phone). This port can also be used as a virtual serial port using the "SerialUSB" object in the Arduino programming language. It is not necessary set a baud rate because Arduino Due will offer the maximum available.

Programming port Serial

The Programming port is connected to an ATMEL 16U2 which acts as a USB-to-Serial converter. This Programming port is the default for uploading sketches and communicating with the Arduino.

The USB-to-serial converter of the Programming port is connected to the first UART of the SAM3X. It's possible to communicate over this port using the "Serial" object in the Arduino programming language.


Automatic (Software) Reset

Native port

To use the serial monitor, and see what your sketch does from the beginning, you'll need to add few lines of code inside the setup(). This will ensure the SAM3X will wait for the SerialUSB port to open before executing the sketch:

 
while (!Serial);

Pressing the Reset button on the Due causes the SAM3X reset as well as the USB communication. This interruption means that if the serial monitor is open, it's necessary to close and reopen it to restart the communication.

Programming port

The USB-to-serial chip has two pins connected to the Reset and Erase pins of the SAM3X. When you open this serial port, the USB-to-Serial activates the Erase and Reset sequence before it begins communicating with the UART of the SAM3X.

The Programming port resets the board each time you open the serial monitor (or any other serial communication).


(More info at: https://www.arduino.cc/en/Guide/ArduinoDue)

Communication Arduino Due - PC(Linux). Native Port

First of all is necessary include the following libraries in your .c file:

#include <stdio.h>   /* Standard input/output definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <string.h>  /* String function definitions */

To send information from the Arduino Due to the PC using the Native port, you need write:

SerialUSB.println("String");

where String is the information that you want to transmit.

To receive this information in your PC is neccesary open the USB port

int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY);

if (fd == -1)
 /* Could not open the port*/
 perror("open_port: Unable to open /dev/ttyACM0");
else 
 fcntl(fd, F_SETFL, 0);

By default the connection between Arduino Due and PC is performed thought ACM0 port, if this port is already in use you must see the active port using in the terminal the command:

 ll /dev/ttyACM*

When you have the connection activated, the next step is read the bytes transmitted:

int nbytes = read(fd, &buffer, sizeof(buffer));    

if (n == -1)
 perror("No bytes received");
else
 printf("%s\n", buffer);

Now you have the String transmitted stored in the array of char "buffer" and the number of bytes received stored in "nbytes".

Communication PC(Linux) - Arduino Due. Native Port

In this case you need write the information to transmit in the USB port from the PC:

int nbytes = write(fd, &buffer, sizeof(buffer));

if (n < 0)
 perror("write() of bytes in USB port failed!");

Then in Arduino you have to read the available information:

if (SerialUSB.available() > 0)
 SerialUSB.readBytes(buffer, nbytes);

You need specify the number of bytes you want to receive "nbytes", for a correct transmission the number of bytes transmitted and receive must be the same.