00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00031 #include <class_dioc.h>
00032 
00033 class_dioc::class_dioc(const char*pdevice)
00034 {
00035         
00036         device=(char*)malloc((strlen(pdevice)+10)*sizeof(char));
00037         strcpy(device,pdevice);
00038         
00039         active=false;
00040         
00041         
00042         struct termios params;
00043         int ret;
00044         
00045         memset( ¶ms,0, sizeof(params)); 
00046         
00047         
00048         printf("Opening comm %s ... ",device);fflush(stdout);
00049         port = open( device, O_RDWR | O_NONBLOCK ); 
00050         if(port == -1) 
00051         {perror("Failed to open port");return;}
00052         printf("Done\n");
00053         
00054         active=true;
00055         
00056         printf("Set new parameters ... ");fflush(stdout);
00057         params.c_cflag = B9600 | CS8 | CLOCAL | CREAD | IGNPAR;
00058         params.c_iflag = IGNPAR;
00059         params.c_oflag = 0;
00060         
00061         ret=tcsetattr(port, TCSANOW, ¶ms ); 
00062         if( ret < 0 )
00063         {perror("Set serial communication parameters failed");return;}
00064         printf("Done\n");
00065         
00066         return;
00067 }
00068 
00069 class_dioc::~class_dioc()
00070 {
00071         if(active)
00072                 close(port);
00073 }
00074 
00075 void class_dioc::CleanBuffer(void)
00076 {
00077         int ret=1;
00078         char data;
00079         
00080         if(!active)
00081                 return;
00082         
00083         while(ret!=0)
00084         {
00085                 ret=read(port,&data,1);
00086                 if(ret<0)
00087                         return;
00088         }
00089 }
00090 
00091 int class_dioc::SetStatus(id_enum ID, int STATUS)
00092 {
00093         char msg;
00094         
00095         if(STATUS)
00096                 msg = 0x80 | ID;
00097         else
00098                 msg = ID;
00099         
00100         ret = write(port,&msg,sizeof(msg));
00101         if(ret<0)
00102         {
00103                 strcpy(err,"Cannot write to port");
00104                 return -3;
00105         }
00106         
00107         return 0;
00108 }
00109 
00110 int class_dioc::SetStatus(id_enum ID,status_enum STATUS)
00111 {
00112         char msg;
00113         
00114         if(STATUS == ON || STATUS == BLINK3)
00115                 msg = 0x80 | ID;
00116         else
00117                 msg = ID;
00118         
00119         ret = write(port,&msg,sizeof(msg));
00120         if(ret<0)
00121         {
00122                 strcpy(err,"Cannot write to port");
00123                 return -3;
00124         }
00125         
00126         return 0;
00127 }
00128 
00129 int class_dioc::CommStatus(void)
00130 {
00131         unsigned char msg = 0x00;
00132         unsigned char data;
00133         int nbytes_to_read=0;
00134         
00135         
00136         CleanBuffer();
00137         
00138         ret = write(port,&msg,sizeof(msg));
00139         if(ret<0)
00140         {
00141                 strcpy(err,"Cannot write to port");
00142                 return -3;
00143         }
00144         
00145         double ts=ros::Time::now().toSec();
00146         double tl=0;
00147         while(nbytes_to_read!=1)
00148         {
00149                 ret=ioctl(port,FIONREAD,&nbytes_to_read); 
00150                 usleep(1000);
00151                 tl=ros::Time::now().toSec()-ts;
00152                 
00153                 if(tl>1)
00154                 {
00155                         strcpy(err,"DIOC is not responding (read timeout)");
00156                         return -1;
00157                 }
00158         }
00159         
00160         ret=read(port,&data,1);
00161         if(ret<0)
00162         {
00163                 strcpy(err,"Cannot read responce");
00164                 return -3;
00165         }
00166         
00167         if(data == 0x80)
00168                 return 1;
00169         else
00170                 return 0;
00171 }
00172 
00173 int class_dioc::GetStatus(id_enum ID)
00174 {
00175         unsigned char msg;
00176         int nbytes_to_read=0;
00177         unsigned char data;
00178         unsigned char status;
00179         
00180         switch(ID)
00181         {
00182                 case CROSS_A:                   
00183                 case CROSS_B:
00184                         msg=ID;
00185                         break;
00186                 default:
00187                         strcpy(err,"This IO does not respond its current state");
00188                         return -1;
00189         }
00190         
00191         
00192         CleanBuffer();
00193         
00194         ret = write(port,&msg,sizeof(msg));
00195         if(ret<0)
00196         {
00197                 strcpy(err,"Cannot write to port");
00198                 return -3;
00199         }
00200         
00201         double ts=ros::Time::now().toSec();
00202         double tl=0;
00203         while(nbytes_to_read!=1)
00204         {
00205                 ret=ioctl(port,FIONREAD,&nbytes_to_read); 
00206                 usleep(1000);
00207                 tl=ros::Time::now().toSec()-ts;
00208                 
00209                 if(tl>1)
00210                 {
00211                         strcpy(err,"DIOC is not responding (read timeout)");
00212                         return -1;
00213                 }
00214         }
00215         
00216         ret=read(port,&data,1);
00217         if(ret<0)
00218         {
00219                 strcpy(err,"Cannot read responce");
00220                 return -3;
00221         }
00222         
00223         status = (data & 0x80) >> 7;
00224         
00225         return status;
00226 }
00227 
00228 void class_dioc::perr(int ret)
00229 {
00230         if(ret==-1)
00231         {
00232                 printf("Error!!   ");
00233                 printf("%s (raising SIGINT)\n",err);
00234                 raise(SIGINT);
00235         }else if(ret==-2)
00236         {
00237                 printf("Warning!! ");
00238                 printf("%s\n",err);
00239         }else if(ret==-3)
00240         {
00241                 printf("Error!!   ");
00242                 printf("%s, ",err);fflush(stdout);
00243                 perror(NULL);
00244                 raise(SIGINT);
00245         }
00246 }