Main Page | Namespace List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

os_dep_serial.c

Go to the documentation of this file.
00001 /*  Implémentation Linux de la bibliotheque XADLL de Sidena
00002  *  Copyright (C) 2005 ENSTAR
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *  You can contact us:
00019  *    Association ENSTAR
00020  *    Ecole Nationale Superieure de Techniques Avancees
00021  *    32 boulevard Victor
00022  *    75739 Paris Cedex 15
00023  *    FRANCE
00024  *
00025  *    enstar@ensta.fr
00026  */
00027 
00028 /** @file    os_dep_serial.c
00029  *  @brief   Bibliothèque de fonctions de communication avec le port série
00030  *  @author  Francois Fevotte <francois.fevotte@ensta.org>
00031  *  $Revision: 60 $
00032  *  $Date: 2006-03-16 16:55:36 +0100 (Thu, 16 Mar 2006) $
00033  *
00034  *  Ce code implémente les fonctions de communications spécifiques à la liaison série (sous LINUX).
00035  */
00036 
00037 /* $Id: os_dep_serial.c 60 2006-03-16 15:55:36Z francois $ */
00038 
00039 #define OS_DEP_USB_C
00040 
00041 #include <unistd.h>
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <fcntl.h>
00045 #include <time.h>
00046 #include <termios.h>
00047 #include <sys/ioctl.h>
00048 #include <string.h>
00049 #include "xalib.h"
00050 #include "os_dep_serial.h"
00051 #include "statique.h"
00052 #include <errno.h>
00053 
00054 /* Gestion des erreurs */
00055 #define SOURCE_ERREUR "os_dep_serial"
00056 #ifdef DEBUG
00057 #define SCREEN_LEVEL 3 // messages de warning a l'ecran
00058 #else
00059 #define SCREEN_LEVEL 2 // pas de messages de warning
00060 #endif
00061 #include "erreur.h"
00062 
00063 /* Variables globales */
00064 char XA_comm_init[2] = {0,0}; /* Les ports séries sont-ils initialisés ? */
00065 int  XA_curr_comm;            /* No du port série courant                */
00066 int  XA_comm_port[2];         /* Descripteurs des ports séries ouverts   */
00067 
00068 int serial::open(char serial)
00069 {
00070   struct termios ios;
00071   char port[32];
00072   int line_status = 0;
00073 
00074   strcpy( port, "/dev/ttyS0" );
00075 
00076   coderr = 0;
00077 
00078   switch (serial)
00079     {
00080     case 1 :
00081       port[9] = '0';
00082       if( XA_comm_init[0] ) /* Le port a déjà été initialisé */
00083         {
00084           coderr = 8;
00085           erreur::warning( "open : Le port a déjà été initialisé" );
00086           return 1;
00087         }
00088       break;
00089     case 2:
00090       port[9] = '1';
00091       if( XA_comm_init[1] ) /* Le port a déjà été initialisé */
00092         {
00093           coderr = 8;
00094           erreur::warning( "open : Le port a déjà été initialisé" );
00095           return 1;
00096         }
00097       break;
00098     default :
00099       coderr = 12;
00100       erreur::fatale( "open : Mauvais port série" );
00101       return 1;
00102     }
00103   
00104   /* Sélection du port courant */
00105   XA_curr_comm = serial-1;
00106 
00107   /* Ouverture du port */
00108   XA_comm_port[XA_curr_comm] = ::open( port, O_RDWR | O_NOCTTY | O_NDELAY );
00109   if( XA_comm_port[XA_curr_comm] == -1 )
00110     {
00111       coderr = 7;
00112       erreur::fatale( "open : erreur à l'ouverture du port" );
00113       return 1;
00114     }
00115   erreur::debug( "Port série ouvert" );
00116 
00117   fcntl(XA_comm_port[XA_curr_comm],F_SETFL,0); // comme l'ENISE...
00118 
00119   /* Configuration des paramètres du port */
00120   coderr = tcgetattr( XA_comm_port[XA_curr_comm], &ios);
00121 
00122   ios.c_cflag &= ~(PARENB| CBAUD |CSIZE | CRTSCTS);
00123   ios.c_cflag |= (CS8 | CSTOPB | CREAD | CLOCAL);
00124 
00125   ios.c_iflag = IGNBRK | IGNPAR;
00126 
00127   ios.c_oflag = 0;
00128   ios.c_lflag = 0;
00129 
00130   ios.c_cc[VTIME] = 0; // intercharacter timer unused
00131   ios.c_cc[VMIN]  = 0; // blocking read until 1 character is received
00132 
00133   coderr |= cfsetospeed( &ios, B115200 );
00134   coderr |= cfsetispeed( &ios, B115200 );
00135 
00136   coderr |= tcsetattr( XA_comm_port[XA_curr_comm], TCSANOW, &ios );
00137 
00138   /* Configuration de l'état des pins */
00139   coderr |= ioctl( XA_comm_port[XA_curr_comm], TIOCMGET, &line_status );
00140   line_status &= ~TIOCM_DTR; /* DTR bas  */
00141   line_status |= TIOCM_RTS;  /* TCS haut */
00142   coderr |= ioctl( XA_comm_port[XA_curr_comm], TIOCMSET, &line_status );
00143 
00144   if( coderr != 0 )
00145     {
00146       /* Une erreur s'est produite lors de l'initialisation */
00147       coderr = 7;
00148       erreur::fatale( "open : erreur de configuration du port" );
00149       return 1;
00150     }
00151   
00152   /* Tout s'est bien passé. */
00153   XA_comm_init[XA_curr_comm]=1;
00154   erreur::debug( "Port série initialisé" );
00155   return 0;
00156 }
00157 
00158 void serial::close()
00159 {
00160   if( XA_comm_init[0] )  /* Le port 1 est ouvert */
00161     {
00162       ::close( XA_comm_port[0] );
00163       XA_comm_init[0] = 0;
00164     }
00165   if( XA_comm_init[1] )  /* Le port 2 est ouvert */
00166     {
00167       ::close( XA_comm_port[1] );
00168       XA_comm_init[1] = 0;
00169     }
00170   erreur::debug( "Ports série fermés" );
00171 }
00172 
00173 int serial::write(short length)
00174 {
00175   if( XA_curr_comm>1 || XA_curr_comm<0 || !XA_comm_init[XA_curr_comm] )
00176     {
00177       erreur::fatale( "write : Port série non initialisé" );
00178       return 0;
00179     }
00180 
00181   /* Flush du port */
00182   ioctl( XA_comm_port[XA_curr_comm], TCFLSH, TCIFLUSH );
00183 
00184   /* Ecriture du buffer */
00185   ::write( XA_comm_port[XA_curr_comm], Out_buffer, length );
00186 
00187 #ifdef DEBUG
00188   {
00189     char message1[1024];
00190     char message2[1024];
00191     int n,i;
00192 
00193     n=snprintf(message1,1023,"%d caractères envoyés :",length );
00194     snprintf(message2,1023,"%d caractères envoyés :",length );
00195     for(i=0 ; i<length && n<1024 ; i++)
00196       {
00197         snprintf(message1+n,1023-n, " %03d",Out_buffer[i]);
00198         snprintf(message2+n,1023-n,"  %02X",Out_buffer[i]);
00199         n+=4;
00200       }
00201     erreur::debug( message1 );
00202     erreur::debug( message2 );
00203   }
00204 #endif
00205 
00206   return 1;
00207 }
00208 
00209 int serial::read(unsigned int *somme)
00210 {
00211   int lu=0;
00212   unsigned char c;
00213   fd_set fd;
00214   struct timeval tv;
00215 
00216   if( XA_curr_comm>1 || XA_curr_comm<0 || !XA_comm_init[XA_curr_comm] )
00217     {
00218       erreur::fatale( "read : Port série non initialisé" );
00219       return 0;
00220     }
00221 
00222   FD_ZERO( &fd );
00223   FD_SET( XA_comm_port[XA_curr_comm], &fd );
00224 
00225   while( 1 )
00226     {
00227       int r;
00228 
00229       /* Timeout : 1sec */
00230       tv.tv_sec  = 1;
00231       tv.tv_usec = 0;
00232       
00233       if( !select( 1+XA_comm_port[XA_curr_comm], &fd, NULL, NULL, &tv ) )
00234         {
00235           char message[1024];
00236           snprintf(message,1023,"read : TIMEOUT ! %d caracteres lus",lu);
00237           erreur::warning(message);
00238           break;
00239         }
00240 
00241       /* data are available on the serial port */
00242       r = ::read( XA_comm_port[XA_curr_comm], &c, 1 );
00243       if( r != -1 ) /* all went well */
00244         {
00245           Rec_buffer[XA_lastcar_in]+=c;
00246           if(c!=26) // on a lu un vrai caractere (i.e. pas de transcodage)
00247             {
00248               if(c==30) // le message est termine
00249                 {
00250                   XA_lastcar_in++;
00251                   lu ++;
00252                   break;
00253                 }
00254               else // caractere normal
00255                 {
00256                   (*somme)+=Rec_buffer[XA_lastcar_in];
00257                   XA_lastcar_in++;
00258                   lu ++;
00259                 }
00260             }
00261         }
00262       else /* read failed */
00263         {
00264           if( errno != EAGAIN ) /* big problem */
00265             {
00266               char err[1024];
00267               snprintf( err, 1023, "read : read failed (%d)", errno );
00268               erreur::fatale( err );
00269               break;
00270             }
00271         }
00272     }
00273 
00274 #ifdef DEBUG
00275  {
00276    char message1[1024];
00277    char message2[1024];
00278    int n,i;
00279 
00280    n=snprintf(message1,1023,"%d caractères lus: ", lu);
00281    snprintf(message2,1023,"%d caractères lus: ", lu);
00282    for(i=0 ; i<XA_lastcar_in && n<1024 ; i++)
00283      {
00284        snprintf(message1+n,1023-n, " %03d",Rec_buffer[i]);
00285        snprintf(message2+n,1023-n,"  %02X",Rec_buffer[i]);
00286        n+=4;
00287      }
00288    erreur::debug( message1 );
00289    erreur::debug( message2 );
00290  }
00291 #endif
00292 
00293   return lu;
00294 }

Project hosted on SourceForge.net
Documentation generated on Mon Apr 2 14:52:55 2007 for libXA by doxygen 1.4.4