serial_comm.cpp
Go to the documentation of this file.
00001 #include "mrp2_hardware/serial_comm.h"
00002 
00003 #define MILVUS_EXCEPT(except, msg, ...) \
00004 { \
00005     char buf[1000]; \
00006     snprintf(buf, 1000, msg " (in milvus::SerialComm::%s)" , ##__VA_ARGS__, __FUNCTION__); \
00007     throw except(buf); \
00008 }
00009 
00010 milvus::SerialComm::SerialComm() : fd_(-1)
00011 {
00012 }
00013 
00014 milvus::SerialComm::~SerialComm()
00015 {
00016 }
00017 
00018 int milvus::SerialComm::open_port(std::string port_name, int baudrate, std::string mode_s)
00019 {
00020         int baudr,
00021       status;
00022 
00023   const char *mode = mode_s.c_str();
00024 
00025   switch(baudrate)
00026   {
00027     case      50 : baudr = B50;
00028                    break;
00029     case      75 : baudr = B75;
00030                    break;
00031     case     110 : baudr = B110;
00032                    break;
00033     case     134 : baudr = B134;
00034                    break;
00035     case     150 : baudr = B150;
00036                    break;
00037     case     200 : baudr = B200;
00038                    break;
00039     case     300 : baudr = B300;
00040                    break;
00041     case     600 : baudr = B600;
00042                    break;
00043     case    1200 : baudr = B1200;
00044                    break;
00045     case    1800 : baudr = B1800;
00046                    break;
00047     case    2400 : baudr = B2400;
00048                    break;
00049     case    4800 : baudr = B4800;
00050                    break;
00051     case    9600 : baudr = B9600;
00052                    break;
00053     case   19200 : baudr = B19200;
00054                    break;
00055     case   38400 : baudr = B38400;
00056                    break;
00057     case   57600 : baudr = B57600;
00058                    break;
00059     case  115200 : baudr = B115200;
00060                    break;
00061     case  230400 : baudr = B230400;
00062                    break;
00063     case  460800 : baudr = B460800;
00064                    break;
00065     case  500000 : baudr = B500000;
00066                    break;
00067     case  576000 : baudr = B576000;
00068                    break;
00069     case  921600 : baudr = B921600;
00070                    break;
00071     case 1000000 : baudr = B1000000;
00072                    break;
00073     case 1152000 : baudr = B1152000;
00074                    break;
00075     case 1500000 : baudr = B1500000;
00076                    break;
00077     case 2000000 : baudr = B2000000;
00078                    break;
00079     case 2500000 : baudr = B2500000;
00080                    break;
00081     case 3000000 : baudr = B3000000;
00082                    break;
00083     case 3500000 : baudr = B3500000;
00084                    break;
00085     case 4000000 : baudr = B4000000;
00086                    break;
00087     default      : printf("invalid baudrate\n");
00088                    return(1);
00089                    break;
00090   }
00091 
00092   int cbits=CS8,
00093       cpar=IGNPAR,
00094       bstop=0;
00095 
00096   if(strlen(mode) < 3)
00097   {
00098     printf("invalid mode:%d \"%s\"\n",(int)strlen(mode), mode);
00099     return(1);
00100   }
00101 
00102   switch(mode[0])
00103   {
00104     case '8': cbits = CS8;
00105               break;
00106     case '7': cbits = CS7;
00107               break;
00108     case '6': cbits = CS6;
00109               break;
00110     case '5': cbits = CS5;
00111               break;
00112     default : printf("invalid number of data-bits '%c'\n", mode[0]);
00113               return(1);
00114               break;
00115   }
00116 
00117   switch(mode[1])
00118   {
00119     case 'N':
00120     case 'n': cpar = IGNPAR;
00121               break;
00122     case 'E':
00123     case 'e': cpar = PARENB;
00124               break;
00125     case 'O':
00126     case 'o': cpar = (PARENB | PARODD);
00127               break;
00128     default : printf("invalid parity '%c'\n", mode[1]);
00129               return(1);
00130               break;
00131   }
00132 
00133   switch(mode[2])
00134   {
00135     case '1': bstop = 0;
00136               break;
00137     case '2': bstop = CSTOPB;
00138               break;
00139     default : printf("invalid number of stop bits '%c'\n", mode[2]);
00140               return(1);
00141               break;
00142   }
00143 
00144 
00145   fd_ = open(port_name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
00146   if(fd_==-1)
00147   {
00148     perror("unable to open comport ");
00149     return(1);
00150   }
00151   else
00152   {
00153     fcntl(fd_, F_SETFL, 0);
00154   }
00155 
00156   error = tcgetattr(fd_, &old_port_settings);
00157   if(error==-1)
00158   {
00159     close(fd_);
00160     perror("unable to read portsettings ");
00161     return(1);
00162   }
00163   memset(&new_port_settings, 0, sizeof(new_port_settings));  
00164 
00165   new_port_settings.c_cflag = cbits | cpar | bstop | CLOCAL | CREAD;
00166   new_port_settings.c_iflag = IGNPAR;
00167   new_port_settings.c_oflag = 0;
00168   new_port_settings.c_lflag = 0;
00169   new_port_settings.c_cc[VMIN] = 0;    
00170   new_port_settings.c_cc[VTIME] = 0;    
00171 
00172   cfsetispeed(&new_port_settings, baudr);
00173   cfsetospeed(&new_port_settings, baudr);
00174 
00175   error = tcsetattr(fd_, TCSANOW, &new_port_settings);
00176   if(error==-1)
00177   {
00178     close(fd_);
00179     perror("unable to adjust portsettings ");
00180     return(1);
00181   }
00182 
00183   if(ioctl(fd_, TIOCMGET, &status) == -1)
00184   {
00185     perror("unable to get portstatus");
00186     return(1);
00187   }
00188 
00189   status |= TIOCM_DTR;   
00190   status |= TIOCM_RTS; 
00191 
00192   if(ioctl(fd_, TIOCMSET, &status) == -1)
00193   {
00194     perror("unable to set portstatus");
00195     return(1);
00196   }
00197   
00198 
00199   return(0);
00200 }
00201 
00202 int milvus::SerialComm::close_port() 
00203 {
00204         int status;
00205 
00206         if(ioctl(fd_, TIOCMGET, &status) == -1)
00207         {
00208                 perror("unable to get portstatus");
00209         }
00210 
00211         status &= ~TIOCM_DTR;    /* turn off DTR */
00212         status &= ~TIOCM_RTS;    /* turn off RTS */
00213 
00214         if(ioctl(fd_, TIOCMSET, &status) == -1)
00215         {
00216                 perror("unable to set portstatus");
00217         }
00218 
00219         close(fd_);
00220 }
00221 
00222 int milvus::SerialComm::poll_comport(unsigned char *buf, int size)
00223 {
00224         int n;
00225         n = read(fd_, buf, size);
00226         return(n);
00227 }
00228 
00229 int milvus::SerialComm::send_byte(unsigned char)
00230 {
00231 
00232 }
00233 
00234 int milvus::SerialComm::send_buf(unsigned char *buf, int size)
00235 {
00236         return(write(fd_, buf, size));
00237 }
00238 
00239 void milvus::SerialComm::cputs(const char *)
00240 {
00241 
00242 }
00243 
00244 int milvus::SerialComm::is_dcd_enabled()
00245 {
00246 
00247 }
00248 
00249 int milvus::SerialComm::is_cts_enabled()
00250 {
00251 
00252 }
00253 
00254 int milvus::SerialComm::is_dsr_enabled()
00255 {
00256 
00257 }
00258 
00259 void milvus::SerialComm::enable_dtr()
00260 {
00261 
00262 }
00263 
00264 void milvus::SerialComm::disable_dtr()
00265 {
00266 
00267 }
00268 
00269 void milvus::SerialComm::enable_rts()
00270 {
00271 
00272 }
00273 
00274 void milvus::SerialComm::disable_rts()
00275 {
00276 
00277 }
00278 


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Thu Jun 6 2019 21:43:37