Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

serial.c

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*/
00016 /*---------------------------------------------------------------------------*/
00017 
00018 #include "serial.h"
00019 
00020 static struct   termios oldtio[ MAX_PORT_NUM], newtio[ MAX_PORT_NUM];
00021 static int      fd_map[ MAX_PORT_NUM]={ -1, -1, -1, -1, -1, -1, -1, -1};
00022 
00023 /*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*/
00030 int     FindFD( int port)
00031 {
00032         if( fd_map[ port] == -1)
00033                 return SERIAL_ERROR_FD;
00034 
00035         return fd_map[ port];
00036 }
00037 
00038 /*---------------------------------------------------------------------------*/
00046 /*---------------------------------------------------------------------------*/
00047 int     SerialOpen( int port, int open_flag, struct termios *tio)
00048 {
00049         int fd;
00050         char device[ 80];
00051 
00052         if( fd_map[ port] != -1)                
00053                 return SERIAL_ERROR_OPEN;
00054 
00055         sprintf( device, "/dev/ttyM%d", port);
00056         fd = open( device, open_flag);
00057         if( fd <0)
00058                 return SERIAL_ERROR_OPEN;
00059 
00060         fd_map[ port]= fd;
00061 
00062         tcgetattr( fd, &(oldtio[ port]));       
00063         newtio[ port]= *tio;
00064 
00065         tcflush( fd, TCIFLUSH);
00066         tcsetattr( fd, TCSANOW, &newtio[ port]);
00067 
00068         return fd;
00069 }
00070 
00071 /*---------------------------------------------------------------------------*/
00079 /*---------------------------------------------------------------------------*/
00080 int     SerialWrite( int port, char* str, int len)
00081 {
00082         int fd= FindFD( port);
00083 
00084         if( fd < 0)                     
00085                 return fd;
00086 
00087         return write( fd, str, len);
00088 }
00089 
00090 /*---------------------------------------------------------------------------*/
00099 /*---------------------------------------------------------------------------*/
00100 int     SerialNonBlockRead( int port, char* buf, int len)
00101 {
00102         int res= 0;
00103         int bytes= 0;
00104         int fd= FindFD( port);
00105 
00106         if( fd < 0)                     
00107                 return fd;
00108 
00109         fcntl( fd, F_SETFL, FNDELAY);
00110         res = read( fd, buf, len);
00111         return res;
00112 }
00113 
00114 /*---------------------------------------------------------------------------*/
00123 /*---------------------------------------------------------------------------*/
00124 int     SerialBlockRead( int port, char* buf, int len)
00125 {
00126         int res= 0;
00127         int bytes= 0;
00128         int fd= FindFD( port);
00129 
00130         if( fd < 0)                     
00131                 return fd;
00132 
00133         fcntl( fd, F_SETFL, 0);
00134         res = read( fd, buf, len);
00135         return res;
00136 }
00137 
00138 /*---------------------------------------------------------------------------*/
00144 /*---------------------------------------------------------------------------*/
00145 int     SerialClose( int port)
00146 {
00147         int fd= FindFD( port);
00148 
00149         if( fd < 0)                     
00150                 return fd;
00151 
00152         tcsetattr( fd, TCSANOW, &(oldtio[ port]));
00153         close( fd);
00154 
00155         fd_map[ port]= -1;
00156 
00157         return SERIAL_OK;
00158 }
00159 
00160 /*---------------------------------------------------------------------------*/
00167 /*---------------------------------------------------------------------------*/
00168 int     SerialDataInInputQueue( int port)
00169 {
00170         int bytes= 0;
00171         int fd= FindFD( port);
00172 
00173         if( fd < 0)                     
00174                 return fd;
00175 
00176         ioctl( fd, FIONREAD, &bytes);
00177         return bytes;
00178 }
00179 
00180 /*---------------------------------------------------------------------------*/
00187 /*---------------------------------------------------------------------------*/
00188 int     SerialFlowControl( int port, int control)
00189 {
00190         int fd= FindFD( port);
00191 
00192         if( fd < 0)                     
00193                 return fd;
00194 
00195         if( control == HW_FLOW_CONTROL)
00196                 newtio[ port].c_cflag |= CRTSCTS;
00197         else if( control == SW_FLOW_CONTROL)
00198                 newtio[ port].c_iflag |= (IXON | IXOFF | IXANY);
00199 
00200         tcflush( fd, TCIFLUSH);
00201         tcsetattr( fd, TCSANOW, &newtio[ port]);
00202 
00203         return SERIAL_OK;
00204 }
00205 
00206 /*---------------------------------------------------------------------------*/
00213 /*---------------------------------------------------------------------------*/
00214 int     SerialSetSpeed( int port, unsigned int speed)
00215 {
00216         int i, table_size= 23;
00217         int speed_table1[]={ 0, 50, 75, 110, 134, 150, 200, 300,
00218                              600, 1200, 1800, 2400, 4800, 9600,
00219                              19200, 38400, 57600, 115200, 230400,
00220                              460800, 500000, 576000, 921600};
00221         int speed_table2[]={ B0, B50, B75, B110, B134, B150, B200, B300,
00222                              B600, B1200, B1800, B2400, B4800, B9600,
00223                              B19200, B38400, B57600, B115200, B230400,
00224                              B460800, B500000, B576000, B921600};
00225         int fd= FindFD( port);
00226 
00227         if( fd < 0)                     
00228                 return fd;
00229 
00230         for( i= 1; i< table_size; i++)  
00231                 if( speed_table1[ i] >= speed)
00232                         break;
00233 
00234         cfsetispeed( &newtio[ port], speed_table2[ i]);
00235         cfsetospeed( &newtio[ port], speed_table2[ i]);
00236         tcsetattr( fd, TCSANOW, &newtio[ port]);
00237 
00238         return SERIAL_OK;
00239 }
00240 
00241 /*---------------------------------------------------------------------------*/
00249 /*---------------------------------------------------------------------------*/
00250 int     SerialSetMode( int port, unsigned int mode)
00251 {
00252         int fd= FindFD( port);
00253         if( fd < 0)                     
00254                 return fd;
00255 
00256         return ioctl( fd, MOXA_SET_OP_MODE, &mode);
00257 }
00258 
00259 /*---------------------------------------------------------------------------*/
00268 /*---------------------------------------------------------------------------*/
00269 int     SerialSetParam( int port, int parity, int databits, int stopbit)
00270 {
00271         int fd= FindFD( port);
00272         if( fd < 0)                     
00273                 return fd;
00274 
00275         if( parity == 0)
00276                 newtio[ port].c_cflag &= ~PARENB;
00277         else if( parity == 1)
00278         {
00279                 newtio[ port].c_cflag |= PARENB;
00280                 newtio[ port].c_cflag |= PARODD;
00281         }
00282         else if( parity == 2)
00283         {
00284                 newtio[ port].c_cflag |= PARENB;
00285                 newtio[ port].c_cflag &= ~PARODD;
00286         }
00287         else if( parity == 3)
00288         {
00289                 newtio[ port].c_cflag &= ~PARENB;
00290                 newtio[ port].c_cflag &= ~CSTOPB;
00291         }
00292 
00293         if( databits == 5)
00294                 newtio[ port].c_cflag |= CS5;
00295         else if( databits == 6)
00296                 newtio[ port].c_cflag |= CS6;
00297         else if( databits == 7)
00298                 newtio[ port].c_cflag |= CS7;
00299         else if( databits == 8)
00300                 newtio[ port].c_cflag |= CS8;
00301 
00302         if( stopbit == 1)
00303                 newtio[ port].c_cflag &= ~CSTOPB;
00304         else if( stopbit == 2)
00305                 newtio[ port].c_cflag |= CSTOPB;
00306 
00307         tcflush( fd, TCIFLUSH);
00308         tcsetattr( fd, TCSANOW, &newtio[ port]);
00309 
00310         return SERIAL_OK;
00311 }

Generated on Thu Oct 6 09:13:41 2005 for Example Modbus Library by  doxygen 1.4.4