Monday, June 17, 2013

Still more about serial connection

Serial connection at the cubieboard side

As we see at the previous post (More about serial connection) I've done the serial part at the arduino's side, but what happen at the cubieboard side.

At the cubieboard side we have to make a program quite similar, which should open the serial connection, and take care about send and receive the data.

This is the simplest version that I made, for sure it has mistakes, and it could be made better, but is just for a demo proposal

There are a lot of documentation in internet better than mine.
The pseudocode should be somthing like this.

 open port (/dev/ttyXXX)
 configure port for arduino
 bifurcate (fork())
 if (son) read until exit
 if (father) write until exit

And with this I've to improve more about my own protocol.
I don't like to put all the code but here it's (with out any warranty :))


#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>

#define MAXBUFFER 255

using namespace std;

int configuraPuerto(struct termios *newtio);//configure the port to communicate with arduinos

int main (int argc, char **argv)
    int fd;//descriptor del fichero
    struct termios oldtio, newtio;
    int pid ; //para bifurcar el proceso
    bool STOP=false;

    char buffer[MAXBUFFER]; //tamano maximo para un buffer serie
    if (argc !=2)//comprobar que recibimos los parametros necesarios
        cout << "Uso: " << argv[0] << " /dev/ttyXXX" << endl;       
        return -1;
    //Abrir el puerto
    fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd <0)
        return -2;
    cout << "Abriendo puerto serie " << argv[1] << endl;   
    if (tcsetattr(fd,TCSANOW,&newtio) <0)
        perror ("No se han podido establecer los atributos del puerto \n");
        return -3;
    cout << "Puerto serie configurado para arduino" << endl;
    pid = fork();
    if (pid < 0)
        perror("Error en fork()");
        return -4;
    cout << "Bifurcacion creada" << endl;
    if (pid ==0) //proceso hijo
        int res=0;//bytes devueltos en la lectura
        cout << "Proceso hijo (lectura)" << endl;
        //Bucle de lectura
        while (!STOP)
            res = read (fd, buffer,MAXBUFFER);
            buffer[res]=0;//Terminacion de la cadena recibida
            if (res >0)
                cout << buffer << endl;
                if (buffer[res -1]==';') STOP = true;
        //saliendo del hijo
       cout << "Saliendo del proceso hijo (lectura)" << endl;
    if (pid >0)//proceso padre
        cout << "Proceso padre (escritura)" << endl;

        //proceso de escritura
    //restablecemos los parametros del puerto
    return 0;

int configuraPuerto(struct termios *newtio)
    //Establecemos los parametros fijos para la comunicacion con Arduino
    //9600 bps, 8N1, Sin control de flujo
    newtio->c_cflag &= ~PARENB;
    newtio->c_cflag &= ~CSTOPB;
    newtio->c_cflag &= ~CSIZE;
    newtio->c_cflag |= CS8;
    //sin control del flujo
    newtio->c_cflag &= ~CRTSCTS;
    //ignoramos las lineas de control
    newtio->c_cflag |= CLOCAL | CREAD;
    newtio->c_iflag &= ~(IXON | IXOFF | IXANY);
    //hacemos la comunicacion en modo raw
    newtio->c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    //establecemos los tiempos de comunicacion
    return 1;


More about serial connection

The importance of communication

Our cubieboard have enough connection to control almost of our sensors, using I2C.
But in my case I'll have a few more analogical sensors, and I want to avoid that, and the cubieboard doesn't have to work with the simplest things.

For this, my cubieboard will be a brain, the arduino will be then the senses, and the connection will be made over a serial connection
I could make using I2C but the arduino is working with it as master for the sensors, and could get more complicated. (by the moment :) )

The arduino part.(Serial vs SoftwareSerial)

Arduino has a serial port inside of it, you can access through the USB, or the digital pins (Rx-0,Tx-1).
This option gave me two problems.

1º The USB - Serial driver (ftdi_sio) does not exit in my version of raspbian (Linux raspberrypi 3.4.24-a10-aufs+), and it's necessary to upgrade (I made it, but also it's necessary to install again the OpenCV, lib-jpeg-turbo, etc. This option is completely necessary if you don't want to work with arduinos sketches (to slow :( at the cubieboard )

2º The other issue is more a problem of design. I'm learning who to make drones, and if I want to debug the arduinos part and don't worry about the cubieboard, I should to make through the USB serial, but if it's used by the cubieboard, I could'nt have access to it.

Arduino give us a solution, and it's to use a software serial.
It's possible to use two different digital pin to serial communication.
Here it's an useful example (

Here it's the schema of connection, (quite simple)
The serial connection send each byte alone, is oriented to a character, so we have to work in our protocol.
Here it's a code it's a modification of the arduino example.
///BEGIN .ino
#include <SoftwareSerial.h>

//Define for the new pins for the Software-serial
#define rxPin 2
#define txPin 3

String buffer="";// In this buffer I'll receive the commands

// Create a new serial port
SoftwareSerial miSerie =  SoftwareSerial(rxPin, txPin);
void setup() 
  Serial.println("USB SERIAL UP");
  // Stablish the serial port
  miSerie.println("SOFT SERIAL UP");

void loop()
//isBufferComplete is to retrieve complet commands
  if (isBufferComplete())//this function read the date from my serial (softserial)
  //From the arduino to the cubieboard (throught SoftSerial)
  if (Serial.available())//Just read from the USB serial

//This function read from the softserial
boolean isBufferComplete()
  //In this function we recieve each caracter till
  // arrive ";" wich means end of command
  boolean completa =false;
  while (miSerie.available())
    char inChar = (char);
    buffer += inChar;
    if (inChar ==';') completa=true;
  return completa;
///END .ino
note: SerialEvent from the arduino, doesn't works with software-serial

At the next post will be the code for the cubieboard.