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.

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

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


///BEGIN COMUNICADOR.cpp

#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)
    {
        perror(argv[1]);
        return -2;
    }
    cout << "Abriendo puerto serie " << argv[1] << endl;   
    tcgetattr(fd,&oldtio);
    bzero(&newtio,sizeof(newtio));
    configuraPuerto(&newtio);
    tcflush(fd,TCIFLUSH);
    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;
        while(!STOP)
        {

            strcpy(buffer,"HOLA;");
            write(fd,buffer,6);
            usleep(5000000)        
        }
        //proceso de escritura
    //restablecemos los parametros del puerto
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
    }
    return 0;
}


int configuraPuerto(struct termios *newtio)
{
    //Establecemos los parametros fijos para la comunicacion con Arduino
    //9600 bps, 8N1, Sin control de flujo
    cfsetispeed(newtio,B9600);
    cfsetospeed(newtio,B9600);
    //8N1
    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
    newtio->c_cc[VMIN]=0;
    newtio->c_cc[VTIME]=20;
    return 1;
}

///END COMUNICADOR.cpp

No comments:

Post a Comment