Difference between revisions of "Arduino Connections"

From RobolaboWiki
Jump to: navigation, search
Line 44: Line 44:
 
  */
 
  */
  
  perror("open_port: Unable to open /dev/ttyCOM0 - ");
+
  perror("open_port: Unable to open /dev/ttyACM0 - ");
 
}else{
 
}else{
 
  fcntl(fd, F_SETFL, 0);
 
  fcntl(fd, F_SETFL, 0);
Line 53: Line 53:
 
  ll /dev/ttyACM*
 
  ll /dev/ttyACM*
 
</pre>
 
</pre>
 +
<p>Next read the bytes transmitted:</p>
 +
<pre>
 +
int nbytes = read(fd, &buffer, sizeof(buffer));   
 +
if (n == -1){
 +
perror("No bytes");
 +
} else{
 +
printf("%s\n", buffer);
 +
}
 +
</pre>
 +
<p>Now you have the String transmitted stored in the array of char "buffer".</p>

Revision as of 12:03, 26 April 2017

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 maximun baud rate.

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);
<p>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).

Communication Arduino Due-PC. Native Port

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);

/* wait for the Arduino to reboot */
usleep(500000);

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 COM0 port, if this port is already in use you must see the active port using the command:

 ll /dev/ttyACM*

Next read the bytes transmitted:

int nbytes = read(fd, &buffer, sizeof(buffer));    
if (n == -1){
 perror("No bytes");
} else{
 printf("%s\n", buffer);
}

Now you have the String transmitted stored in the array of char "buffer".