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

nsser.c

Go to the documentation of this file.
00001 
00025 #include <stdio.h>
00026 #include <assert.h>
00027 #include <stdlib.h>
00028 #include "nsutil.h"
00029 #include "nsser.h"
00030 
00031 
00032 
00033 ser_t openSerialPort(const char *devname, unsigned int BaudRate)
00034 {
00035 #ifdef __MINGW32__
00036         ser_t retval;
00037         retval = CreateFile(devname,
00038         GENERIC_READ | GENERIC_WRITE,
00039         0, NULL, OPEN_EXISTING,
00040             FILE_ATTRIBUTE_NORMAL, NULL);
00041         if (retval == INVALID_HANDLE_VALUE) {
00042          rprintf("Couldn't open serial port: %s", devname);
00043                 rexit(1);
00044         }
00045         if (!SetCommMask(retval, EV_RXCHAR))
00046         rprintf("Couldn't do a SetCommMask(): %s", devname);
00047 
00048         // Set read buffer == 10K, write buffer == 4K
00049         SetupComm(retval, 10240, 4096);
00050 
00051         do {
00052         DCB dcb;
00053 
00054         dcb.DCBlength= sizeof(DCB);
00055         GetCommState(retval, &dcb);
00056         switch(BaudRate) {
00057          case 19200: dcb.BaudRate=CBR_19200;break;
00058          case 57600: dcb.BaudRate=CBR_57600;break;
00059          case 115200: dcb.BaudRate=CBR_115200;break;
00060          default: dcb.BaudRate=CBR_57600;
00061         } 
00062                 dcb.ByteSize= 8;
00063                 dcb.Parity= NOPARITY;
00064                 dcb.StopBits= ONESTOPBIT;
00065                 dcb.fOutxCtsFlow= FALSE;
00066                 dcb.fOutxDsrFlow= FALSE;
00067                 dcb.fInX= FALSE;
00068                 dcb.fOutX= FALSE;
00069                 dcb.fDtrControl= DTR_CONTROL_DISABLE;
00070                 dcb.fRtsControl= RTS_CONTROL_DISABLE;
00071                 dcb.fBinary= TRUE;
00072                 dcb.fParity= FALSE;
00073 
00074         if (!SetCommState(retval, &dcb)) {
00075                         rprintf("%s", "Unable to set up serial port parameters");
00076                         rexit(1);
00077                 }
00078         } while (0);
00079         return retval;
00080 #else
00081         ser_t fd; /* File descriptor for the port */
00082 
00083 
00084         fd = open(devname, O_RDWR | O_NOCTTY | O_NDELAY);
00085         if (fd == -1)
00086                 {
00087                 fprintf(stderr, "Cannot open port %s\n", devname);
00088                 exit(1);
00089                 }
00090         else
00091                 fcntl(fd, F_SETFL, FNDELAY);
00092         set_port_options(fd,BaudRate);
00093         return (fd);
00094 #endif
00095 }
00096 /* Returns number of bytes read, or -1 for error */
00097 #ifdef __MINGW32__
00098 int readSerial(ser_t s, char *buf, int size)
00099 {
00100    BOOL rv;
00101    COMSTAT comStat;
00102    DWORD errorFlags;
00103    DWORD len;
00104    // Only try to read number of bytes in queue
00105    ClearCommError(s, &errorFlags, &comStat);
00106    len = comStat.cbInQue;
00107    if (len > size) len = size;
00108    if (len > 0) {
00109       rv = ReadFile(s, (LPSTR)buf, len, &len, NULL);
00110                         if (!rv) {
00111                                  len= 0;
00112                                  ClearCommError(s, &errorFlags, &comStat);
00113                         }
00114                 if (errorFlags > 0) {
00115                         rprintf("Serial read error");
00116                         ClearCommError(s, &errorFlags, &comStat);
00117                         return -1;
00118                 }
00119         }
00120         return len;
00121 }
00122 #else
00123 
00124 int readSerial(ser_t s, char *buf, int size)
00125 {
00126         int retval;
00127         retval = read(s, buf, size);
00128         if (retval < 0)
00129                 retval = 0;
00130         return retval;
00131 }
00132 
00134 
00135 int writeSerial(ser_t s, char *buf, int size)
00136 {
00137         int retval;
00138         retval = write(s, buf, size);
00139         if (retval < 0)
00140                 retval = 0;
00141         return retval;
00142 }
00143 
00144 int set_port_options(int fd, unsigned int BaudRate)
00145 {
00146         int retval;
00147         struct termios options;
00148 /*
00149  *      * Get the current options for the port...
00150  *      */
00151 
00152     retval = tcgetattr(fd, &options);
00153                 assert(retval == 0 && "tcgetattr problem");
00154 
00155 /*
00156  *      * Set the baud rates to 19200...
00157  *      */
00158         switch(BaudRate) {
00159          case 19200:  cfsetispeed(&options, B19200);cfsetospeed(&options, B19200);break;
00160          case 57600:  cfsetispeed(&options, B57600);cfsetospeed(&options, B57600);break;
00161          case 115200:  cfsetispeed(&options, B115200);cfsetospeed(&options, B115200);break;
00162          default: cfsetispeed(&options, B57600);cfsetospeed(&options, B57600);
00163         } 
00164    
00165 
00166 
00167 /*
00168  *      * Enable the receiver and set local mode...
00169  *      */
00170 
00171     options.c_cflag |= (CLOCAL | CREAD);
00172 
00173 /* 8-N-1 */
00174 options.c_cflag &= ~PARENB;
00175 options.c_cflag &= ~CSTOPB;
00176 options.c_cflag &= ~CSIZE;
00177 options.c_cflag |= CS8;
00178 
00179 /* Disable flow-control */
00180   options.c_cflag &= ~CRTSCTS;
00181 
00182 /* Raw processing mode */
00183         options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
00184 
00185 
00186 
00187 
00188 
00189 /*
00190  *      * Set the new options for the port...
00191  *      */
00192 
00193         retval = tcsetattr(fd, TCSANOW, &options);
00194         return retval;
00195 }
00196 #endif
00197 
00198 

Generated on Tue Feb 8 00:05:16 2005 for Neuroserver by doxygen 1.3.3