Koala Library
koala_serial.c
Go to the documentation of this file.
00001 //--------------------------------------------------------------------------------//
00002 //-                   KOALA( Koala extension board                       )                       -//
00003 //                                                                               -//
00004 //-  Copyright (C) Julien Tharin, K-Team S.A. 2013                               -//
00005 //-  This library is free software; you can redistribute it and/or               -//
00006 //-  modify it under the terms of the GNU Lesser General Public                  -//
00007 //-  License as published by the Free Software Foundation; either                -//
00008 //-  version 2.1 of the License, or any later version.                           -//
00009 //-                                                                              -//
00010 //-  This library is distributed in the hope that it will be useful,             -//
00011 //-  but WITHOUT ANY WARRANTY; without even the implied warranty of              -//
00012 //-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           -//
00013 //-  Lesser General Public License for more details.                             -//
00014 //-                                                                              -//
00015 //-  You should have received a copy of the GNU Lesser General Public            -//
00016 //-  License along with this library; if not, write to the Free Software         -//
00017 //-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   -//
00018 //-                                                                              -//
00019 //-                               __  __  ________                               -//
00020 //- K-Team S.A.                  |  |/  /|__    __|___  _____  ___  ___          -//
00021 //- Chemin des Plans-Praz 28,    |     / __ |  | _____|/  _  \|   \/   |         -//
00022 //- 1337 Vallorbe                |  |  \    |  | ____|/  /_\  |        |         -//
00023 //- Switzerland                  |__|\__\   |__|______|_/   \_|__|\/|__|         -//
00024 //- jtharin@k-team.com   tel:+41 24 423 89 56 fax:+41 24 423 8960                -//
00025 //-                                                                              -//
00026 //--------------------------------------------------------------------------------//
00027 
00040 #include "koala.h"
00041 #include "koala_serial.h"
00042 
00043 
00044 #include <stdlib.h>
00045 #include <fcntl.h>
00046 #include <sys/stat.h>
00047 #include <termio.h>
00048 
00049 #define LineLength 256
00050 
00051 /*--------------------------------------------------------------------*/
00063 koala_rs232_t * koala_rs232_open( 
00064                                             const char * name, int baudrate)
00065 {
00066   koala_rs232_t * rs232;
00067   int fd;
00068  
00069   if ((fd=open(name,O_RDWR | O_NOCTTY | O_NONBLOCK))<0) {
00070     return NULL;
00071   }
00072 
00073   if (baudrate==0)
00074         {
00075                 baudrate = B115200;
00076         }
00077 
00078   rs232 = malloc(sizeof(koala_rs232_t));
00079 
00080   fcntl(fd, F_SETFL, 0); // blocking input
00081 
00082   rs232->fd = fd;
00083 
00084   tcgetattr(fd , &rs232->tios);
00085   rs232->tios.c_cflag = ( CS8 | CLOCAL | CREAD | baudrate );
00086   rs232->tios.c_iflag = IGNPAR;
00087   rs232->tios.c_oflag = 0;
00088   //rs232->tios.c_lflag =  ICANNON;
00089   rs232->tios.c_lflag = 0;
00090   rs232->tios.c_cc[VMIN] = 1;
00091   rs232->tios.c_cc[VTIME] = 1;
00092   tcflush(fd, TCIFLUSH);
00093   tcsetattr( fd , TCSANOW , &rs232->tios );
00094   
00095   
00096   return (rs232);
00097 }
00098 
00099 /*--------------------------------------------------------------------*/
00106 void koala_rs232_close(koala_rs232_t * rs232 )
00107 {
00108 
00109   if ( rs232 != NULL ) {
00110 
00111     if ( rs232->fd != -1 ) 
00112       close(rs232->fd );
00113    
00114      free( rs232 );
00115   }
00116 }
00117 
00118 /*--------------------------------------------------------------------*/
00132 int koala_rs232_read(koala_rs232_t * rs232,
00133                             char * buf , unsigned int len )
00134 {
00135   if ( rs232 != NULL && rs232->fd != -1 )
00136     return read( rs232->fd , buf , len );
00137 
00138   return -1;
00139 }
00140 
00141 
00142 /*--------------------------------------------------------------------*/
00143 /*
00152 int koala_rs232_readLine_nowait(koala_rs232_t * rs232, char *buffer) {
00153 
00154   int i;
00155   struct timeval Timeout;
00156   int res; 
00157   fd_set rfds;
00158   
00159   
00160   if ( rs232 == NULL || rs232->fd == -1 )
00161     return -1;
00162   
00163         FD_ZERO(&rfds);
00164                 FD_SET(rs232->fd, &rfds);
00165                                                                                           
00166          // set timeout value                          
00167                 Timeout.tv_usec = 0;  // microseconds                           
00168                 Timeout.tv_sec  = 0;  // seconds                               
00169                 res = select(rs232->fd+1, &rfds, NULL, NULL, &Timeout);
00170 
00171   if (res<=0) // error or timeout
00172                  return -2;
00173   
00174   for (i = 0; i < LineLength -1; ++i) {
00175     char recv_ch;
00176     
00177                       
00178 
00179                 
00180     
00181     int n = read(rs232->fd, &recv_ch, 1);// com_recv(&recv_ch, 1, Timeout);
00182     if (n <= 0) {
00183       if (i == 0) {
00184                                 return -3;              // timeout
00185       }
00186       break;
00187     }
00188     if ((recv_ch == '\r') || (recv_ch == '\n')) {
00189       break;
00190     }
00191     buffer[i] = recv_ch;
00192   }
00193   buffer[i] = '\0';
00194 
00195         
00196         
00197   return i;
00198 }
00199 
00200 /*--------------------------------------------------------------------*/
00201 /*
00210 int koala_rs232_readLine(koala_rs232_t * rs232, char *buffer) {
00211 
00212   int i;
00213   struct timeval Timeout;
00214   int res; 
00215   fd_set rfds;
00216   
00217   
00218   if ( rs232 == NULL || rs232->fd == -1 )
00219     return -1;
00220   
00221         FD_ZERO(&rfds);
00222                 FD_SET(rs232->fd, &rfds);
00223                                                                                           
00224          // set timeout value                          
00225                 Timeout.tv_usec = 500000;  // microseconds                           
00226                 Timeout.tv_sec  = 0;  // seconds                               
00227                 res = select(rs232->fd+1, &rfds, NULL, NULL, &Timeout);
00228 
00229   if (res<=0) // error or timeout
00230                  return -2;
00231   
00232   
00233   for (i = 0; i < LineLength -1; ++i) {
00234     char recv_ch;
00235     
00236            
00237     int n = read(rs232->fd, &recv_ch, 1);// com_recv(&recv_ch, 1, Timeout);
00238     if (n <= 0) {
00239       if (i == 0) {
00240                                 return -3;              // timeout
00241       }
00242       break;
00243     }
00244     if ((recv_ch == '\r') || (recv_ch == '\n')) {
00245       break;
00246     }
00247     buffer[i] = recv_ch;
00248   }
00249   buffer[i] = '\0';
00250 
00251         
00252         
00253   return i;
00254 }
00255 
00256 /*--------------------------------------------------------------------*/
00271 int koala_rs232_write(koala_rs232_t * rs232 ,
00272                            const char * buf , unsigned int len )
00273 {
00274   if ( rs232 != NULL && rs232->fd != -1 )
00275     return write( rs232->fd , buf , len );
00276 
00277   return -1;
00278 }